diff --git a/Sources/ContainersPreview/CMakeLists.txt b/Sources/ContainersPreview/CMakeLists.txt index 7d85b8d3..35317ad1 100644 --- a/Sources/ContainersPreview/CMakeLists.txt +++ b/Sources/ContainersPreview/CMakeLists.txt @@ -69,6 +69,7 @@ target_sources(${module_name} PRIVATE "Protocols/Producer+Filter.swift" "Protocols/Producer+Map.swift" "Protocols/Producer+Reduce.swift" + "Protocols/Producer+Unfold.swift" "Types/InputSpan.swift" "Types/MutableRef.swift" "Types/OutputSpan+Helpers.swift" diff --git a/Sources/ContainersPreview/Protocols/Producer+Unfold.swift b/Sources/ContainersPreview/Protocols/Producer+Unfold.swift new file mode 100644 index 00000000..d4654537 --- /dev/null +++ b/Sources/ContainersPreview/Protocols/Producer+Unfold.swift @@ -0,0 +1,86 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +#endif + +#if compiler(>=6.4) && UnstableContainersPreview + +@_lifetime(copy state) +public func producer< + State: ~Copyable & ~Escapable, + Failure: Error, + Element: ~Copyable +>( + state: consuming State, + next: @escaping (inout State) throws(Failure) -> Element? +) -> UnfoldProducer { + .init(state: state, next: next) +} + +@frozen +public struct UnfoldProducer< + State: ~Copyable & ~Escapable, + Failure: Error, + Element: ~Copyable +>: ~Copyable, ~Escapable { + @usableFromInline + internal let _next: (inout State) throws(Failure) -> Element? + + @usableFromInline + internal var _state: State + + @inlinable + @_lifetime(copy state) + public init( + state: consuming State, + next: @escaping (inout State) throws(Failure) -> Element? + ) { + self._next = next + self._state = state + } +} + +extension UnfoldProducer: Producer +where + State: ~Copyable & ~Escapable, + Element: ~Copyable +{ + @inlinable + public var underestimatedCount: Int { 0 } + + @inlinable + @discardableResult + @_lifetime(target: copy target) + @_lifetime(self: copy self) + public mutating func generate( + into target: inout OutputSpan + ) throws(Failure) -> Bool { + var c = 0 + while !target.isFull { + guard let next = try _next(&self._state) else { break } + target.append(next) + c += 1 + } + return c > 0 + } + + @inlinable + @_lifetime(self: copy self) + public mutating func next() throws(Failure) -> Element? { + try _next(&_state) + } +} + +#endif diff --git a/Xcode/Collections.xcodeproj/project.pbxproj b/Xcode/Collections.xcodeproj/project.pbxproj index d4a8b58a..6712ff1a 100644 --- a/Xcode/Collections.xcodeproj/project.pbxproj +++ b/Xcode/Collections.xcodeproj/project.pbxproj @@ -65,6 +65,7 @@ 7D2A779C2F27F2BF005780C7 /* Deque+ExpressibleByArrayLiteral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A775C2F27F2BF005780C7 /* Deque+ExpressibleByArrayLiteral.swift */; }; 7D2A77A02F27F367005780C7 /* RefTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2A779D2F27F367005780C7 /* RefTests.swift */; }; 7D2F598E2FAC229B00D43FE7 /* MutableRefTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D2F598D2FAC229B00D43FE7 /* MutableRefTests.swift */; }; + 7D3F1F7B3007177100AD924D /* Producer+Unfold.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D3F1F7A3007176C00AD924D /* Producer+Unfold.swift */; }; 7D479CB02F450E4600A8CB59 /* RigidSet+BorrowingSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CAF2F450E3F00A8CB59 /* RigidSet+BorrowingSequence.swift */; }; 7D479CB32F45296A00A8CB59 /* UniqueSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CB12F45296000A8CB59 /* UniqueSet.swift */; }; 7D479CB52F452D6900A8CB59 /* UniqueSetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CB42F452D6700A8CB59 /* UniqueSetTests.swift */; }; @@ -691,6 +692,7 @@ 7D2A77762F27F2BF005780C7 /* UniqueDeque+Replacements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDeque+Replacements.swift"; sourceTree = ""; }; 7D2A779D2F27F367005780C7 /* RefTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefTests.swift; sourceTree = ""; }; 7D2F598D2FAC229B00D43FE7 /* MutableRefTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MutableRefTests.swift; sourceTree = ""; }; + 7D3F1F7A3007176C00AD924D /* Producer+Unfold.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Producer+Unfold.swift"; sourceTree = ""; }; 7D479CAF2F450E3F00A8CB59 /* RigidSet+BorrowingSequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidSet+BorrowingSequence.swift"; sourceTree = ""; }; 7D479CB12F45296000A8CB59 /* UniqueSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniqueSet.swift; sourceTree = ""; }; 7D479CB42F452D6700A8CB59 /* UniqueSetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniqueSetTests.swift; sourceTree = ""; }; @@ -1364,6 +1366,7 @@ 7D57EDE62F64913C0029D5E3 /* Producer+Filter.swift */, 7D57EDD12F647DB10029D5E3 /* Producer+Map.swift */, 7D57EDD22F647DB10029D5E3 /* Producer+Reduce.swift */, + 7D3F1F7A3007176C00AD924D /* Producer+Unfold.swift */, ); path = Protocols; sourceTree = ""; @@ -2825,6 +2828,7 @@ 7D479CF32F47FB4100A8CB59 /* _HTable+Removal.swift in Sources */, 7DE9203529CA70F3004483EB /* OrderedSet+CustomReflectable.swift in Sources */, 7DEB31582F4E762200F9AF28 /* RigidDictionary+Equatable.swift in Sources */, + 7D3F1F7B3007177100AD924D /* Producer+Unfold.swift in Sources */, 7DE920C929CA70F4004483EB /* BitSet+BidirectionalCollection.swift in Sources */, 7DE920ED29CA70F4004483EB /* BitArray+Extras.swift in Sources */, 7DE9206829CA70F4004483EB /* BigString+Chunk+Append and Insert.swift in Sources */,