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 @@ -635,9 +635,10 @@ extension Span where Element: ~Copyable {
635
635
public func withUnsafeBufferPointer< E: Error , Result: ~ Copyable> (
636
636
_ body: ( _ buffer: UnsafeBufferPointer < Element > ) throws ( E ) -> Result
637
637
) throws ( E ) -> Result {
638
- guard let pointer = _pointer else {
638
+ guard let pointer = _pointer, _count > 0 else {
639
639
return try unsafe body( . init( start: nil , count: 0 ) )
640
640
}
641
+ // manual memory rebinding to avoid recalculating the alignment checks
641
642
let binding = Builtin . bindMemory (
642
643
pointer. _rawValue, count. _builtinWordValue, Element . self
643
644
)
@@ -667,8 +668,11 @@ extension Span where Element: BitwiseCopyable {
667
668
public func withUnsafeBytes< E: Error , Result: ~ Copyable> (
668
669
_ body: ( _ buffer: UnsafeRawBufferPointer ) throws ( E ) -> Result
669
670
) throws ( E ) -> Result {
670
- try unsafe body(
671
- . init( start: _pointer , count: _count * MemoryLayout< Element > . stride )
671
+ guard let _pointer, _count > 0 else {
672
+ return try unsafe body( . init( start: nil , count: 0 ) )
673
+ }
674
+ return try unsafe body(
675
+ . init ( start: _pointer , count: _count & * MemoryLayout< Element> . stride)
672
676
)
673
677
}
674
678
}
You can’t perform that action at this time.
0 commit comments