Skip to content

Thoughts on breaking usage convention for Data.withUnsafeBytes? #73

Description

@anohren

It seems to me that this initializer is not following the documentation of withUnsafeBytes which says not to use the pointer outside the passed closure. What's the reason for writing the init like this? Of course, in absence of a comment, I don't know of its specific use case.

AFAIK Data doesn't guarantee using contiguous memory. What are the implications of that here?

Do you happen to know why the pointer shouldn't be accessed outside the closure?

   public init(data : Data, cache : FBReaderCache? = FBReaderCache()) {
       self.count = data.count
       self.cache = cache
       var pointer : UnsafePointer<UInt8>! = nil
       data.withUnsafeBytes { (u8Ptr: UnsafePointer<UInt8>) in
           pointer = u8Ptr
       }
       self.buffer = UnsafeRawPointer(pointer)
   }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions