1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
extension AsyncIteratorProtocol where Element == UInt8 {
public mutating func nextInt<T: FixedWidthInteger>() async throws -> T? {
if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) {
try await nextInt(isolation: nil)
} else {
try await next(count: T.bitWidth / 8).map(T.init(bytes:))
}
}
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
public mutating func nextInt<T: FixedWidthInteger>(isolation actor: isolated (any Actor)?) async throws -> T? {
try await next(count: T.bitWidth / 8, isolation: actor).map(T.init(bytes:))
}
}
extension AsyncIteratorProtocol {
public mutating func next(count: Int) async throws -> [Element]? {
if #available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) {
return try await next(count: count, isolation: nil)
} else {
var bytes = [Element]()
bytes.reserveCapacity(count)
while bytes.count < count, let byte = try await self.next() {
bytes.append(byte)
}
switch bytes.count {
case count:
return bytes
case 0:
return nil
default:
throw EOFError()
}
}
}
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
public mutating func next(count: Int, isolation actor: isolated (any Actor)?) async throws -> [Element]? {
var bytes = [Element]()
bytes.reserveCapacity(count)
while bytes.count < count, let byte = try await self.next(isolation: actor) {
bytes.append(byte)
}
switch bytes.count {
case count:
return bytes
case 0:
return nil
default:
throw EOFError()
}
}
}
extension FixedWidthInteger {
public init(bytes: [UInt8]) {
self = bytes.withUnsafeBytes { $0.load(as: Self.self) }
}
public var bytes: [UInt8] {
withUnsafeBytes(of: self, Array.init)
}
}
fileprivate struct EOFError: Error {
}
|