Skip to content

Commit d6d8323

Browse files
committed
Add SingleInlineArray read/_modify accessors
For mutable access.
1 parent 182a27e commit d6d8323

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

SwiftCompilerSources/Sources/SIL/Utilities/SequenceUtilities.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,35 @@ extension LazyFilterSequence : CollectionLikeSequence,
123123
//===----------------------------------------------------------------------===//
124124

125125
public struct SingleInlineArray<Element>: RandomAccessCollection, FormattedLikeArray {
126-
private var singleElement: Element? = nil
126+
private var singleElement: Element?
127127
private var multipleElements: [Element] = []
128128

129129
public init() {}
130130

131+
public init(element: Element) {
132+
singleElement = element
133+
}
134+
131135
public var startIndex: Int { 0 }
132136
public var endIndex: Int {
133137
singleElement == nil ? 0 : multipleElements.count + 1
134138
}
135139

136140
public subscript(_ index: Int) -> Element {
137-
if index == 0 {
138-
return singleElement!
141+
_read {
142+
if index == 0 {
143+
yield singleElement!
144+
} else {
145+
yield multipleElements[index - 1]
146+
}
147+
}
148+
_modify {
149+
if index == 0 {
150+
yield &singleElement!
151+
} else {
152+
yield &multipleElements[index - 1]
153+
}
139154
}
140-
return multipleElements[index - 1]
141155
}
142156

143157
public mutating func push(_ element: Element) {

0 commit comments

Comments
 (0)