As we approach the release of Swift 6.2, I would like to provide some information about how to support Span
in packages. As you may have noticed in the pre-released compilers, Span
and its related types are being added to the standard library with backdeployment support for Darwin-based platforms. While the types are backdeployable, there is a limit to how far back they can be back-deployed, so that there are restrictions both on the compiler and on the deployment.
The Span
family of types includes: Span<T>
, RawSpan
, MutableSpan<T>
, MutableRawSpan
, OutputSpan<T>
and OutputRawSpan
. Note that UTF8Span
does not have back-deployment support.
To use APIs involving the Span
family of types, you must use a Swift 6.2 compiler. This is the only requirement for platforms without a stable ABI, such as Linux, Windows, and embedded targets.
On platforms with a stable ABI, API using the Span
family of types can execute on a wide range of OS versions, starting with macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, and visionOS 1.0.
These restrictions can be accommodated with some annotations, namely conditional compilation and availability annotations. Here is an example declaration for extensions to ByteBuffer
:
extension ByteBuffer {
#if compiler(>=6.2)
@available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *)
public var someBytes: RawSpan {
@_lifetime(self)
borrowing get { /* ... */ }
}
@available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *)
public init(copying bytes: RawSpan) { /* ... */ }
#endif
}