diff --git a/Package.swift b/Package.swift index cb425802..10a2ab68 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ import PackageDescription let _traits: Set = [ .default( enabledTraits: [ -// "UnstableContainersPreview", + "UnstableContainersPreview", // "UnstableHashedContainers", // "UnstableSortedCollections", ]), diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift index 23e8e506..ca77fa15 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift @@ -349,12 +349,12 @@ extension RigidArray { #if compiler(>=6.4) && UnstableContainersPreview @inlinable internal mutating func _append< - Source: Iterable_ & ~Copyable & ~Escapable + Source: Iterable_ & ~Copyable & ~Escapable, E >( copying newElements: borrowing Source - ) { + ) throws(E) { let target = _freeSpace - _count += newElements._copyContents(intoPrefixOf: target) + _count += try newElements._copyContents(intoPrefixOf: target) } #endif @@ -371,11 +371,11 @@ extension RigidArray { @_alwaysEmitIntoClient @inline(__always) public mutating func append< - Source: Iterable_ & ~Copyable & ~Escapable + Source: Iterable_ & ~Copyable & ~Escapable, E >( copying newElements: borrowing Source - ) { - _append(copying: newElements) + ) throws(E) { + try _append(copying: newElements) } #endif @@ -413,7 +413,7 @@ extension RigidArray { @_alwaysEmitIntoClient @inline(__always) public mutating func append< - Source: Iterable_ & Sequence + Source: Iterable_ & Sequence >(copying newElements: Source) { _append(copying: newElements) } diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift b/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift index 39666940..0011174b 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift @@ -124,12 +124,12 @@ extension RigidArray /*where Element: Copyable*/ { /// The container must not contain more than `capacity` elements. @_alwaysEmitIntoClient @inline(__always) - public init & ~Copyable & ~Escapable>( + public init & ~Copyable & ~Escapable, E>( capacity: Int, copying contents: borrowing Source - ) { + ) throws(E) { self.init(capacity: capacity) - self.append(copying: contents) + try self.append(copying: contents) } #endif @@ -145,7 +145,7 @@ extension RigidArray /*where Element: Copyable*/ { /// The container must not contain more than `capacity` elements. @_alwaysEmitIntoClient @inline(__always) - public init & Sequence>( + public init & Sequence>( capacity: Int, copying contents: Source ) { diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift b/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift index 82b7da61..d81d8c4d 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift @@ -414,7 +414,7 @@ extension RigidArray { at index: Int, copying items: borrowing C, newCount: Int - ) { + ) where C.Failure == Never { var it = items.makeIterableIterator_() insert(addingCount: newCount, at: index) { target in while !target.isFull { @@ -479,7 +479,7 @@ extension RigidArray { C: Container & ~Copyable & ~Escapable >( copying newElements: borrowing C, at index: Int - ) { + ) where C.Failure == Never { _insertContainer( at: index, copying: newElements, newCount: newElements.count) } @@ -539,7 +539,7 @@ extension RigidArray { C: Container & Collection >( copying newElements: borrowing C, at index: Int - ) { + ) where C.Failure == Never { _insertContainer( at: index, copying: newElements, newCount: newElements.count) } diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift b/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift index 0d332c7a..c3bfd183 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift @@ -590,7 +590,7 @@ extension RigidArray { removing subrange: Range, copyingContainer items: borrowing C, newCount: Int - ) { + ) where C.Failure == Never { var it = items.makeIterableIterator_() self.replace(removing: subrange, addingCount: newCount) { target in while !target.isFull { @@ -668,7 +668,7 @@ extension RigidArray { >( removing subrange: Range, copying newElements: borrowing C - ) { + ) where C.Failure == Never { _replace( removing: subrange, copyingContainer: newElements, @@ -749,7 +749,7 @@ extension RigidArray { >( removing subrange: Range, copying newElements: C - ) { + ) where C.Failure == Never { _replace( removing: subrange, copyingContainer: newElements, diff --git a/Sources/BasicContainers/RigidSet/RigidSet+Initializers.swift b/Sources/BasicContainers/RigidSet/RigidSet+Initializers.swift index d3e5a4a9..b594b01e 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet+Initializers.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet+Initializers.swift @@ -119,7 +119,7 @@ extension RigidSet /* where Element: Copyable */ { @_alwaysEmitIntoClient @inline(__always) public init< - S: Iterable_ & Sequence + S: Iterable_ & Sequence >( capacity: Int, copying contents: borrowing S @@ -131,7 +131,7 @@ extension RigidSet /* where Element: Copyable */ { @_alwaysEmitIntoClient @inline(__always) public init< - S: Iterable_ & Collection + S: Iterable_ & Collection >( capacity: Int? = nil, copying contents: S diff --git a/Sources/BasicContainers/RigidSet/RigidSet+Insertions.swift b/Sources/BasicContainers/RigidSet/RigidSet+Insertions.swift index 99d5cb2b..b77317f8 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet+Insertions.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet+Insertions.swift @@ -261,7 +261,7 @@ extension RigidSet /* where Element: Copyable */ { @_alwaysEmitIntoClient @inline(__always) public mutating func insert< - S: Iterable_ & Sequence + S: Iterable_ & Sequence >( copying items: borrowing S ) { diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift index 7a2f99bc..16795e81 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift @@ -310,14 +310,14 @@ extension UniqueArray { #if compiler(>=6.4) && UnstableContainersPreview @inlinable internal mutating func _append< - Source: Iterable_ & ~Copyable & ~Escapable + Source: Iterable_ & ~Copyable & ~Escapable, E >( copying newElements: borrowing Source - ) { + ) throws(E) { _ensureFreeCapacity(newElements.underestimatedCount_) var it = newElements.makeIterableIterator_() while true { - let span = it.nextSpan_() + let span = try it.nextSpan_() if span.isEmpty { break } _ensureFreeCapacity(span.count) _storage.append(copying: span) @@ -343,11 +343,11 @@ extension UniqueArray { @_alwaysEmitIntoClient @inline(__always) public mutating func append< - Source: Iterable_ & ~Copyable & ~Escapable + Source: Iterable_ & ~Copyable & ~Escapable, E >( copying newElements: borrowing Source - ) { - self._append(copying: newElements) + ) throws(E) { + try self._append(copying: newElements) } #endif @@ -397,7 +397,7 @@ extension UniqueArray { @_alwaysEmitIntoClient @inline(__always) public mutating func append< - Source: Iterable_ & Sequence + Source: Iterable_ & Sequence >(copying newElements: Source) { self._append(copying: newElements) } diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift index 26faa3a9..20ad1c9f 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift @@ -96,12 +96,12 @@ extension UniqueArray /*where Element: Copyable*/ { /// The sequence must not contain more than `capacity` elements. @_alwaysEmitIntoClient @inline(__always) - public init & ~Copyable & ~Escapable>( + public init & ~Copyable & ~Escapable, E>( capacity: Int? = nil, copying contents: borrowing Source - ) { + ) throws(E) { self.init(minimumCapacity: capacity ?? 0) - self.append(copying: contents) + try self.append(copying: contents) } #endif @@ -132,7 +132,7 @@ extension UniqueArray /*where Element: Copyable*/ { /// - contents: The container whose contents to copy into the new array. @_alwaysEmitIntoClient @inline(__always) - public init & Sequence>( + public init & Sequence>( capacity: Int? = nil, copying contents: Source ) { diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift index 83d19688..55c28179 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift @@ -396,7 +396,7 @@ extension UniqueArray { C: Container & ~Copyable & ~Escapable >( copying newElements: borrowing C, at index: Int - ) { + ) where C.Failure == Never { // FIXME: Avoid moving the subsequent elements twice. let c = newElements.count _ensureFreeCapacity(c) @@ -463,7 +463,7 @@ extension UniqueArray { C: Container & Collection >( copying newElements: borrowing C, at index: Int - ) { + ) where C.Failure == Never { // FIXME: Avoid moving the subsequent elements twice. let c = newElements.count _ensureFreeCapacity(c) diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift index 2b6c91b5..c12e5306 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift @@ -558,7 +558,7 @@ extension UniqueArray { >( removing subrange: Range, copying newElements: borrowing C - ) { + ) where C.Failure == Never { // FIXME: Avoid moving the subsequent elements twice. let c = newElements.count _ensureFreeCapacity(c - subrange.count) @@ -648,7 +648,7 @@ extension UniqueArray { >( removing subrange: Range, copying newElements: borrowing C - ) { + ) where C.Failure == Never { // FIXME: Avoid moving the subsequent elements twice. let c = newElements.count _ensureFreeCapacity(c - subrange.count) diff --git a/Sources/BasicContainers/UniqueSet/UniqueSet+Initializers.swift b/Sources/BasicContainers/UniqueSet/UniqueSet+Initializers.swift index 2b2639de..99122caa 100644 --- a/Sources/BasicContainers/UniqueSet/UniqueSet+Initializers.swift +++ b/Sources/BasicContainers/UniqueSet/UniqueSet+Initializers.swift @@ -95,7 +95,7 @@ extension UniqueSet where Element: Copyable { #if UnstableContainersPreview @_alwaysEmitIntoClient public init< - S: Iterable_ & Sequence + S: Iterable_ & Sequence >( copying items: borrowing S ) { diff --git a/Sources/BasicContainers/UniqueSet/UniqueSet+Insertions.swift b/Sources/BasicContainers/UniqueSet/UniqueSet+Insertions.swift index 8e1fbf13..64a4e2d9 100644 --- a/Sources/BasicContainers/UniqueSet/UniqueSet+Insertions.swift +++ b/Sources/BasicContainers/UniqueSet/UniqueSet+Insertions.swift @@ -172,7 +172,7 @@ extension UniqueSet /* where Element: Copyable */ { @_alwaysEmitIntoClient @inline(__always) public mutating func insert< - S: Iterable_ & Sequence + S: Iterable_ & Sequence >( copying items: borrowing S ) { diff --git a/Sources/ContainersPreview/Protocols/Container/Container.swift b/Sources/ContainersPreview/Protocols/Container/Container.swift index 0ec3829a..59ad3e2c 100644 --- a/Sources/ContainersPreview/Protocols/Container/Container.swift +++ b/Sources/ContainersPreview/Protocols/Container/Container.swift @@ -16,7 +16,7 @@ @available(SwiftStdlib 5.0, *) public protocol Container: Iterable_, ~Copyable, ~Escapable - where Element: ~Copyable, Element == Element_ + where Element: ~Copyable, Element == Element_ //, Failure == Never { associatedtype Element: ~Copyable associatedtype Index: Equatable diff --git a/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift b/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift index 1dc54bbd..53a3792d 100644 --- a/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift +++ b/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift @@ -427,13 +427,13 @@ where @inlinable public mutating func append< - S: Iterable_ & ~Copyable & ~Escapable + S: Iterable_ & ~Copyable & ~Escapable >( copying items: borrowing S - ) { + ) throws(Failure) { var it = items.makeIterableIterator_() while true { - let span = it.nextSpan_() + let span = try it.nextSpan_() guard !span.isEmpty else { break } self.append(copying: span) } diff --git a/Sources/ContainersPreview/Protocols/Iterable+Algorithms.swift b/Sources/ContainersPreview/Protocols/Iterable+Algorithms.swift index 1e407ac8..51c47b74 100644 --- a/Sources/ContainersPreview/Protocols/Iterable+Algorithms.swift +++ b/Sources/ContainersPreview/Protocols/Iterable+Algorithms.swift @@ -21,8 +21,8 @@ extension Iterable_ public func reduce( _ initialResult: consuming Result, _ nextPartialResult: - (_ partialResult: consuming Result, borrowing Element_) throws -> Result - ) rethrows -> Result { + (_ partialResult: consuming Result, borrowing Element_) throws(Failure) -> Result + ) throws(Failure) -> Result { try makeIterableIterator_().reduce(initialResult, nextPartialResult) } @@ -30,8 +30,8 @@ extension Iterable_ public func reduce( into initialResult: consuming Result, _ updateAccumulatingResult: - (_ partialResult: inout Result, borrowing Element_) throws -> () - ) rethrows -> Result { + (_ partialResult: inout Result, borrowing Element_) throws(Failure) -> () + ) throws(Failure) -> Result { try makeIterableIterator_().reduce(into: initialResult, updateAccumulatingResult) } } @@ -42,8 +42,8 @@ extension Sequence where Self: Iterable_ { public func reduce( _ initialResult: consuming Result, _ nextPartialResult: - (_ partialResult: consuming Result, borrowing Element_) throws -> Result - ) rethrows -> Result { + (_ partialResult: consuming Result, borrowing Element_) throws(Failure) -> Result + ) throws(Failure) -> Result { try makeIterableIterator_().reduce(initialResult, nextPartialResult) } @@ -51,8 +51,8 @@ extension Sequence where Self: Iterable_ { public func reduce( into initialResult: consuming Result, _ updateAccumulatingResult: - (_ partialResult: inout Result, borrowing Element_) throws -> () - ) rethrows -> Result { + (_ partialResult: inout Result, borrowing Element_) throws(Failure) -> () + ) throws(Failure) -> Result { try makeIterableIterator_().reduce(into: initialResult, updateAccumulatingResult) } } @@ -64,24 +64,24 @@ extension Iterable_ @inlinable public func elementsEqual( _ other: borrowing OtherSequence, - by areEquivalent: (borrowing Element_, borrowing OtherSequence.Element_) throws -> Bool - ) rethrows -> Bool - where OtherSequence: ~Copyable & ~Escapable, OtherSequence.Element_: ~Copyable + by areEquivalent: (borrowing Element_, borrowing OtherSequence.Element_) throws(Failure) -> Bool + ) throws(Failure) -> Bool + where OtherSequence: ~Copyable & ~Escapable, OtherSequence.Element_: ~Copyable, OtherSequence.Failure == Failure { // FIXME: Forward to the iterator's implementation of same var iter1 = makeIterableIterator_() var iter2 = other.makeIterableIterator_() while true { - var el1 = iter1.nextSpan_() + var el1 = try iter1.nextSpan_() if el1.isEmpty { // LHS is empty - sequences are equal iff RHS is also empty - let el2 = iter2.nextSpan_(maximumCount: 1) + let el2 = try iter2.nextSpan_(maximumCount: 1) return el2.isEmpty } while el1.count > 0 { - let el2 = iter2.nextSpan_(maximumCount: el1.count) + let el2 = try iter2.nextSpan_(maximumCount: el1.count) if el2.isEmpty { return false } for i in 0..>( + public func elementsEqual>( _ other: borrowing OtherSequence - ) -> Bool + ) throws(Failure) -> Bool where OtherSequence: ~Copyable & ~Escapable, OtherSequence.Element_: ~Copyable { - return self.elementsEqual(other, by: ==) + return try self.elementsEqual(other, by: ==) } } diff --git a/Sources/ContainersPreview/Protocols/Iterable+Utilities.swift b/Sources/ContainersPreview/Protocols/Iterable+Utilities.swift index 995c3cbf..0e573b7c 100644 --- a/Sources/ContainersPreview/Protocols/Iterable+Utilities.swift +++ b/Sources/ContainersPreview/Protocols/Iterable+Utilities.swift @@ -22,17 +22,17 @@ extension Iterable_ where Self: ~Copyable & ~Escapable, Element_: Copyable { @inlinable package func _copyContents( intoPrefixOf buffer: UnsafeMutableBufferPointer - ) -> Int { + ) throws(Failure) -> Int { var target = buffer var it = self.makeIterableIterator_() while target.count != 0 { - let span = it.nextSpan_(maximumCount: target.count) + let span = try it.nextSpan_(maximumCount: target.count) if span.isEmpty { return buffer.count - target.count } target._initializeAndDropPrefix(copying: span) } - let test = it.nextSpan_() + let test = try it.nextSpan_() precondition(test.isEmpty, "Contents do not fit in target buffer") return buffer.count } diff --git a/Sources/ContainersPreview/Protocols/Iterable.swift b/Sources/ContainersPreview/Protocols/Iterable.swift index 735123bd..8af99599 100644 --- a/Sources/ContainersPreview/Protocols/Iterable.swift +++ b/Sources/ContainersPreview/Protocols/Iterable.swift @@ -14,9 +14,11 @@ #if compiler(>=6.4) && UnstableContainersPreview @available(SwiftStdlib 5.0, *) -public protocol Iterable_: ~Copyable, ~Escapable { - associatedtype Element_: ~Copyable - associatedtype IterableIterator_: IterableIteratorProtocol_ & ~Copyable & ~Escapable +public protocol Iterable_: ~Copyable, ~Escapable { + associatedtype Element_: ~Copyable & ~Escapable + associatedtype Failure: Error = Never + + associatedtype IterableIterator_: IterableIteratorProtocol_ & ~Copyable & ~Escapable var underestimatedCount_: Int { get } @@ -58,12 +60,12 @@ where { /// Implementation demo of what borrowing for-in loops would need to expand into. @inlinable - public func _borrowingForEach( - _ body: (borrowing Element_) throws(E) -> Void - ) throws(E) -> Void { + public func _borrowingForEach( + _ body: (borrowing Element_) throws(Failure) -> Void + ) throws(Failure) -> Void { var it = makeIterableIterator_() while true { - let span = it.nextSpan_() + let span = try it.nextSpan_() if span.isEmpty { break } var i = 0 while i < span.count { diff --git a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+ElementsEqual.swift b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+ElementsEqual.swift index 08a67ea5..0438cd81 100644 --- a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+ElementsEqual.swift +++ b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+ElementsEqual.swift @@ -25,15 +25,15 @@ where { @inlinable package func _elementsEqual< - Other: Iterable_ & ~Copyable & ~Escapable + Other: Iterable_ & ~Copyable & ~Escapable >( _ other: borrowing Other, - ) -> Bool + ) throws(Failure) -> Bool where Other.Element_: ~Copyable { let it1 = self.makeIterableIterator_() let it2 = other.makeIterableIterator_() - return it1.elementsEqual(it2) + return try it1.elementsEqual(it2) } } @@ -66,13 +66,12 @@ where /// - Complexity: O(*m*), where *m* is the count of the longer of the input sequences. @inlinable package func _elementsEqual< - E: Error, Other: Iterable_ & ~Copyable & ~Escapable >( _ other: borrowing Other, - by areEquivalent: (borrowing Element_, borrowing Other.Element_) throws(E) -> Bool - ) throws(E) -> Bool - where Other.Element_: ~Copyable + by areEquivalent: (borrowing Element_, borrowing Other.Element_) throws(Failure) -> Bool + ) throws(Failure) -> Bool + where Other.Element_: ~Copyable, Other.Failure == Failure { let it1 = self.makeIterableIterator_() let it2 = other.makeIterableIterator_() @@ -88,14 +87,14 @@ where { @inlinable package consuming func elementsEqual< - Other: IterableIteratorProtocol_ & ~Copyable & ~Escapable + Other: IterableIteratorProtocol_ & ~Copyable & ~Escapable >( _ other: consuming Other, - ) -> Bool + ) throws(Failure) -> Bool where Other.Element_: ~Copyable { var result = true - _spanwiseZip(state: &result, with: other) { state, a, b in + try _spanwiseZip(state: &result, with: other) { state, a, b in if a.isEmpty || b.isEmpty { state = false return false @@ -114,10 +113,10 @@ where @inlinable package consuming func _directElementsEqual< - Other: IterableIteratorProtocol_ & ~Copyable & ~Escapable + Other: IterableIteratorProtocol_ & ~Copyable & ~Escapable >( _ other: consuming Other, - ) -> Bool + ) throws(Failure) -> Bool where Other.Element_: ~Copyable { #if true // FIXME: rdar://150228920 Exclusive access scopes aren't expanded enough @@ -126,13 +125,13 @@ where // (It lets the two iterators run at their native speeds, with no artificial // maximumCounts.) while true { - let a = self.nextSpan_() + let a = try self.nextSpan_() var i = 0 if a.isEmpty { - return other.nextSpan_().isEmpty + return try other.nextSpan_().isEmpty } while i < a.count { - let b = other.nextSpan_(maximumCount: a.count - i) + let b = try other.nextSpan_(maximumCount: a.count - i) if b.isEmpty { return false } @@ -182,16 +181,15 @@ where { @inlinable package consuming func elementsEqual< - E: Error, Other: IterableIteratorProtocol_ & ~Copyable & ~Escapable >( _ other: consuming Other, - by areEquivalent: (borrowing Element_, borrowing Other.Element_) throws(E) -> Bool - ) throws(E) -> Bool - where Other.Element_: ~Copyable + by areEquivalent: (borrowing Element_, borrowing Other.Element_) throws(Failure) -> Bool + ) throws(Failure) -> Bool + where Other.Element_: ~Copyable, Other.Failure == Failure { var result = true - try _spanwiseZip(state: &result, with: other) { state, a, b throws(E) in + try _spanwiseZip(state: &result, with: other) { state, a, b throws(Failure) in assert(a.count == b.count || a.isEmpty || b.isEmpty) if a.isEmpty || b.isEmpty { state = false diff --git a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Filter.swift b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Filter.swift index 293f8be7..16a22f53 100644 --- a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Filter.swift +++ b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Filter.swift @@ -34,9 +34,10 @@ public struct BorrowingFilter< >: ~Copyable, ~Escapable where Base.Element_: ~Copyable { public typealias Element_ = Base.Element_ - + public typealias Failure = Base.Failure + @_alwaysEmitIntoClient - public let _isIncluded: (borrowing Element_) -> Bool + public let _isIncluded: (borrowing Element_) throws(Base.Failure) -> Bool @_alwaysEmitIntoClient public var _base: Base @@ -58,13 +59,13 @@ where Base.Element_: ~Copyable { extension BorrowingFilter: IterableIteratorProtocol_ where Base: ~Copyable & ~Escapable, Base.Element_: ~Copyable { @_lifetime(&self) - public mutating func nextSpan_(maximumCount: Int) -> Span { + public mutating func nextSpan_(maximumCount: Int) throws(Failure) -> Span { // FIXME: This is quite inefficient compared to Container's filter while true { - let span = _base.nextSpan_(maximumCount: 1) + let span = try _base.nextSpan_(maximumCount: 1) if span.isEmpty { return span } precondition(span.count == 1, "Invalid BorrowingIterator") - if _isIncluded(span[unchecked: 0]) { return span } + if try _isIncluded(span[unchecked: 0]) { return span } } } } diff --git a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Map.swift b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Map.swift index ccfdd7f0..653b86c7 100644 --- a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Map.swift +++ b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Map.swift @@ -68,8 +68,11 @@ where @inlinable public mutating func next() throws(Failure) -> Element? { - let span = _it.nextSpan_(maximumCount: 1) - guard !span.isEmpty else { return nil } + // FIXME: Probably wrong to eat the error from the base iterator + guard + let span = try? _it.nextSpan_(maximumCount: 1), + !span.isEmpty + else { return nil } return try _transform(span[unchecked: 0]) } @@ -82,8 +85,11 @@ where ) throws(Error) -> Bool { var success = false while !target.isFull { - let span = _it.nextSpan_(maximumCount: target.freeCapacity) - guard !span.isEmpty else { break } + // FIXME: Probably wrong to eat the error from the base iterator + guard + let span = try? _it.nextSpan_(maximumCount: target.freeCapacity), + !span.isEmpty + else { break } success = true var i = 0 while i < span.count { diff --git a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Reduce.swift b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Reduce.swift index 20cc9b22..9272d19c 100644 --- a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Reduce.swift +++ b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+Reduce.swift @@ -20,13 +20,13 @@ where Element_: ~Copyable { @inlinable - public consuming func reduce( + public consuming func reduce( _ initialResult: consuming Result, - _ nextPartialResult: (consuming Result, borrowing Element_) throws(E) -> Result - ) throws(E) -> Result { + _ nextPartialResult: (consuming Result, borrowing Element_) throws(Failure) -> Result + ) throws(Failure) -> Result { var result = initialResult while true { - let span = self.nextSpan_() + let span = try self.nextSpan_() guard !span.isEmpty else { break } var i = 0 while i < span.count { @@ -38,13 +38,13 @@ where } @inlinable - public consuming func reduce( + public consuming func reduce( into initialResult: consuming Result, - _ updateAccumulatingResult: (inout Result, borrowing Element_) throws(E) -> Void - ) throws(E) -> Result { + _ updateAccumulatingResult: (inout Result, borrowing Element_) throws(Failure) -> Void + ) throws(Failure) -> Result { var result = initialResult while true { - let span = self.nextSpan_() + let span = try self.nextSpan_() guard !span.isEmpty else { break } var i = 0 while i < span.count { diff --git a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+SpanwiseZip.swift b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+SpanwiseZip.swift index 0fe44983..1b578cae 100644 --- a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+SpanwiseZip.swift +++ b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+SpanwiseZip.swift @@ -26,13 +26,13 @@ where @inlinable internal consuming func _spanwiseZip< Other: IterableIteratorProtocol_ & ~Copyable & ~Escapable, - State: ~Copyable, E: Error + State: ~Copyable >( state: inout State, with other: consuming Other, - by process: (inout State, Span, Span) throws(E) -> Bool - ) throws(E) - where Other.Element_: ~Copyable + by process: (inout State, Span, Span) throws(Failure) -> Bool + ) throws(Failure) + where Other.Element_: ~Copyable, Other.Failure == Failure { #if true // FIXME: rdar://150228920 Exclusive access scopes aren't expanded enough // Note: This is the less efficient implementation of spanwiseZip. The @@ -41,17 +41,17 @@ where // maximumCounts.) loop: while true { - var a = self.nextSpan_() + var a = try self.nextSpan_() if a.isEmpty { while true { - let b = other.nextSpan_() + let b = try other.nextSpan_() guard !b.isEmpty else { break } guard try process(&state, a, b) else { break } } return } repeat { - let b = other.nextSpan_(maximumCount: a.count) + let b = try other.nextSpan_(maximumCount: a.count) if b.isEmpty { guard try process(&state, a, b) else { return } } else { diff --git a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol.swift b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol.swift index 409e18c7..f61d5c28 100644 --- a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol.swift +++ b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol.swift @@ -18,8 +18,9 @@ import InternalCollectionsUtilities #endif @available(SwiftStdlib 5.0, *) -public protocol IterableIteratorProtocol_: ~Copyable, ~Escapable { - associatedtype Element_: ~Copyable +public protocol IterableIteratorProtocol_: ~Copyable, ~Escapable { + associatedtype Element_: ~Copyable & ~Escapable + associatedtype Failure: Error = Never /// Advance the iterator, returning an ephemeral span over the elements /// that are ready to be visited. @@ -69,7 +70,7 @@ public protocol IterableIteratorProtocol_: ~Copyable, ~Escapable { /// method may vary between different borrows of the same container.) @_lifetime(&self) @_lifetime(self: copy self) - mutating func nextSpan_(maximumCount: Int) -> Span + mutating func nextSpan_(maximumCount: Int) throws(Failure) -> Span /// Advance the position of this iterator by the specified offset, or until /// the end of the underlying sequence. @@ -81,7 +82,7 @@ public protocol IterableIteratorProtocol_: ~Copyable, ~Escapable { /// /// `maximumOffset` must be nonnegative. @_lifetime(self: copy self) - mutating func skip_(by maximumOffset: Int) -> Int + mutating func skip_(by maximumOffset: Int) throws(Failure) -> Int } @available(SwiftStdlib 5.0, *) @@ -90,8 +91,8 @@ where Self: ~Copyable & ~Escapable, Element_: ~Copyable { @_lifetime(&self) @_lifetime(self: copy self) @_transparent - public mutating func nextSpan_() -> Span { - nextSpan_(maximumCount: Int.max) + public mutating func nextSpan_() throws(Failure) -> Span { + try nextSpan_(maximumCount: Int.max) } } @@ -100,10 +101,10 @@ extension IterableIteratorProtocol_ where Self: ~Copyable & ~Escapable, Element_: ~Copyable { @_lifetime(self: copy self) @inlinable - public mutating func skip_(by offset: Int) -> Int { + public mutating func skip_(by offset: Int) throws(Failure) -> Int { var remainder = offset while remainder > 0 { - let span = nextSpan_(maximumCount: remainder) + let span = try nextSpan_(maximumCount: remainder) if span.isEmpty { break } remainder &-= span.count } @@ -126,16 +127,16 @@ extension IterableIteratorProtocol_ where Self: ~Copyable & ~Escapable { @available(SwiftStdlib 5.0, *) extension IterableIteratorProtocol_ -where Self: ~Copyable & ~Escapable, Element_: Copyable + where Self: ~Copyable & ~Escapable, Element_: Copyable { @_lifetime(self: copy self) @inlinable @_transparent - package mutating func _copyContents_(into target: inout OutputSpan) { - target.withUnsafeMutableBufferPointer { dst, dstCount in + package mutating func _copyContents_(into target: inout OutputSpan) throws(Failure) { + try target.withUnsafeMutableBufferPointer { (dst, dstCount) throws(Failure) -> Void in var tail = dst._extracting(droppingFirst: dstCount) while !tail.isEmpty { - let src = nextSpan_(maximumCount: tail.count) + let src = try nextSpan_(maximumCount: tail.count) if src.isEmpty { break } tail._initializeAndDropPrefix(copying: src) dstCount += src.count diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Append.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Append.swift index ee169e94..44519882 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Append.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Append.swift @@ -313,11 +313,11 @@ extension RigidDeque /*where Element: Copyable*/ { #if compiler(>=6.4) && UnstableContainersPreview @_alwaysEmitIntoClient internal mutating func _append< - S: Iterable_ & ~Copyable & ~Escapable - >(copying items: borrowing S) { + S: Iterable_ & ~Copyable & ~Escapable, E + >(copying items: borrowing S) throws(E) { var it = items.makeIterableIterator_() while true { - let span = it.nextSpan_() + let span = try it.nextSpan_() if span.isEmpty { break } self.append(copying: span) } @@ -334,10 +334,10 @@ extension RigidDeque /*where Element: Copyable*/ { /// /// - Complexity: O(*m*), where *m* is the length of `newElements`. @_alwaysEmitIntoClient - public mutating func append & ~Copyable & ~Escapable>( + public mutating func append & ~Copyable & ~Escapable, E>( copying newElements: borrowing S - ) { - self._append(copying: newElements) + ) throws(E) { + try self._append(copying: newElements) } #endif @@ -375,11 +375,11 @@ extension RigidDeque /*where Element: Copyable*/ { /// - Complexity: O(*m*), where *m* is the length of `items`. @available(SwiftStdlib 5.0, *) @_alwaysEmitIntoClient public mutating func append< - S: Iterable_ & Sequence + S: Iterable_ & Sequence >( copying items: S ) { - self._append(copying: items) + try self._append(copying: items) } #endif } diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Initializers.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Initializers.swift index 79bedde8..3b9c2de7 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Initializers.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Initializers.swift @@ -132,13 +132,13 @@ extension RigidDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient @inline(__always) public init< - S: Iterable_ & ~Copyable & ~Escapable + S: Iterable_ & ~Copyable & ~Escapable, E >( capacity: Int, copying contents: borrowing S - ) { + ) throws(E) { self.init(capacity: capacity) - self._append(copying: contents) + try self._append(copying: contents) } #endif @@ -187,7 +187,7 @@ extension RigidDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient @inline(__always) public init< - S: Iterable_ & Sequence + S: Iterable_ & Sequence >( capacity: Int, copying contents: borrowing S @@ -208,7 +208,7 @@ extension RigidDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient @inline(__always) public init< - S: Iterable_ & Collection + S: Iterable_ & Collection >( capacity: Int? = nil, copying contents: S diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift index 046e1766..2d351cbe 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift @@ -363,7 +363,7 @@ extension RigidDeque /* where Element: Copyable */ { at index: Int, copying items: borrowing C, newCount: Int - ) { + ) where C.Failure == Never { let expectedCount = self.count + newCount var it = items.makeIterableIterator_() insert(addingCount: newCount, at: index) { target in @@ -424,7 +424,7 @@ extension RigidDeque /* where Element: Copyable */ { C: Container & ~Copyable & ~Escapable >( copying items: borrowing C, at index: Int - ) { + ) where C.Failure == Never { _insertContainer( at: index, copying: items, newCount: items.count) } @@ -486,7 +486,7 @@ extension RigidDeque /* where Element: Copyable */ { C: Container & Collection >( copying items: borrowing C, at index: Int - ) { + ) where C.Failure == Never { _insertContainer( at: index, copying: items, newCount: items.count) } diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Prepend.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Prepend.swift index 45c1556e..c1c2aa7d 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Prepend.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Prepend.swift @@ -290,8 +290,8 @@ extension RigidDeque /*where Element: Copyable*/ { #if compiler(>=6.4) && UnstableContainersPreview @inlinable internal mutating func _prepend< - S: Iterable_ & ~Copyable & ~Escapable - >(copying items: borrowing S) { + S: Iterable_ & ~Copyable & ~Escapable, E + >(copying items: borrowing S) throws(E) { // We don't know the exact count of new elements, so we cannot initialize // them in place. Append them to the end of the deque first, then rotate // them to their correct location. @@ -300,20 +300,20 @@ extension RigidDeque /*where Element: Copyable*/ { // then we should use that when possible to copy items to their final // location in a single pass. let oldCount = self.count - self._append(copying: items) // Not a typo! + try self._append(copying: items) // Not a typo! _handle.rotate(toStartAtOffset: oldCount) } @inlinable internal mutating func _prepend< - S: Iterable_ & ~Copyable & ~Escapable + S: Iterable_ & ~Copyable & ~Escapable, E >( copying items: borrowing S, exactCount: Int - ) { + ) throws(E) { var it = items.makeIterableIterator_() - self.prepend(addingCount: exactCount) { target in - let span = it.nextSpan_(maximumCount: target.freeCapacity) + try self.prepend(addingCount: exactCount) { (target) throws(E) in + let span = try it.nextSpan_(maximumCount: target.freeCapacity) target._append(copying: span) } } @@ -333,10 +333,10 @@ extension RigidDeque /*where Element: Copyable*/ { /// /// - Complexity: O(*m*), where *m* is the length of `items`. @_alwaysEmitIntoClient - public mutating func prepend & ~Copyable & ~Escapable>( + public mutating func prepend & ~Copyable & ~Escapable, E>( copying items: borrowing S - ) { - self._prepend(copying: items) + ) throws(E) { + try self._prepend(copying: items) } /// Copies the elements of a container and prepends them to the front @@ -352,7 +352,7 @@ extension RigidDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient public mutating func prepend & ~Copyable & ~Escapable>( copying items: borrowing C - ) { + ) where C.Failure == Never { self._prepend(copying: items, exactCount: items.count) } #endif @@ -437,7 +437,7 @@ extension RigidDeque /*where Element: Copyable*/ { /// /// - Complexity: O(*m*), where *m* is the length of `items`. @_alwaysEmitIntoClient - public mutating func prepend & Sequence>( + public mutating func prepend & Sequence>( copying items: borrowing S ) { self._prepend(copying: items) @@ -453,7 +453,7 @@ extension RigidDeque /*where Element: Copyable*/ { /// /// - Complexity: O(*m*), where *m* is the length of `items`. @_alwaysEmitIntoClient - public mutating func prepend & Collection>( + public mutating func prepend & Collection>( copying items: borrowing S ) { self._prepend(copying: items, exactCount: items.count) @@ -471,7 +471,7 @@ extension RigidDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient public mutating func prepend & Sequence>( copying items: borrowing C - ) { + ) where C.Failure == Never { self._prepend(copying: items, exactCount: items.count) } @@ -487,7 +487,7 @@ extension RigidDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient public mutating func prepend & Collection>( copying items: borrowing C - ) { + ) where C.Failure == Never { self._prepend(copying: items, exactCount: items.count) } #endif diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift index e9d592a4..1d7e61c4 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Replacements.swift @@ -463,7 +463,7 @@ extension RigidDeque /* where Element: Copyable */ { removing subrange: Range, copyingContainer items: borrowing C, newCount: Int - ) { + ) where C.Failure == Never { let expectedCount = self.count - subrange.count + newCount var it = items.makeIterableIterator_() @@ -536,7 +536,7 @@ extension RigidDeque /* where Element: Copyable */ { >( removing subrange: Range, copying items: borrowing C - ) { + ) where C.Failure == Never { _replace( removing: subrange, copyingContainer: items, newCount: items.count) } @@ -613,7 +613,7 @@ extension RigidDeque /* where Element: Copyable */ { >( removing subrange: Range, copying items: C - ) { + ) where C.Failure == Never { _replace( removing: subrange, copyingContainer: items, newCount: items.count) } diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Append.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Append.swift index f598555f..37f0b5d5 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Append.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Append.swift @@ -300,11 +300,11 @@ extension UniqueDeque /*where Element: Copyable*/ { #if compiler(>=6.4) && UnstableContainersPreview @_alwaysEmitIntoClient internal mutating func _append< - S: Iterable_ & ~Copyable & ~Escapable - >(copying items: borrowing S) { + S: Iterable_ & ~Copyable & ~Escapable, E + >(copying items: borrowing S) throws(E) { var it = items.makeIterableIterator_() while true { - let span = it.nextSpan_() + let span = try it.nextSpan_() if span.isEmpty { break } self.append(copying: span) } @@ -323,10 +323,10 @@ extension UniqueDeque /*where Element: Copyable*/ { /// - Complexity: O(*m*), where *m* is the length of `items`, when amortized /// over many similar invocations on the same deque @_alwaysEmitIntoClient - public mutating func append & ~Copyable & ~Escapable>( + public mutating func append & ~Copyable & ~Escapable, E>( copying items: borrowing S - ) { - self._append(copying: items) + ) throws(E) { + try self._append(copying: items) } #endif @@ -374,11 +374,11 @@ extension UniqueDeque /*where Element: Copyable*/ { /// amortized over many similar invocations over the same deque. @_alwaysEmitIntoClient public mutating func append< - S: Iterable_ & Sequence + S: Iterable_ & Sequence >( copying items: S ) { - self._append(copying: items) + try self._append(copying: items) } #endif diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Initializers.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Initializers.swift index 4045ef9f..7f8fd9f6 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Initializers.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Initializers.swift @@ -145,13 +145,13 @@ extension UniqueDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient @inline(__always) public init< - S: Iterable_ & ~Copyable & ~Escapable + S: Iterable_ & ~Copyable & ~Escapable, E >( capacity: Int? = nil, copying contents: borrowing S - ) { + ) throws(E) { self.init(minimumCapacity: capacity ?? 0) - self._append(copying: contents) + try self._append(copying: contents) } #endif @@ -184,7 +184,7 @@ extension UniqueDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient @inline(__always) public init< - S: Iterable_ & Sequence + S: Iterable_ & Sequence >( capacity: Int? = nil, copying contents: S @@ -203,7 +203,7 @@ extension UniqueDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient @inline(__always) public init< - S: Iterable_ & Collection + S: Iterable_ & Collection >( capacity: Int? = nil, copying contents: S diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Insertions.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Insertions.swift index f7785077..2327735c 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Insertions.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Insertions.swift @@ -381,7 +381,7 @@ extension UniqueDeque /* where Element: Copyable */ { at index: Int, copying items: borrowing C, newCount: Int - ) { + ) where C.Failure == Never { let expectedCount = self.count + newCount var it = items.makeIterableIterator_() insert(addingCount: newCount, at: index) { target in @@ -445,7 +445,7 @@ extension UniqueDeque /* where Element: Copyable */ { C: Container & ~Copyable & ~Escapable >( copying items: borrowing C, at index: Int - ) { + ) where C.Failure == Never { _insertContainer( at: index, copying: items, newCount: items.count) } @@ -511,7 +511,7 @@ extension UniqueDeque /* where Element: Copyable */ { C: Container & Collection >( copying items: borrowing C, at index: Int - ) { + ) where C.Failure == Never { _insertContainer( at: index, copying: items, newCount: items.count) } diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Prepend.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Prepend.swift index c59fa248..1f8dd8d2 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Prepend.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Prepend.swift @@ -322,8 +322,8 @@ extension UniqueDeque /*where Element: Copyable*/ { #if compiler(>=6.4) && UnstableContainersPreview @inlinable internal mutating func _prepend< - S: Iterable_ & ~Copyable & ~Escapable - >(copying items: borrowing S) { + S: Iterable_ & ~Copyable & ~Escapable, E + >(copying items: borrowing S) throws(E) { // We don't know the exact count of new elements, so we cannot initialize // them in place. Append them to the end of the deque first, then rotate // them to their correct location. @@ -332,20 +332,20 @@ extension UniqueDeque /*where Element: Copyable*/ { // then we should use that when possible to copy items to their final // location in a single pass. let oldCount = self.count - self._append(copying: items) // Not a typo! + try self._append(copying: items) // Not a typo! _storage._handle.rotate(toStartAtOffset: oldCount) } @inlinable internal mutating func _prepend< - S: Iterable_ & ~Copyable & ~Escapable + S: Iterable_ & ~Copyable & ~Escapable, E >( copying items: borrowing S, exactCount: Int - ) { + ) throws(E) { var it = items.makeIterableIterator_() - self.prepend(addingCount: exactCount) { target in - let span = it.nextSpan_(maximumCount: target.freeCapacity) + try self.prepend(addingCount: exactCount) { (target) throws(E) in + let span = try it.nextSpan_(maximumCount: target.freeCapacity) target._append(copying: span) } } @@ -367,11 +367,11 @@ extension UniqueDeque /*where Element: Copyable*/ { /// over many similar invocations on the same deque. @_alwaysEmitIntoClient public mutating func prepend< - S: Iterable_ & ~Copyable & ~Escapable + S: Iterable_ & ~Copyable & ~Escapable, E >( copying items: borrowing S - ) { - self._prepend(copying: items) + ) throws(E) { + try self._prepend(copying: items) } /// Copies the elements of a container and prepends them to the front @@ -388,7 +388,7 @@ extension UniqueDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient public mutating func prepend & ~Copyable & ~Escapable>( copying items: borrowing C - ) { + ) where C.Failure == Never { self._prepend(copying: items, exactCount: items.count) } #endif @@ -479,7 +479,7 @@ extension UniqueDeque /*where Element: Copyable*/ { /// over many similar invocations on the same deque. @_alwaysEmitIntoClient public mutating func prepend< - S: Iterable_ & Sequence + S: Iterable_ & Sequence >( copying items: borrowing S ) { @@ -498,7 +498,7 @@ extension UniqueDeque /*where Element: Copyable*/ { /// over many similar invocations on the same deque. @_alwaysEmitIntoClient public mutating func prepend< - S: Iterable_ & Collection + S: Iterable_ & Collection >( copying items: borrowing S ) { @@ -518,7 +518,7 @@ extension UniqueDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient public mutating func prepend & Sequence>( copying items: borrowing C - ) { + ) where C.Failure == Never { self._prepend(copying: items, exactCount: items.count) } @@ -535,7 +535,7 @@ extension UniqueDeque /*where Element: Copyable*/ { @_alwaysEmitIntoClient public mutating func prepend & Collection>( copying items: C - ) { + ) where C.Failure == Never { self._prepend(copying: items, exactCount: items.count) } #endif diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift index 3546a7df..a6c5b169 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Replacements.swift @@ -472,7 +472,7 @@ extension UniqueDeque /* where Element: Copyable */ { removing subrange: Range, copyingContainer items: borrowing C, newCount: Int - ) { + ) where C.Failure == Never { let expectedCount = self.count - subrange.count + newCount var it = items.makeIterableIterator_() @@ -546,7 +546,7 @@ extension UniqueDeque /* where Element: Copyable */ { >( removing subrange: Range, copying items: borrowing C - ) { + ) where C.Failure == Never { _replace( removing: subrange, copyingContainer: items, newCount: items.count) } @@ -625,7 +625,7 @@ extension UniqueDeque /* where Element: Copyable */ { >( removing subrange: Range, copying items: C - ) { + ) where C.Failure == Never { _replace( removing: subrange, copyingContainer: items, newCount: items.count) } diff --git a/Tests/ContainersTests/BorrowingSequenceTests.swift b/Tests/ContainersTests/BorrowingSequenceTests.swift index be1f647c..3d1fd812 100644 --- a/Tests/ContainersTests/BorrowingSequenceTests.swift +++ b/Tests/ContainersTests/BorrowingSequenceTests.swift @@ -65,11 +65,11 @@ final class BorrowingSequenceTests: XCTestCase { @available(SwiftStdlib 5.0, *) extension Iterable_ where Self: ~Copyable & ~Escapable, Element_: Copyable { - func collectViaBorrowing() -> [Element_] { + func collectViaBorrowing() throws(Failure) -> [Element_] { var borrowIterator = makeIterableIterator_() var result: [Element_] = [] while true { - let span = borrowIterator.nextSpan_(maximumCount: .max) + let span = try borrowIterator.nextSpan_(maximumCount: .max) if span.isEmpty { break } for i in span.indices { result.append(span[i]) diff --git a/Tests/_CollectionsTestSupport/AssertionContexts/Assertions+Containers.swift b/Tests/_CollectionsTestSupport/AssertionContexts/Assertions+Containers.swift index 4b6577f0..b5f9c530 100644 --- a/Tests/_CollectionsTestSupport/AssertionContexts/Assertions+Containers.swift +++ b/Tests/_CollectionsTestSupport/AssertionContexts/Assertions+Containers.swift @@ -127,8 +127,9 @@ public func expectIterableContents< @available(SwiftStdlib 5.0, *) public func expectIterablePayloads< Payload: Equatable, - E1: Iterable_> & ~Copyable & ~Escapable, + E1: Iterable_, E> & ~Copyable & ~Escapable, C2: Collection, + E, >( _ left: borrowing E1, equalTo right: C2, @@ -136,12 +137,12 @@ public func expectIterablePayloads< trapping: Bool = false, file: StaticString = #filePath, line: UInt = #line -) where E1.Element_: ~Copyable { +) throws(E) where E1.Element_: ~Copyable { var it1 = left.makeIterableIterator_() var it2 = right.makeIterator() var i = 0 while true { - let next1 = it1.nextSpan_(maximumCount: 1) + let next1 = try it1.nextSpan_(maximumCount: 1) let next2 = it2.next() switch (next1.isEmpty, next2) { case (true, nil): @@ -174,8 +175,9 @@ public func expectIterablePayloads< @available(SwiftStdlib 5.0, *) public func expectIterablePayloads< Payload: Equatable, - E1: Iterable_> & ~Copyable & ~Escapable, + E1: Iterable_, E> & ~Copyable & ~Escapable, C2: Collection, + E, >( _ left: borrowing E1, equalTo right: C2, @@ -183,12 +185,12 @@ public func expectIterablePayloads< trapping: Bool = false, file: StaticString = #filePath, line: UInt = #line -) { +) throws(E) { var it1 = left.makeIterableIterator_() var it2 = right.makeIterator() var i = 0 while true { - let next1 = it1.nextSpan_(maximumCount: 1) + let next1 = try it1.nextSpan_(maximumCount: 1) let next2 = it2.next() switch (next1.isEmpty, next2) { case (true, nil): @@ -230,12 +232,13 @@ public func expectIterablesWithEquivalentElements< trapping: Bool = false, file: StaticString = #filePath, line: UInt = #line -) +) throws(S1.Failure) where S1.Element_: ~Copyable, - S2.Element_: ~Copyable + S2.Element_: ~Copyable, + S1.Failure == S2.Failure { - if left._elementsEqual(right, by: areEquivalent) { return } + if try left._elementsEqual(right, by: areEquivalent) { return } _expectFailure( "Containers do not have equivalent elements", message, trapping: trapping, file: file, line: line) @@ -245,8 +248,9 @@ where @available(SwiftStdlib 5.0, *) public func expectIterablesWithEqualElements< Element: Equatable, - S1: Iterable_ & ~Copyable & ~Escapable, - S2: Iterable_ & ~Copyable & ~Escapable, + S1: Iterable_ & ~Copyable & ~Escapable, + S2: Iterable_ & ~Copyable & ~Escapable, + E, >( _ left: borrowing S1, _ right: borrowing S2, @@ -254,12 +258,12 @@ public func expectIterablesWithEqualElements< trapping: Bool = false, file: StaticString = #filePath, line: UInt = #line -) +) throws(E) where S1.Element_: ~Copyable, S2.Element_: ~Copyable { - if left._elementsEqual(right) { return } + if try left._elementsEqual(right) { return } _expectFailure( "Containers do not have equal elements", message, trapping: trapping, file: file, line: line) @@ -269,8 +273,9 @@ where @available(SwiftStdlib 5.0, *) public func expectIterableContents< Element: Equatable, - S1: Iterable_ & ~Copyable & ~Escapable, + S1: Iterable_ & ~Copyable & ~Escapable, C2: Collection, + E, >( _ left: borrowing S1, equalTo right: C2, @@ -278,11 +283,11 @@ public func expectIterableContents< trapping: Bool = false, file: StaticString = #filePath, line: UInt = #line -) { +) throws(E) { var it1 = left.makeIterableIterator_() var it2 = right.makeIterator() while true { - let span = it1.nextSpan_() + let span = try it1.nextSpan_() if span.isEmpty { break } for i in 0 ..< span.count { guard let b = it2.next() else { @@ -321,12 +326,12 @@ public func expectIterableContents< trapping: Bool = false, file: StaticString = #filePath, line: UInt = #line -) where S1.Element_: ~Copyable { +) throws(S1.Failure) where S1.Element_: ~Copyable { var it1 = left.makeIterableIterator_() var it2 = right.makeIterator() var offset = 0 while true { - let span = it1.nextSpan_() + let span = try it1.nextSpan_() if span.isEmpty { break } for i in 0 ..< span.count { guard let b = it2.next() else { diff --git a/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift b/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift index 7bc473b5..03d7aa5f 100644 --- a/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift +++ b/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift @@ -29,8 +29,8 @@ public func checkIterable< expectedContents: Expected, file: StaticString = #filePath, line: UInt = #line -) where S.Element_: Equatable { - checkIterable( +) throws(S.Failure) where S.Element_: Equatable { + try checkIterable( iterable, expectedContents: expectedContents, by: ==, @@ -48,7 +48,7 @@ public func checkIterable< by areEquivalent: (S.Element_, S.Element_) -> Bool, file: StaticString = #filePath, line: UInt = #line -) where S.Element_: Equatable { +) throws(S.Failure) where S.Element_: Equatable { let entry = TestContext.current.push("checkIterable", file: file, line: line) defer { TestContext.current.pop(entry) } @@ -57,13 +57,13 @@ public func checkIterable< expectLessThanOrEqual(iterable.underestimatedCount_, expectedContents.count) // Check that the spans seem plausibly sized and that the indices are monotonic. - let spanShapes: [Range] = { + let spanShapes: [Range] = try { () throws(S.Failure) in var r: [Range] = [] var pos = 0 var it = iterable.makeIterableIterator_() while true { let origPos = pos - let span = it.nextSpan_() + let span = try it.nextSpan_() pos += span.count if span.isEmpty { break @@ -82,7 +82,7 @@ public func checkIterable< var it = iterable.makeIterableIterator_() var spanIndex = 0 while true { - let span = it.nextSpan_() + let span = try it.nextSpan_() if span.isEmpty { break } expectEqual( span.count, spanShapes[spanIndex].count, @@ -102,7 +102,7 @@ public func checkIterable< var pos = 0 var it = iterable.makeIterableIterator_() while true { - let span = it.nextSpan_(maximumCount: 1) + let span = try it.nextSpan_(maximumCount: 1) if span.isEmpty { break } expectEqual(span.count, 1) for i in 0 ..< span.count { @@ -119,7 +119,7 @@ public func checkIterable< var it = iterable.makeIterableIterator_() var spanIndex = 0 while true { - let span = it.nextSpan_(maximumCount: Int.max) + let span = try it.nextSpan_(maximumCount: Int.max) if span.isEmpty { break } expectEqual( span.count, spanShapes[spanIndex].count,