Skip to content

Commit ab9b599

Browse files
committed
[stdlib] add CollectionOfOne.withContiguousStorageIfAvailable fast path
1 parent f84dd82 commit ab9b599

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

stdlib/public/core/CollectionOfOne.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ extension CollectionOfOne.Iterator: IteratorProtocol {
7070
}
7171
}
7272

73+
extension CollectionOfOne: Sequence {
74+
// `mutating` since `withUnsafePointer` needs inout.
75+
@inlinable // trivial-implementation
76+
public mutating func withContiguousStorageIfAvailable<R>(
77+
_ body: (UnsafeBufferPointer<Element>) throws -> R
78+
) rethrows -> R? {
79+
return try withUnsafePointer(to: &_element) {
80+
let buf = UnsafeBufferPointer(start: $0, count: 1)
81+
return try body(buf)
82+
}
83+
}
84+
}
85+
7386
extension CollectionOfOne: RandomAccessCollection, MutableCollection {
7487

7588
public typealias Index = Int

0 commit comments

Comments
 (0)