mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +00:00
FactoryIterator → Producer, ConsumingIterator → Drain
This commit is contained in:
8 files changed
+77
-67
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<Element>: Producer, ~Copyable, ~Escapable {
|
||||
@_lifetime(&self)
|
||||
mutating func nextSpan(maximumCount: Int?) -> InputSpan<Element>
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension Drain where Self: ~Copyable & ~Escapable {
|
||||
@_lifetime(target: copy target)
|
||||
mutating func drain(filling target: inout OutputSpan<Element>) {
|
||||
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<Element>) {
|
||||
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
|
||||
@@ -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<Element>: ~Copyable, ~Escapable {
|
||||
associatedtype Element: ~Copyable /* ~Escapable */
|
||||
|
||||
@_lifetime(target: copy target)
|
||||
mutating func generate(into target: inout OutputSpan<Element>)
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
public protocol ConsumingIterator<Element>: FactoryIterator, ~Copyable, ~Escapable {
|
||||
@_lifetime(&self)
|
||||
mutating func nextSpan(maximumCount: Int?) -> InputSpan<Element>
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension ConsumingIterator where Self: ~Copyable & ~Escapable {
|
||||
@_lifetime(target: copy target)
|
||||
mutating func generate(into target: inout OutputSpan<Element>) {
|
||||
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<Element>) {
|
||||
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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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<Element>: ~Copyable, ~Escapable {
|
||||
associatedtype Element: ~Copyable /* ~Escapable */
|
||||
|
||||
@_lifetime(target: copy target)
|
||||
mutating func generate(into target: inout OutputSpan<Element>)
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user