From 33b50fdb766ffc84481648d2f71ce5cb0c5bd54d Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Mon, 12 Jan 2026 14:10:25 -0800 Subject: [PATCH] =?UTF-8?q?FactoryIterator=20=E2=86=92=20Producer,=20Consu?= =?UTF-8?q?mingIterator=20=E2=86=92=20Drain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BorrowIteratorProtocol.swift | 2 +- Sources/ContainersPreview/Container.swift | 2 +- Sources/ContainersPreview/Drain.swift | 50 +++++++++++++++ .../ContainersPreview/FactoryIterator.swift | 62 ------------------- Sources/ContainersPreview/InputSpan.swift | 2 +- Sources/ContainersPreview/Iterable.swift | 2 +- Sources/ContainersPreview/Mut.swift | 2 +- Sources/ContainersPreview/Producer.swift | 22 +++++++ 8 files changed, 77 insertions(+), 67 deletions(-) create mode 100644 Sources/ContainersPreview/Drain.swift delete mode 100644 Sources/ContainersPreview/FactoryIterator.swift create mode 100644 Sources/ContainersPreview/Producer.swift diff --git a/Sources/ContainersPreview/BorrowIteratorProtocol.swift b/Sources/ContainersPreview/BorrowIteratorProtocol.swift index b13a0147..26ed63e2 100644 --- a/Sources/ContainersPreview/BorrowIteratorProtocol.swift +++ b/Sources/ContainersPreview/BorrowIteratorProtocol.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift Collections open source project // -// Copyright (c) 2024 - 2025 Apple Inc. and the Swift project authors +// 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 diff --git a/Sources/ContainersPreview/Container.swift b/Sources/ContainersPreview/Container.swift index 3856fe76..495da94f 100644 --- a/Sources/ContainersPreview/Container.swift +++ b/Sources/ContainersPreview/Container.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift Collections open source project // -// Copyright (c) 2024 - 2025 Apple Inc. and the Swift project authors +// 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 diff --git a/Sources/ContainersPreview/Drain.swift b/Sources/ContainersPreview/Drain.swift new file mode 100644 index 00000000..e3bb4c6d --- /dev/null +++ b/Sources/ContainersPreview/Drain.swift @@ -0,0 +1,50 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +#endif + +@available(SwiftStdlib 5.0, *) +public protocol Drain: Producer, ~Copyable, ~Escapable { + @_lifetime(&self) + mutating func nextSpan(maximumCount: Int?) -> InputSpan +} + +@available(SwiftStdlib 5.0, *) +extension Drain where Self: ~Copyable & ~Escapable { + @_lifetime(target: copy target) + mutating func drain(filling target: inout OutputSpan) { + while target.freeCapacity > 0 { + var source = self.nextSpan(maximumCount: target.freeCapacity) + target._append(moving: &source) + } + } +} + +@available(SwiftStdlib 5.0, *) +extension OutputSpan where Element: ~Copyable { + @_lifetime(source: copy source) + mutating func _append(moving source: inout InputSpan) { + self.withUnsafeMutableBufferPointer { dst, dstCount in + source.withUnsafeMutableBufferPointer { src, srcCount in + precondition(srcCount <= dstCount, "OutputSpan capacity overflow") + dst + .extracting(dstCount ..< dstCount + srcCount) + .moveInitializeAll(fromContentsOf: src._extracting(last: srcCount)) + } + } + } +} + +#endif diff --git a/Sources/ContainersPreview/FactoryIterator.swift b/Sources/ContainersPreview/FactoryIterator.swift deleted file mode 100644 index beedad56..00000000 --- a/Sources/ContainersPreview/FactoryIterator.swift +++ /dev/null @@ -1,62 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift Collections open source project -// -// Copyright (c) 2024 - 2025 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 -// -//===----------------------------------------------------------------------===// - -#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW - -#if !COLLECTIONS_SINGLE_MODULE -import InternalCollectionsUtilities -#endif - -@available(SwiftStdlib 5.0, *) -public protocol FactoryIterator: ~Copyable, ~Escapable { - associatedtype Element: ~Copyable /* ~Escapable */ - - @_lifetime(target: copy target) - mutating func generate(into target: inout OutputSpan) -} - -//------------------------------------------------------------------------------ - -@available(SwiftStdlib 5.0, *) -public protocol ConsumingIterator: FactoryIterator, ~Copyable, ~Escapable { - @_lifetime(&self) - mutating func nextSpan(maximumCount: Int?) -> InputSpan -} - -@available(SwiftStdlib 5.0, *) -extension ConsumingIterator where Self: ~Copyable & ~Escapable { - @_lifetime(target: copy target) - mutating func generate(into target: inout OutputSpan) { - var remainder = target.count - while true { - var source = self.nextSpan(maximumCount: target.count) - remainder -= source.count - target._append(moving: &source) - } - } -} - -@available(SwiftStdlib 5.0, *) -extension OutputSpan where Element: ~Copyable { - @_lifetime(source: copy source) - mutating func _append(moving source: inout InputSpan) { - self.withUnsafeMutableBufferPointer { target, targetCount in - source.withUnsafeMutableBufferPointer { source, sourceCount in - precondition(sourceCount <= targetCount, "OutputSpan capacity overflow") - target - .extracting(targetCount ..< targetCount + sourceCount) - .moveInitializeAll(fromContentsOf: source._extracting(last: sourceCount)) - } - } - } -} - -#endif diff --git a/Sources/ContainersPreview/InputSpan.swift b/Sources/ContainersPreview/InputSpan.swift index 7560041c..e705015f 100644 --- a/Sources/ContainersPreview/InputSpan.swift +++ b/Sources/ContainersPreview/InputSpan.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift Collections open source project // -// Copyright (c) 2024 - 2025 Apple Inc. and the Swift project authors +// 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 diff --git a/Sources/ContainersPreview/Iterable.swift b/Sources/ContainersPreview/Iterable.swift index d6034139..e7306cc7 100644 --- a/Sources/ContainersPreview/Iterable.swift +++ b/Sources/ContainersPreview/Iterable.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift Collections open source project // -// Copyright (c) 2024 - 2025 Apple Inc. and the Swift project authors +// 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 diff --git a/Sources/ContainersPreview/Mut.swift b/Sources/ContainersPreview/Mut.swift index 7451d8ab..310163c1 100644 --- a/Sources/ContainersPreview/Mut.swift +++ b/Sources/ContainersPreview/Mut.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -#if compiler(>=6.2) +#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW import Builtin /// A safe mutable reference allowing in-place mutation to an exclusive value. diff --git a/Sources/ContainersPreview/Producer.swift b/Sources/ContainersPreview/Producer.swift new file mode 100644 index 00000000..c63c9ff9 --- /dev/null +++ b/Sources/ContainersPreview/Producer.swift @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + +@available(SwiftStdlib 5.0, *) +public protocol Producer: ~Copyable, ~Escapable { + associatedtype Element: ~Copyable /* ~Escapable */ + + @_lifetime(target: copy target) + mutating func generate(into target: inout OutputSpan) +} + +#endif