Skip to content

Commit f527e4b

Browse files
committed
adding a new Iterator
1 parent 773f208 commit f527e4b

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

Sources/_CryptoExtras/AES/Nonces.swift

+30-12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ import Foundation
1818
// any edits of this file WILL be overwritten and thus discarded
1919
// see section `gyb` in `README` for details.
2020

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+
2145
// MARK: - AES._CBC + IV
2246
extension AES._CBC {
2347
/// A value used once during a cryptographic operation and then discarded.
@@ -109,10 +133,8 @@ extension AES._CBC {
109133
}
110134

111135
/// 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)
116138
}
117139
}
118140
}
@@ -202,10 +224,8 @@ extension AES._CFB {
202224
}
203225

204226
/// 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)
209229
}
210230
}
211231
}
@@ -295,10 +315,8 @@ extension AES._CTR {
295315
}
296316

297317
/// 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)
302320
}
303321
}
304322
}

Sources/_CryptoExtras/AES/Nonces.swift.gyb

+26-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,30 @@ import Foundation
1717
// MARK: - Generated file, do NOT edit
1818
// any edits of this file WILL be overwritten and thus discarded
1919
// see section `gyb` in `README` for details.
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+
2044
%{
2145
ciphers = [
2246
{"name": "AES._CBC", "nonceName": "IV", "tupleDefinition": """(
@@ -123,10 +147,8 @@ extension ${name} {
123147
}
124148

125149
/// Returns an iterator over the elements of the nonce.
126-
public func makeIterator() -> Array<UInt8>.Iterator {
127-
self.withUnsafeBytes({ (buffPtr) in
128-
return Array(buffPtr).makeIterator()
129-
})
150+
public func makeIterator() -> some IteratorProtocol<UInt8> {
151+
ByteIterator(bytes)
130152
}
131153
}
132154
}

0 commit comments

Comments
 (0)