This does not seem to be the case:

  1> import Foundation
  2> var data = "Foo Bar".data(using: .ascii)!
data: Foundation.Data = 7 bytes
  3> data.withContiguousStorageIfAvailable { _ in return 1 }
$R0: Int? = nil

I, too, would love to see a way to access byte buffers from UInt8 collections (including Data) in a library without forcing clients to link against Foundation.

The lack of this is actually obnoxious enough that I made my own library just to reimplement DataProtocol without requiring Foundation. It's not an ideal solution, though, since in order for Data and DispatchData to be properly supported, a client that actually does link against Foundation has to be sure to link against the CSDataProtocol+Foundation version of the library, or else any Datas that get passed in will end up taking the slow path. It would be great if we didn't have to do this.

DataProtocol is so small, lightweight, and fundamental in purpose that it really makes no sense for it not to be part of the standard library, IMO.

1 Like