File tree Expand file tree Collapse file tree 2 files changed +11
-4
lines changed Expand file tree Collapse file tree 2 files changed +11
-4
lines changed Original file line number Diff line number Diff line change @@ -469,7 +469,10 @@ extension RawSpan {
469
469
public func withUnsafeBytes< E: Error , Result: ~ Copyable> (
470
470
_ body: ( _ buffer: UnsafeRawBufferPointer ) throws ( E ) -> Result
471
471
) throws ( E ) -> Result {
472
- try unsafe body( . init( start: _pointer , count: byteCount ) )
472
+ guard let _pointer, byteCount > 0 else {
473
+ return try unsafe body( . init( start: nil , count: 0 ) )
474
+ }
475
+ return try unsafe body( . init ( start: _pointer , count: byteCount ) )
473
476
}
474
477
}
475
478
Original file line number Diff line number Diff line change @@ -622,9 +622,10 @@ extension Span where Element: ~Copyable {
622
622
public func withUnsafeBufferPointer< E: Error , Result: ~ Copyable> (
623
623
_ body: ( _ buffer: UnsafeBufferPointer < Element > ) throws ( E ) -> Result
624
624
) throws ( E ) -> Result {
625
- guard let pointer = _pointer else {
625
+ guard let pointer = _pointer, _count > 0 else {
626
626
return try unsafe body( . init( start: nil , count: 0 ) )
627
627
}
628
+ // manual memory rebinding to avoid recalculating the alignment checks
628
629
let binding = Builtin . bindMemory (
629
630
pointer. _rawValue, count. _builtinWordValue, Element . self
630
631
)
@@ -654,8 +655,11 @@ extension Span where Element: BitwiseCopyable {
654
655
public func withUnsafeBytes< E: Error , Result: ~ Copyable> (
655
656
_ body: ( _ buffer: UnsafeRawBufferPointer ) throws ( E ) -> Result
656
657
) throws ( E ) -> Result {
657
- try unsafe body(
658
- . init( start: _pointer , count: _count * MemoryLayout< Element > . stride )
658
+ guard let _pointer, _count > 0 else {
659
+ return try unsafe body( . init( start: nil , count: 0 ) )
660
+ }
661
+ return try unsafe body(
662
+ . init ( start: _pointer , count: _count & * MemoryLayout< Element> . stride)
659
663
)
660
664
}
661
665
}
You can’t perform that action at this time.
0 commit comments