|
49 | 49 | #endif
|
50 | 50 |
|
51 | 51 | internal import _FoundationCShims
|
| 52 | +import Builtin |
52 | 53 |
|
53 | 54 | #if canImport(Darwin)
|
54 | 55 | import Darwin
|
@@ -604,6 +605,9 @@ internal final class __DataStorage : @unchecked Sendable {
|
604 | 605 |
|
605 | 606 | @frozen
|
606 | 607 | @available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
|
| 608 | +#if compiler(>=6.2) |
| 609 | +@_addressableForDependencies |
| 610 | +#endif |
607 | 611 | public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollection, RangeReplaceableCollection, MutableDataProtocol, ContiguousBytes, Sendable {
|
608 | 612 |
|
609 | 613 | public typealias Index = Int
|
@@ -2198,7 +2202,107 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
|
2198 | 2202 | public func withUnsafeBytes<ResultType>(_ body: (UnsafeRawBufferPointer) throws -> ResultType) rethrows -> ResultType {
|
2199 | 2203 | return try _representation.withUnsafeBytes(body)
|
2200 | 2204 | }
|
2201 |
| - |
| 2205 | + |
| 2206 | +#if compiler(>=6.2) && $LifetimeDependence |
| 2207 | + @available(FoundationSpan 6.2, *) |
| 2208 | + public var bytes: RawSpan { |
| 2209 | + @lifetime(borrow self) |
| 2210 | + borrowing get { |
| 2211 | + let buffer: UnsafeRawBufferPointer |
| 2212 | + switch _representation { |
| 2213 | + case .empty: |
| 2214 | + buffer = UnsafeRawBufferPointer(start: nil, count: 0) |
| 2215 | + case .inline: |
| 2216 | + buffer = unsafe UnsafeRawBufferPointer( |
| 2217 | + start: UnsafeRawPointer(Builtin.addressOfBorrow(self)), |
| 2218 | + count: _representation.count |
| 2219 | + ) |
| 2220 | + case .large(let slice): |
| 2221 | + buffer = unsafe UnsafeRawBufferPointer( |
| 2222 | + start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count |
| 2223 | + ) |
| 2224 | + case .slice(let slice): |
| 2225 | + buffer = unsafe UnsafeRawBufferPointer( |
| 2226 | + start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count |
| 2227 | + ) |
| 2228 | + } |
| 2229 | + let span = unsafe RawSpan(_unsafeBytes: buffer) |
| 2230 | + return unsafe _overrideLifetime(span, borrowing: self) |
| 2231 | + } |
| 2232 | + } |
| 2233 | + |
| 2234 | + @available(FoundationSpan 6.2, *) |
| 2235 | + public var span: Span<UInt8> { |
| 2236 | + @lifetime(borrow self) |
| 2237 | + borrowing get { |
| 2238 | + let span = unsafe bytes._unsafeView(as: UInt8.self) |
| 2239 | + return _overrideLifetime(span, borrowing: self) |
| 2240 | + } |
| 2241 | + } |
| 2242 | +#endif |
| 2243 | + |
| 2244 | +#if compiler(>=5.9) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors |
| 2245 | + @available(FoundationSpan 6.2, *) |
| 2246 | + public var mutableBytes: MutableRawSpan { |
| 2247 | + @lifetime(&self) |
| 2248 | + mutating get { |
| 2249 | + let buffer: UnsafeMutableRawBufferPointer |
| 2250 | + switch _representation { |
| 2251 | + case .empty: |
| 2252 | + buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0) |
| 2253 | + case .inline: |
| 2254 | + buffer = unsafe UnsafeMutableRawBufferPointer( |
| 2255 | + start: UnsafeMutableRawPointer(Builtin.addressOfBorrow(self)), |
| 2256 | + count: _representation.count |
| 2257 | + ) |
| 2258 | + case .large(let slice): |
| 2259 | + buffer = unsafe UnsafeMutableRawBufferPointer( |
| 2260 | + start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count |
| 2261 | + ) |
| 2262 | + case .slice(let slice): |
| 2263 | + buffer = unsafe UnsafeMutableRawBufferPointer( |
| 2264 | + start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count |
| 2265 | + ) |
| 2266 | + } |
| 2267 | + let span = unsafe MutableRawSpan(_unsafeBytes: buffer) |
| 2268 | + return unsafe _overrideLifetime(span, mutating: &self) |
| 2269 | + } |
| 2270 | + } |
| 2271 | + |
| 2272 | + @available(FoundationSpan 6.2, *) |
| 2273 | + public var mutableSpan: MutableSpan<UInt8> { |
| 2274 | + @lifetime(&self) |
| 2275 | + mutating get { |
| 2276 | +#if false // see https://github.com/swiftlang/swift/issues/81218 |
| 2277 | + var bytes = mutableBytes |
| 2278 | + let span = unsafe bytes._unsafeMutableView(as: UInt8.self) |
| 2279 | + return _overrideLifetime(span, mutating: &self) |
| 2280 | +#else |
| 2281 | + let buffer: UnsafeMutableRawBufferPointer |
| 2282 | + switch _representation { |
| 2283 | + case .empty: |
| 2284 | + buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0) |
| 2285 | + case .inline: |
| 2286 | + buffer = unsafe UnsafeMutableRawBufferPointer( |
| 2287 | + start: UnsafeMutableRawPointer(Builtin.addressOfBorrow(self)), |
| 2288 | + count: _representation.count |
| 2289 | + ) |
| 2290 | + case .large(let slice): |
| 2291 | + buffer = unsafe UnsafeMutableRawBufferPointer( |
| 2292 | + start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count |
| 2293 | + ) |
| 2294 | + case .slice(let slice): |
| 2295 | + buffer = unsafe UnsafeMutableRawBufferPointer( |
| 2296 | + start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count |
| 2297 | + ) |
| 2298 | + } |
| 2299 | + let span = unsafe MutableSpan<UInt8>(_unsafeBytes: buffer) |
| 2300 | + return unsafe _overrideLifetime(span, mutating: &self) |
| 2301 | +#endif |
| 2302 | + } |
| 2303 | + } |
| 2304 | +#endif // $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors |
| 2305 | + |
2202 | 2306 | @_alwaysEmitIntoClient
|
2203 | 2307 | public func withContiguousStorageIfAvailable<ResultType>(_ body: (_ buffer: UnsafeBufferPointer<UInt8>) throws -> ResultType) rethrows -> ResultType? {
|
2204 | 2308 | return try _representation.withUnsafeBytes {
|
@@ -2870,3 +2974,57 @@ extension Data : Codable {
|
2870 | 2974 | }
|
2871 | 2975 | }
|
2872 | 2976 | }
|
| 2977 | + |
| 2978 | +// TODO: remove once _overrideLifetime is public in the standard library |
| 2979 | +#if compiler(>=6.2) && $LifetimeDependence |
| 2980 | +/// Unsafely discard any lifetime dependency on the `dependent` argument. Return |
| 2981 | +/// a value identical to `dependent` with a lifetime dependency on the caller's |
| 2982 | +/// borrow scope of the `source` argument. |
| 2983 | +@unsafe |
| 2984 | +@_unsafeNonescapableResult |
| 2985 | +@_alwaysEmitIntoClient |
| 2986 | +@_transparent |
| 2987 | +@lifetime(borrow source) |
| 2988 | +internal func _overrideLifetime< |
| 2989 | + T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable |
| 2990 | +>( |
| 2991 | + _ dependent: consuming T, borrowing source: borrowing U |
| 2992 | +) -> T { |
| 2993 | + dependent |
| 2994 | +} |
| 2995 | + |
| 2996 | +/// Unsafely discard any lifetime dependency on the `dependent` argument. Return |
| 2997 | +/// a value identical to `dependent` that inherits all lifetime dependencies from |
| 2998 | +/// the `source` argument. |
| 2999 | +@unsafe |
| 3000 | +@_unsafeNonescapableResult |
| 3001 | +@_alwaysEmitIntoClient |
| 3002 | +@_transparent |
| 3003 | +@lifetime(copy source) |
| 3004 | +internal func _overrideLifetime< |
| 3005 | + T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable |
| 3006 | +>( |
| 3007 | + _ dependent: consuming T, copying source: borrowing U |
| 3008 | +) -> T { |
| 3009 | + dependent |
| 3010 | +} |
| 3011 | +#endif |
| 3012 | + |
| 3013 | +#if compiler(>=5.9) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors |
| 3014 | +/// Unsafely discard any lifetime dependency on the `dependent` argument. |
| 3015 | +/// Return a value identical to `dependent` with a lifetime dependency |
| 3016 | +/// on the caller's exclusive borrow scope of the `source` argument. |
| 3017 | +@unsafe |
| 3018 | +@_unsafeNonescapableResult |
| 3019 | +@_alwaysEmitIntoClient |
| 3020 | +@_transparent |
| 3021 | +@lifetime(&source) |
| 3022 | +internal func _overrideLifetime< |
| 3023 | + T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable |
| 3024 | +>( |
| 3025 | + _ dependent: consuming T, |
| 3026 | + mutating source: inout U |
| 3027 | +) -> T { |
| 3028 | + dependent |
| 3029 | +} |
| 3030 | +#endif // $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors |
0 commit comments