@@ -76,35 +76,36 @@ public struct Array2d<T: Equatable & AdditiveArithmetic & Sendable>: Equatable,
7676 columnCount: columnCount)
7777 }
7878
79- /// Provides scoped access to the underlying buffer storing the array's data.
79+ /// Provides scoped access to the underlying buffer storing the array's data using a Span .
8080 ///
8181 /// Use this method when you need temporary read-only access to the array's contiguous storage.
82- /// The buffer pointer is only valid for the duration of the closure's execution.
82+ /// The Span is only valid for the duration of the closure's execution.
8383 ///
84- /// - Parameter body: A closure that takes an `UnsafeBufferPointer ` to the array's data.
85- /// The buffer pointer argument is valid only for the duration of the closure's execution.
84+ /// - Parameter body: A closure that takes a `Span<T> ` to the array's data.
85+ /// The Span argument is valid only for the duration of the closure's execution.
8686 /// - Returns: The return value of the `body` closure.
8787 /// - Throws: Rethrows any error thrown by the `body` closure.
88- public func withUnsafeData< Return> ( _ body: ( UnsafeBufferPointer < T > ) throws -> Return ) rethrows -> Return {
89- try data. withUnsafeBufferPointer { pointer in
90- try body ( pointer)
91- }
88+ @inlinable
89+ public func withDataSpan< Return> ( _ body: ( Span < T > ) throws -> Return ) rethrows -> Return {
90+ try body ( data. span)
9291 }
9392
9493 /// Provides scoped access to the underlying buffer storing the array's data for mutation.
9594 ///
9695 /// Use this method when you need temporary read-write access to the array's contiguous storage.
97- /// The buffer pointer is only valid for the duration of the closure's execution.
96+ /// The MutableSpan is only valid for the duration of the closure's execution.
9897 ///
99- /// - Parameter body: A closure that takes an `UnsafeMutableBufferPointer ` to the array's data.
100- /// The buffer pointer argument is valid only for the duration of the closure's execution.
98+ /// - Parameter body: A closure that takes a `MutableSpan<T> ` to the array's data.
99+ /// The MutableSpan argument is valid only for the duration of the closure's execution.
101100 /// - Returns: The return value of the `body` closure.
102101 /// - Throws: Rethrows any error thrown by the `body` closure.
103- public mutating func withUnsafeMutableData< Return> ( _ body: ( UnsafeMutableBufferPointer < T > ) throws
104- -> Return ) rethrows -> Return
102+ @inlinable
103+ public mutating func withMutableDataSpan< Return> ( _ body: ( inout MutableSpan < T > ) throws -> Return ) rethrows
104+ -> Return
105105 {
106- try data. withUnsafeMutableBufferPointer { pointer in
107- try body ( pointer)
106+ try data. withUnsafeMutableBufferPointer { buffer in
107+ var span = buffer. mutableSpan
108+ return try body ( & span)
108109 }
109110 }
110111}
0 commit comments