Skip to content

Commit 82fa4b0

Browse files
committed
[stdlib] relax stride check
- The stride check in `UnsafePointer.withMemoryRebound` makes less sense when rebinding memory for a single element. - This skips the stride-matching portion of the `_debugPrecondition` in `withMemoryRebound` when `count == 1`.
1 parent 496da9d commit 82fa4b0

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

stdlib/public/core/UnsafePointer.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,10 @@ public struct UnsafePointer<Pointee>: _Pointer {
315315
) rethrows -> Result {
316316
_debugPrecondition(
317317
Int(bitPattern: .init(_rawValue)) & (MemoryLayout<T>.alignment-1) == 0 &&
318-
MemoryLayout<Pointee>.stride > MemoryLayout<T>.stride
319-
? MemoryLayout<Pointee>.stride % MemoryLayout<T>.stride == 0
320-
: MemoryLayout<T>.stride % MemoryLayout<Pointee>.stride == 0
318+
( count == 1 ||
319+
MemoryLayout<Pointee>.stride > MemoryLayout<T>.stride
320+
? MemoryLayout<Pointee>.stride % MemoryLayout<T>.stride == 0
321+
: MemoryLayout<T>.stride % MemoryLayout<Pointee>.stride == 0 )
321322
)
322323
let binding = Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
323324
defer { Builtin.rebindMemory(_rawValue, binding) }
@@ -1001,9 +1002,10 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
10011002
) rethrows -> Result {
10021003
_debugPrecondition(
10031004
Int(bitPattern: .init(_rawValue)) & (MemoryLayout<T>.alignment-1) == 0 &&
1004-
MemoryLayout<Pointee>.stride > MemoryLayout<T>.stride
1005-
? MemoryLayout<Pointee>.stride % MemoryLayout<T>.stride == 0
1006-
: MemoryLayout<T>.stride % MemoryLayout<Pointee>.stride == 0
1005+
( count == 1 ||
1006+
MemoryLayout<Pointee>.stride > MemoryLayout<T>.stride
1007+
? MemoryLayout<Pointee>.stride % MemoryLayout<T>.stride == 0
1008+
: MemoryLayout<T>.stride % MemoryLayout<Pointee>.stride == 0 )
10071009
)
10081010
let binding = Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
10091011
defer { Builtin.rebindMemory(_rawValue, binding) }

0 commit comments

Comments
 (0)