@@ -535,14 +535,14 @@ extension Unsafe${Mutable}RawBufferPointer {
535
535
/// be less than or equal to this buffer's `count`.
536
536
@inlinable
537
537
public func copyBytes( from source: some Collection < UInt8 > ) {
538
- _debugPrecondition ( source. count <= self . count,
539
- " ${Self}.copyBytes source has too many elements " )
540
538
guard let position = _position else {
541
539
return
542
540
}
543
541
544
542
if source. withContiguousStorageIfAvailable ( {
545
543
( buffer: UnsafeBufferPointer < UInt8 > ) -> Void in
544
+ _debugPrecondition ( source. count <= self . count,
545
+ " ${Self}.copyBytes source has too many elements " )
546
546
if let base = buffer. baseAddress {
547
547
position. copyMemory ( from: base, byteCount: buffer. count)
548
548
}
@@ -551,6 +551,8 @@ extension Unsafe${Mutable}RawBufferPointer {
551
551
}
552
552
553
553
for (index, byteValue) in source. enumerated ( ) {
554
+ _debugPrecondition ( index < self . count,
555
+ " ${Self}.copyBytes source has too many elements " )
554
556
position. storeBytes (
555
557
of: byteValue, toByteOffset: index, as: UInt8 . self)
556
558
}
@@ -711,14 +713,14 @@ extension Unsafe${Mutable}RawBufferPointer {
711
713
public func initializeMemory< T> ( as type: T . Type , repeating repeatedValue: T )
712
714
-> UnsafeMutableBufferPointer < T > {
713
715
guard let base = _position else {
714
- return UnsafeMutableBufferPointer < T > ( start: nil , count: 0 )
716
+ return . init ( start: nil , count: 0 )
715
717
}
716
718
717
719
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)
722
724
}
723
725
724
726
/// Initializes the buffer's memory with the given elements, binding the
@@ -749,8 +751,6 @@ extension Unsafe${Mutable}RawBufferPointer {
749
751
public func initializeMemory< S: Sequence > (
750
752
as type: S . Element . Type , from source: S
751
753
) -> ( unwritten: S . Iterator , initialized: UnsafeMutableBufferPointer < S . Element > ) {
752
- // TODO: Optimize where `C` is a `ContiguousArrayBuffer`.
753
-
754
754
var it = source. makeIterator ( )
755
755
var idx = startIndex
756
756
let elementStride = MemoryLayout< S . Element> . stride
@@ -907,14 +907,14 @@ extension Unsafe${Mutable}RawBufferPointer {
907
907
guard let sourceAddress = source. baseAddress, !source. isEmpty else {
908
908
return . init( start: nil , count: 0 )
909
909
}
910
- _precondition (
911
- source. count * MemoryLayout < T > . stride <= self . count,
912
- " buffer cannot contain every element from source. "
913
- )
914
910
_debugPrecondition (
915
911
Int ( bitPattern: baseAddress) % MemoryLayout< T> . stride == 0 ,
916
912
" buffer base address must be properly aligned to access T "
917
913
)
914
+ _precondition (
915
+ source. count * MemoryLayout < T > . stride <= self . count,
916
+ " buffer cannot contain every element from source. "
917
+ )
918
918
let initialized = baseAddress? . moveInitializeMemory (
919
919
as: T . self, from: sourceAddress, count: source. count
920
920
)
0 commit comments