34
34
// struct A { struct B { let x: UnsafeMutableBufferPointer<...> } let b: B }
35
35
@_fixed_layout
36
36
public struct Unsafe${ Mutable} BufferPointer< Element> {
37
+
37
38
@usableFromInline
38
- let _position , _end : Unsafe ${ Mutable} Pointer < Element > ?
39
+ let _position : Unsafe ${ Mutable} Pointer< Element>?
40
+
41
+ /// The number of elements in the buffer.
42
+ ///
43
+ /// If the `baseAddress` of this buffer is `nil`, the count is zero. However,
44
+ /// a buffer can have a `count` of zero even with a non-`nil` base address.
45
+ public let count : Int
39
46
}
40
47
41
48
% if not mutable:
@@ -62,10 +69,15 @@ extension UnsafeBufferPointer.Iterator: IteratorProtocol {
62
69
/// Once `nil` has been returned, all subsequent calls return `nil`.
63
70
@inlinable
64
71
public mutating func next( ) -> Element ? {
65
- if _position == _end { return nil }
72
+ guard let start = _position else {
73
+ return nil
74
+ }
75
+ _sanityCheck ( _end != nil , " inconsistent _position, _end pointers " )
76
+
77
+ if start == _end. _unsafelyUnwrappedUnchecked { return nil }
66
78
67
- let result = _position! . pointee
68
- _position! += 1
79
+ let result = start . pointee
80
+ _position = start + 1
69
81
return result
70
82
}
71
83
}
@@ -81,7 +93,10 @@ extension Unsafe${Mutable}BufferPointer: Sequence {
81
93
/// - Returns: An iterator over the elements of this buffer.
82
94
@inlinable
83
95
public func makeIterator( ) -> Iterator {
84
- return Iterator ( _position: _position, _end: _end)
96
+ guard let start = _position else {
97
+ return Iterator ( _position: nil , _end: nil )
98
+ }
99
+ return Iterator ( _position: start, _end: start + count)
85
100
}
86
101
87
102
/// Initializes the memory at `destination.baseAddress` with elements of `self`,
@@ -98,7 +113,7 @@ extension Unsafe${Mutable}BufferPointer: Sequence {
98
113
let d = destination. baseAddress. _unsafelyUnwrappedUnchecked
99
114
let n = Swift . min ( destination. count, self . count)
100
115
d. initialize ( from: s, count: n)
101
- return ( Iterator ( _position: s + n, _end: _end ) , n)
116
+ return ( Iterator ( _position: s + n, _end: s + count ) , n)
102
117
}
103
118
}
104
119
@@ -214,19 +229,6 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
214
229
return startIndex..< endIndex
215
230
}
216
231
217
- /// The number of elements in the buffer.
218
- ///
219
- /// If the `baseAddress` of this buffer is `nil`, the count is zero. However,
220
- /// a buffer can have a `count` of zero even with a non-`nil` base address.
221
- @inlinable
222
- public var count : Int {
223
- guard let start = _position, let end = _end else {
224
- return 0
225
- }
226
-
227
- return end - start
228
- }
229
-
230
232
/// Accesses the element at the specified position.
231
233
///
232
234
% if Mutable:
@@ -385,21 +387,13 @@ extension Unsafe${Mutable}BufferPointer {
385
387
count == 0 || start != nil ,
386
388
" Unsafe${Mutable}BufferPointer has a nil start and nonzero count " )
387
389
_position = start
388
- _end = start. map { $0 + count }
389
- }
390
-
391
- /// Creates a buffer pointer starting at `start` and extending up to the last
392
- /// addressable pointer to `Element` in the memory space
393
- @inlinable
394
- public init( _unboundedStartingAt start: Unsafe${ Mutable} Pointer< Element> ) {
395
- _position = start
396
- _end = type ( of: start) . _max
390
+ self . count = count
397
391
}
398
392
399
393
@inlinable
400
394
public init( _empty: ( ) ) {
401
- _position = Unsafe $ { Mutable } Pointer . _max
402
- _end = _position
395
+ _position = nil
396
+ count = 0
403
397
}
404
398
405
399
% if Mutable:
@@ -411,7 +405,7 @@ extension Unsafe${Mutable}BufferPointer {
411
405
@inlinable
412
406
public init( mutating other: UnsafeBufferPointer< Element> ) {
413
407
_position = UnsafeMutablePointer < Element > ( mutating: other. _position)
414
- _end = UnsafeMutablePointer < Element > ( mutating : other. _end )
408
+ count = other. count
415
409
}
416
410
417
411
% else:
@@ -423,7 +417,7 @@ extension Unsafe${Mutable}BufferPointer {
423
417
@inlinable
424
418
public init( _ other: UnsafeMutableBufferPointer< Element> ) {
425
419
_position = UnsafePointer < Element > ( other. _position)
426
- _end = UnsafePointer < Element > ( other. _end )
420
+ count = other. count
427
421
}
428
422
429
423
% end
@@ -543,7 +537,7 @@ extension Unsafe${Mutable}BufferPointer {
543
537
return
544
538
}
545
539
546
- dstBase. initialize ( repeating: repeatedValue, count: _end! - dstBase )
540
+ dstBase. initialize ( repeating: repeatedValue, count: count )
547
541
}
548
542
549
543
/// Assigns every element in this buffer's memory to a copy of the given value.
@@ -563,7 +557,7 @@ extension Unsafe${Mutable}BufferPointer {
563
557
return
564
558
}
565
559
566
- dstBase. assign ( repeating: repeatedValue, count: _end! - dstBase )
560
+ dstBase. assign ( repeating: repeatedValue, count: count )
567
561
}
568
562
569
563
% end
0 commit comments