@@ -770,35 +770,29 @@ public final class Tensor<T: Scalar>: Equatable {
770
770
/// - Parameter body: A closure that receives an `UnsafeBufferPointer<T>` bound to the tensor’s data.
771
771
/// - Returns: The value returned by `body`.
772
772
/// - Throws: Any error thrown by `body`.
773
- public func withUnsafeBytes< R> ( _ body: ( UnsafeBufferPointer < T > ) throws -> R ) throws -> R {
774
- var result : Result < R , Error > ?
775
- anyTensor. bytes { pointer, count, _ in
776
- result = Result { try body (
777
- UnsafeBufferPointer (
778
- start: pointer. assumingMemoryBound ( to: T . self) ,
779
- count: count
780
- )
781
- ) }
773
+ public func withUnsafeBytes< R> ( _ body: ( UnsafeBufferPointer < T > ) throws -> R ) rethrows -> R {
774
+ try withoutActuallyEscaping ( body) { body in
775
+ var result : Result < R , Error > ?
776
+ anyTensor. bytes { pointer, count, _ in
777
+ result = Result { try body ( UnsafeBufferPointer ( start: pointer. assumingMemoryBound ( to: T . self) , count: count) ) }
778
+ }
779
+ return try result!. get ( )
782
780
}
783
- return try result!. get ( )
784
781
}
785
782
786
783
/// Calls the closure with a typed, mutable buffer pointer over the tensor’s elements.
787
784
///
788
785
/// - Parameter body: A closure that receives an `UnsafeMutableBufferPointer<T>` bound to the tensor’s data.
789
786
/// - Returns: The value returned by `body`.
790
787
/// - Throws: Any error thrown by `body`.
791
- public func withUnsafeMutableBytes< R> ( _ body: ( UnsafeMutableBufferPointer < T > ) throws -> R ) throws -> R {
792
- var result : Result < R , Error > ?
793
- anyTensor. mutableBytes { pointer, count, _ in
794
- result = Result { try body (
795
- UnsafeMutableBufferPointer (
796
- start: pointer. assumingMemoryBound ( to: T . self) ,
797
- count: count
798
- )
799
- ) }
788
+ public func withUnsafeMutableBytes< R> ( _ body: ( UnsafeMutableBufferPointer < T > ) throws -> R ) rethrows -> R {
789
+ try withoutActuallyEscaping ( body) { body in
790
+ var result : Result < R , Error > ?
791
+ anyTensor. mutableBytes { pointer, count, _ in
792
+ result = Result { try body ( UnsafeMutableBufferPointer ( start: pointer. assumingMemoryBound ( to: T . self) , count: count) ) }
793
+ }
794
+ return try result!. get ( )
800
795
}
801
- return try result!. get ( )
802
796
}
803
797
804
798
/// Resizes the tensor to a new shape.
@@ -830,9 +824,8 @@ public extension Tensor {
830
824
/// Returns the tensor's elements as an array of scalars.
831
825
///
832
826
/// - Returns: An array of scalars of type `T`.
833
- /// - Throws: An error if the underlying data cannot be accessed.
834
- func scalars( ) throws -> [ T ] {
835
- try withUnsafeBytes { Array ( $0) }
827
+ func scalars( ) -> [ T ] {
828
+ withUnsafeBytes { Array ( $0) }
836
829
}
837
830
}
838
831
0 commit comments