diff --git a/Sources/ContainersPreview/CMakeLists.txt b/Sources/ContainersPreview/CMakeLists.txt index 0933fbd4..84941192 100644 --- a/Sources/ContainersPreview/CMakeLists.txt +++ b/Sources/ContainersPreview/CMakeLists.txt @@ -44,6 +44,7 @@ target_sources(${module_name} PRIVATE "Protocols/IterableIteratorProtocol+ElementsEqual.swift" "Protocols/IterableIteratorProtocol+Filter.swift" "Protocols/IterableIteratorProtocol+Map.swift" + "Protocols/IterableIteratorProtocol+MapError.swift" "Protocols/IterableIteratorProtocol+Reduce.swift" "Protocols/IterableIteratorProtocol+SpanwiseZip.swift" "Protocols/Iterable.swift" diff --git a/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+MapError.swift b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+MapError.swift new file mode 100644 index 00000000..14d8ed99 --- /dev/null +++ b/Sources/ContainersPreview/Protocols/IterableIteratorProtocol+MapError.swift @@ -0,0 +1,95 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 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 compiler(>=6.4) && UnstableContainersPreview + +@available(SwiftStdlib 5.0, *) +extension IterableIteratorProtocol_ +where + Self: ~Copyable & ~Escapable, + Element_: ~Copyable +{ + @inlinable + @_lifetime(copy self) + public consuming func mapError( + _ transform: @escaping (Failure_) -> NewFailure + ) -> ErrorMappedIterator { + ErrorMappedIterator(base: self, transform: transform) + } + + @inlinable + @_lifetime(copy self) + public consuming func mapError( + to error: NewFailure.Type = NewFailure.self + ) -> ErrorMappedIterator + where Failure_ == Never { + ErrorMappedIterator(base: self, transform: { _ in + fatalError("Unreachable") + }) + } +} + +@frozen +public struct ErrorMappedIterator< + Base: IterableIteratorProtocol_ & ~Copyable & ~Escapable, + Failure: Error +>: ~Copyable, ~Escapable +where Base.Element_: ~Copyable { + @usableFromInline + internal let _transform: (Base.Failure_) -> Failure + + @usableFromInline + internal var _base: Base + + @inlinable + @_lifetime(copy base) + public init( + base: consuming Base, + transform: @escaping (Base.Failure_) -> Failure + ) { + self._base = base + self._transform = transform + } +} + +extension ErrorMappedIterator: IterableIteratorProtocol_ +where + Base: ~Copyable & ~Escapable, + Base.Element_: ~Copyable +{ + public typealias Element_ = Base.Element_ + public typealias Failure_ = Failure + + @inlinable + @_lifetime(&self) + @_lifetime(self: copy self) + public mutating func nextSpan_(maximumCount: Int) throws(Failure_) -> Span { + do { + return try _base.nextSpan_(maximumCount: maximumCount) + } catch { + throw _transform(error) + } + } + + @inlinable + @_lifetime(self: copy self) + public mutating func skip_(by maximumOffset: Int) throws(Failure_) -> Int { + do { + return try _base.skip_(by: maximumOffset) + } catch { + throw _transform(error) + } + } +} + +#endif diff --git a/Xcode/Collections.xcodeproj/project.pbxproj b/Xcode/Collections.xcodeproj/project.pbxproj index 0a8fd1a2..3b1bd797 100644 --- a/Xcode/Collections.xcodeproj/project.pbxproj +++ b/Xcode/Collections.xcodeproj/project.pbxproj @@ -209,6 +209,7 @@ 7DC037C02FE3492D00CF0CB8 /* IterableIteratorProtocol+SpanwiseZip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC037B62FE3492D00CF0CB8 /* IterableIteratorProtocol+SpanwiseZip.swift */; }; 7DC037C12FE3492D00CF0CB8 /* IterableIteratorProtocol+Map.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC037B42FE3492D00CF0CB8 /* IterableIteratorProtocol+Map.swift */; }; 7DC037C32FE34B2800CF0CB8 /* Heap+Testing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC037C22FE34B2800CF0CB8 /* Heap+Testing.swift */; }; + 7DC037C52FE3717A00CF0CB8 /* IterableIteratorProtocol+MapError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC037C42FE3717200CF0CB8 /* IterableIteratorProtocol+MapError.swift */; }; 7DC85F8D2F3FDC91004A4E3D /* String+Padding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC85F8C2F3FDC82004A4E3D /* String+Padding.swift */; }; 7DC85F8F2F43D065004A4E3D /* RigidDictionaryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC85F8E2F43D05F004A4E3D /* RigidDictionaryTests.swift */; }; 7DE448452F2D3705005853A7 /* BinaryInteger extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE448442F2D36F9005853A7 /* BinaryInteger extensions.swift */; }; @@ -824,6 +825,7 @@ 7DC037B52FE3492D00CF0CB8 /* IterableIteratorProtocol+Reduce.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IterableIteratorProtocol+Reduce.swift"; sourceTree = ""; }; 7DC037B62FE3492D00CF0CB8 /* IterableIteratorProtocol+SpanwiseZip.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IterableIteratorProtocol+SpanwiseZip.swift"; sourceTree = ""; }; 7DC037C22FE34B2800CF0CB8 /* Heap+Testing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Heap+Testing.swift"; sourceTree = ""; }; + 7DC037C42FE3717200CF0CB8 /* IterableIteratorProtocol+MapError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "IterableIteratorProtocol+MapError.swift"; sourceTree = ""; }; 7DC85F8C2F3FDC82004A4E3D /* String+Padding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Padding.swift"; sourceTree = ""; }; 7DC85F8E2F43D05F004A4E3D /* RigidDictionaryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RigidDictionaryTests.swift; sourceTree = ""; }; 7DE448442F2D36F9005853A7 /* BinaryInteger extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BinaryInteger extensions.swift"; sourceTree = ""; }; @@ -1323,6 +1325,7 @@ 7DC037B22FE3492D00CF0CB8 /* IterableIteratorProtocol+ElementsEqual.swift */, 7DC037B32FE3492D00CF0CB8 /* IterableIteratorProtocol+Filter.swift */, 7DC037B42FE3492D00CF0CB8 /* IterableIteratorProtocol+Map.swift */, + 7DC037C42FE3717200CF0CB8 /* IterableIteratorProtocol+MapError.swift */, 7DC037B52FE3492D00CF0CB8 /* IterableIteratorProtocol+Reduce.swift */, 7DC037B62FE3492D00CF0CB8 /* IterableIteratorProtocol+SpanwiseZip.swift */, 7D57EDCE2F647DB10029D5E3 /* Container */, @@ -2634,6 +2637,7 @@ 7D479CB02F450E4600A8CB59 /* RigidSet+BorrowingSequence.swift in Sources */, 7DE9216629CA70F4004483EB /* _HashNode+Storage.swift in Sources */, 7DE920CE29CA70F4004483EB /* BitSet+CustomReflectable.swift in Sources */, + 7DC037C52FE3717A00CF0CB8 /* IterableIteratorProtocol+MapError.swift in Sources */, 7DE9215029CA70F4004483EB /* _UnmanagedHashNode.swift in Sources */, 7DE920EF29CA70F4004483EB /* BitArray+Codable.swift in Sources */, 7DE920C029CA70F4004483EB /* BitSet+SetAlgebra conformance.swift in Sources */,