@@ -770,35 +770,29 @@ public final class Tensor<T: Scalar>: Equatable {
770770 /// - Parameter body: A closure that receives an `UnsafeBufferPointer<T>` bound to the tensor’s data.
771771 /// - Returns: The value returned by `body`.
772772 /// - 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 ( )
782780 }
783- return try result!. get ( )
784781 }
785782
786783 /// Calls the closure with a typed, mutable buffer pointer over the tensor’s elements.
787784 ///
788785 /// - Parameter body: A closure that receives an `UnsafeMutableBufferPointer<T>` bound to the tensor’s data.
789786 /// - Returns: The value returned by `body`.
790787 /// - 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 ( )
800795 }
801- return try result!. get ( )
802796 }
803797
804798 /// Resizes the tensor to a new shape.
@@ -830,9 +824,8 @@ public extension Tensor {
830824 /// Returns the tensor's elements as an array of scalars.
831825 ///
832826 /// - 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) }
836829 }
837830}
838831
0 commit comments