@@ -137,9 +137,20 @@ extension Data : ContiguousBytes { }
137137
138138@available ( macOS 10 . 10 , iOS 8 . 0 , watchOS 2 . 0 , tvOS 9 . 0 , * )
139139extension UnsafeRawBufferPointer : ContiguousBytes {
140- // TODO: Generalize for typed throws
141- @inlinable
142- public func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) rethrows -> R {
140+ // TODO: This should be limited to DATA_LEGACY_ABI once clients are rebuilt
141+ @available ( macOS, obsoleted: 1.0 )
142+ @available ( iOS, obsoleted: 1.0 )
143+ @available ( watchOS, obsoleted: 1.0 )
144+ @available ( tvOS, obsoleted: 1.0 )
145+ @available ( visionOS, obsoleted: 1.0 )
146+ @usableFromInline
147+ @abi ( func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer) throws -> R) throws -> R)
148+ func __legacy_withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
149+ return try withUnsafeBytes ( body)
150+ }
151+
152+ @_alwaysEmitIntoClient
153+ public func withUnsafeBytes< R, E> ( _ body: ( UnsafeRawBufferPointer ) throws ( E ) -> R ) throws ( E) -> R {
143154 return try body ( self )
144155 }
145156
@@ -152,9 +163,20 @@ extension UnsafeRawBufferPointer : ContiguousBytes {
152163
153164@available ( macOS 10 . 10 , iOS 8 . 0 , watchOS 2 . 0 , tvOS 9 . 0 , * )
154165extension UnsafeMutableRawBufferPointer : ContiguousBytes {
155- // TODO: Generalize for typed throws
156- @inlinable
157- public func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) rethrows -> R {
166+ // TODO: This should be limited to DATA_LEGACY_ABI once clients are rebuilt
167+ @available ( macOS, obsoleted: 1.0 )
168+ @available ( iOS, obsoleted: 1.0 )
169+ @available ( watchOS, obsoleted: 1.0 )
170+ @available ( tvOS, obsoleted: 1.0 )
171+ @available ( visionOS, obsoleted: 1.0 )
172+ @usableFromInline
173+ @abi ( func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer) throws -> R) throws -> R)
174+ func __legacy_withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
175+ return try withUnsafeBytes ( body)
176+ }
177+
178+ @_alwaysEmitIntoClient
179+ public func withUnsafeBytes< R, E> ( _ body: ( UnsafeRawBufferPointer ) throws ( E ) -> R ) throws ( E) -> R {
158180 return try body ( UnsafeRawBufferPointer ( self ) )
159181 }
160182
@@ -168,9 +190,20 @@ extension UnsafeMutableRawBufferPointer : ContiguousBytes {
168190// FIXME: When possible, expand conformance to `where Element : Trivial`.
169191@available ( macOS 10 . 10 , iOS 8 . 0 , watchOS 2 . 0 , tvOS 9 . 0 , * )
170192extension UnsafeBufferPointer : ContiguousBytes where Element == UInt8 {
171- // TODO: Generalize for typed throws
172- @inlinable
173- public func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) rethrows -> R {
193+ // TODO: This should be limited to DATA_LEGACY_ABI once clients are rebuilt
194+ @available ( macOS, obsoleted: 1.0 )
195+ @available ( iOS, obsoleted: 1.0 )
196+ @available ( watchOS, obsoleted: 1.0 )
197+ @available ( tvOS, obsoleted: 1.0 )
198+ @available ( visionOS, obsoleted: 1.0 )
199+ @usableFromInline
200+ @abi ( func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer) throws -> R) throws -> R)
201+ func __legacy_withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
202+ return try withUnsafeBytes ( body)
203+ }
204+
205+ @_alwaysEmitIntoClient
206+ public func withUnsafeBytes< R, E> ( _ body: ( UnsafeRawBufferPointer ) throws ( E ) -> R ) throws ( E) -> R {
174207 return try body ( UnsafeRawBufferPointer ( self ) )
175208 }
176209
@@ -184,9 +217,20 @@ extension UnsafeBufferPointer : ContiguousBytes where Element == UInt8 {
184217// FIXME: When possible, expand conformance to `where Element : Trivial`.
185218@available ( macOS 10 . 10 , iOS 8 . 0 , watchOS 2 . 0 , tvOS 9 . 0 , * )
186219extension UnsafeMutableBufferPointer : ContiguousBytes where Element == UInt8 {
187- // TODO: Generalize for typed throws
188- @inlinable
189- public func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) rethrows -> R {
220+ // TODO: This should be limited to DATA_LEGACY_ABI once clients are rebuilt
221+ @available ( macOS, obsoleted: 1.0 )
222+ @available ( iOS, obsoleted: 1.0 )
223+ @available ( watchOS, obsoleted: 1.0 )
224+ @available ( tvOS, obsoleted: 1.0 )
225+ @available ( visionOS, obsoleted: 1.0 )
226+ @usableFromInline
227+ @abi ( func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer) throws -> R) throws -> R)
228+ func __legacy_withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
229+ return try withUnsafeBytes ( body)
230+ }
231+
232+ @_alwaysEmitIntoClient
233+ public func withUnsafeBytes< R, E> ( _ body: ( UnsafeRawBufferPointer ) throws ( E ) -> R ) throws ( E) -> R {
190234 return try body ( UnsafeRawBufferPointer ( self ) )
191235 }
192236
@@ -200,9 +244,20 @@ extension UnsafeMutableBufferPointer : ContiguousBytes where Element == UInt8 {
200244// FIXME: When possible, expand conformance to `where Element : Trivial`.
201245@available ( macOS 10 . 10 , iOS 8 . 0 , watchOS 2 . 0 , tvOS 9 . 0 , * )
202246extension EmptyCollection : ContiguousBytes where Element == UInt8 {
203- // TODO: Generalize for typed throws
204- @inlinable
205- public func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) rethrows -> R {
247+ // TODO: This should be limited to DATA_LEGACY_ABI once clients are rebuilt
248+ @available ( macOS, obsoleted: 1.0 )
249+ @available ( iOS, obsoleted: 1.0 )
250+ @available ( watchOS, obsoleted: 1.0 )
251+ @available ( tvOS, obsoleted: 1.0 )
252+ @available ( visionOS, obsoleted: 1.0 )
253+ @usableFromInline
254+ @abi ( func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer) throws -> R) throws -> R)
255+ func __legacy_withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
256+ return try withUnsafeBytes ( body)
257+ }
258+
259+ @_alwaysEmitIntoClient
260+ public func withUnsafeBytes< R, E> ( _ body: ( UnsafeRawBufferPointer ) throws ( E ) -> R ) throws ( E) -> R {
206261 return try body ( UnsafeRawBufferPointer ( start: nil , count: 0 ) )
207262 }
208263
@@ -216,11 +271,22 @@ extension EmptyCollection : ContiguousBytes where Element == UInt8 {
216271// FIXME: When possible, expand conformance to `where Element : Trivial`.
217272@available ( macOS 10 . 10 , iOS 8 . 0 , watchOS 2 . 0 , tvOS 9 . 0 , * )
218273extension CollectionOfOne : ContiguousBytes where Element == UInt8 {
219- // TODO: Generalize for typed throws
220- @inlinable
221- public func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) rethrows -> R {
274+ // TODO: This should be limited to DATA_LEGACY_ABI once clients are rebuilt
275+ @available ( macOS, obsoleted: 1.0 )
276+ @available ( iOS, obsoleted: 1.0 )
277+ @available ( watchOS, obsoleted: 1.0 )
278+ @available ( tvOS, obsoleted: 1.0 )
279+ @available ( visionOS, obsoleted: 1.0 )
280+ @usableFromInline
281+ @abi ( func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer) throws -> R) throws -> R)
282+ func __legacy_withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
283+ return try withUnsafeBytes ( body)
284+ }
285+
286+ @_alwaysEmitIntoClient
287+ public func withUnsafeBytes< R, E> ( _ body: ( UnsafeRawBufferPointer ) throws ( E ) -> R ) throws ( E) -> R {
222288 let element = self . first!
223- return try Swift . withUnsafeBytes ( of: element) { ( buffer) in
289+ return try Swift . withUnsafeBytes ( of: element) { ( buffer) throws ( E ) in
224290 return try body ( buffer)
225291 }
226292 }
@@ -240,15 +306,39 @@ extension CollectionOfOne : ContiguousBytes where Element == UInt8 {
240306
241307@available ( macOS 10 . 10 , iOS 8 . 0 , watchOS 2 . 0 , tvOS 9 . 0 , * )
242308extension Slice : ContiguousBytes where Base : ContiguousBytes {
243- // TODO: Generalize for typed throws
244- @inlinable
245- public func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) rethrows -> R {
309+ // TODO: This should be limited to DATA_LEGACY_ABI once clients are rebuilt
310+ @available ( macOS, obsoleted: 1.0 )
311+ @available ( iOS, obsoleted: 1.0 )
312+ @available ( watchOS, obsoleted: 1.0 )
313+ @available ( tvOS, obsoleted: 1.0 )
314+ @available ( visionOS, obsoleted: 1.0 )
315+ @usableFromInline
316+ @abi ( func withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer) throws -> R) throws -> R)
317+ func __legacy_withUnsafeBytes< R> ( _ body: ( UnsafeRawBufferPointer ) throws -> R ) throws -> R {
318+ return try withUnsafeBytes ( body)
319+ }
320+
321+ @_alwaysEmitIntoClient
322+ public func withUnsafeBytes< R, E> ( _ body: ( UnsafeRawBufferPointer ) throws ( E ) -> R ) throws ( E) -> R {
246323 let offset = base. distance ( from: base. startIndex, to: self . startIndex)
247- return try base. withUnsafeBytes { ptr in
324+ #if !hasFeature(Embedded)
325+ do {
326+ return try base. withUnsafeBytes { ( ptr) in
327+ let slicePtr = ptr. baseAddress? . advanced ( by: offset)
328+ let sliceBuffer = UnsafeRawBufferPointer ( start: slicePtr, count: self . count)
329+ return try body ( sliceBuffer)
330+ }
331+ } catch let error {
332+ // Note: withUnsafeBytes is rethrowing, so we have an "any Error" here that needs casting.
333+ throw error as! E
334+ }
335+ #else
336+ return try base. withUnsafeBytes { ( ptr) throws ( E) in
248337 let slicePtr = ptr. baseAddress? . advanced ( by: offset)
249338 let sliceBuffer = UnsafeRawBufferPointer ( start: slicePtr, count: self . count)
250339 return try body ( sliceBuffer)
251340 }
341+ #endif
252342 }
253343}
254344
0 commit comments