Skip to content

Commit

Permalink
Using Array iterator under the hood
Browse files Browse the repository at this point in the history
  • Loading branch information
maschall committed Oct 20, 2024
1 parent ae25dca commit fbbccdf
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Sources/_CryptoExtras/AES/Nonces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ import Foundation

fileprivate struct ByteIterator<T>: IteratorProtocol {
var currentOffset = 0
var pointer: UnsafeRawBufferPointer? = nil
var iterator: Array<UInt8>.Iterator? = nil
let length: Int

init(_ bytes: T) {
self.length = Mirror(reflecting: bytes).children.count
withUnsafeBytes(of: bytes) { pointer in
self.pointer = pointer
self.iterator = Array(pointer).makeIterator()
}
}

@inlinable
public mutating func next() -> UInt8? {
guard let pointer,
guard var iterator,
currentOffset < length else { return nil }

let next = pointer.load(fromByteOffset: currentOffset, as: UInt8.self)
let next = iterator.next()
currentOffset += 1
return next
}
Expand Down

0 comments on commit fbbccdf

Please sign in to comment.