Skip to content

Commit 88225a2

Browse files
committed
[se-0370] some consistency tweaks
1 parent 6512840 commit 88225a2

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -535,14 +535,14 @@ extension Unsafe${Mutable}RawBufferPointer {
535535
/// be less than or equal to this buffer's `count`.
536536
@inlinable
537537
public func copyBytes(from source: some Collection<UInt8>) {
538-
_debugPrecondition(source.count <= self.count,
539-
"${Self}.copyBytes source has too many elements")
540538
guard let position = _position else {
541539
return
542540
}
543541

544542
if source.withContiguousStorageIfAvailable({
545543
(buffer: UnsafeBufferPointer<UInt8>) -> Void in
544+
_debugPrecondition(source.count <= self.count,
545+
"${Self}.copyBytes source has too many elements")
546546
if let base = buffer.baseAddress {
547547
position.copyMemory(from: base, byteCount: buffer.count)
548548
}
@@ -551,6 +551,8 @@ extension Unsafe${Mutable}RawBufferPointer {
551551
}
552552

553553
for (index, byteValue) in source.enumerated() {
554+
_debugPrecondition(index < self.count,
555+
"${Self}.copyBytes source has too many elements")
554556
position.storeBytes(
555557
of: byteValue, toByteOffset: index, as: UInt8.self)
556558
}
@@ -711,14 +713,14 @@ extension Unsafe${Mutable}RawBufferPointer {
711713
public func initializeMemory<T>(as type: T.Type, repeating repeatedValue: T)
712714
-> UnsafeMutableBufferPointer<T> {
713715
guard let base = _position else {
714-
return UnsafeMutableBufferPointer<T>(start: nil, count: 0)
716+
return .init(start: nil, count: 0)
715717
}
716718

717719
let count = (_end._unsafelyUnwrappedUnchecked-base) / MemoryLayout<T>.stride
718-
let typed = base.initializeMemory(as: type,
719-
repeating: repeatedValue,
720-
count: count)
721-
return UnsafeMutableBufferPointer<T>(start: typed, count: count)
720+
let initialized = base.initializeMemory(
721+
as: type, repeating: repeatedValue, count: count
722+
)
723+
return .init(start: initialized, count: count)
722724
}
723725

724726
/// Initializes the buffer's memory with the given elements, binding the
@@ -749,8 +751,6 @@ extension Unsafe${Mutable}RawBufferPointer {
749751
public func initializeMemory<S: Sequence>(
750752
as type: S.Element.Type, from source: S
751753
) -> (unwritten: S.Iterator, initialized: UnsafeMutableBufferPointer<S.Element>) {
752-
// TODO: Optimize where `C` is a `ContiguousArrayBuffer`.
753-
754754
var it = source.makeIterator()
755755
var idx = startIndex
756756
let elementStride = MemoryLayout<S.Element>.stride
@@ -907,14 +907,14 @@ extension Unsafe${Mutable}RawBufferPointer {
907907
guard let sourceAddress = source.baseAddress, !source.isEmpty else {
908908
return .init(start: nil, count: 0)
909909
}
910-
_precondition(
911-
source.count * MemoryLayout<T>.stride <= self.count,
912-
"buffer cannot contain every element from source."
913-
)
914910
_debugPrecondition(
915911
Int(bitPattern: baseAddress) % MemoryLayout<T>.stride == 0,
916912
"buffer base address must be properly aligned to access T"
917913
)
914+
_precondition(
915+
source.count * MemoryLayout<T>.stride <= self.count,
916+
"buffer cannot contain every element from source."
917+
)
918918
let initialized = baseAddress?.moveInitializeMemory(
919919
as: T.self, from: sourceAddress, count: source.count
920920
)

0 commit comments

Comments
 (0)