@@ -18,6 +18,30 @@ import Foundation
18
18
// any edits of this file WILL be overwritten and thus discarded
19
19
// see section `gyb` in `README` for details.
20
20
21
+ fileprivate struct ByteIterator < T> : IteratorProtocol {
22
+ var currentOffset = 0
23
+ var pointer : UnsafeRawBufferPointer ? = nil
24
+ let length : Int
25
+
26
+ init ( _ bytes: T ) {
27
+ self . length = Mirror ( reflecting: bytes) . children. count
28
+ withUnsafeBytes ( of: bytes) { pointer in
29
+ self . pointer = pointer
30
+ }
31
+ }
32
+
33
+ @inlinable
34
+ public mutating func next( ) -> UInt8 ? {
35
+ guard let pointer,
36
+ currentOffset < length else { return nil }
37
+
38
+ let next = pointer. load ( fromByteOffset: currentOffset, as: UInt8 . self)
39
+ currentOffset += 1
40
+ return next
41
+ }
42
+ }
43
+
44
+
21
45
// MARK: - AES._CBC + IV
22
46
extension AES . _CBC {
23
47
/// A value used once during a cryptographic operation and then discarded.
@@ -109,10 +133,8 @@ extension AES._CBC {
109
133
}
110
134
111
135
/// Returns an iterator over the elements of the nonce.
112
- public func makeIterator( ) -> Array < UInt8 > . Iterator {
113
- self . withUnsafeBytes ( { ( buffPtr) in
114
- return Array ( buffPtr) . makeIterator ( )
115
- } )
136
+ public func makeIterator( ) -> some IteratorProtocol < UInt8 > {
137
+ ByteIterator ( bytes)
116
138
}
117
139
}
118
140
}
@@ -202,10 +224,8 @@ extension AES._CFB {
202
224
}
203
225
204
226
/// Returns an iterator over the elements of the nonce.
205
- public func makeIterator( ) -> Array < UInt8 > . Iterator {
206
- self . withUnsafeBytes ( { ( buffPtr) in
207
- return Array ( buffPtr) . makeIterator ( )
208
- } )
227
+ public func makeIterator( ) -> some IteratorProtocol < UInt8 > {
228
+ ByteIterator ( bytes)
209
229
}
210
230
}
211
231
}
@@ -295,10 +315,8 @@ extension AES._CTR {
295
315
}
296
316
297
317
/// Returns an iterator over the elements of the nonce.
298
- public func makeIterator( ) -> Array < UInt8 > . Iterator {
299
- self . withUnsafeBytes ( { ( buffPtr) in
300
- return Array ( buffPtr) . makeIterator ( )
301
- } )
318
+ public func makeIterator( ) -> some IteratorProtocol < UInt8 > {
319
+ ByteIterator ( bytes)
302
320
}
303
321
}
304
322
}
0 commit comments