Skip to content

Commit 69a613c

Browse files
committed
Rename and publicize stored properties
Make the collection's stored properties accessible to everyone. (Their mutabilities have not changed.) Adjust the names of those properties, for clarity.
1 parent e8f694e commit 69a613c

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

Sources/EmbeddedIntegerCollection/EmbeddedIntegerCollection.swift

+21-23
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ where
99
// be a multiple of Element.bitWidth.
1010

1111
/// The containing integer.
12-
@usableFromInline
13-
var word: Wrapped
14-
/// The sub-word to be treated as the first element.
15-
@usableFromInline
16-
let initialBitRange: EmbeddedIteratorDirection
12+
public var container: Wrapped
13+
/// Which sub-word is to be treated as the first element.
14+
public let endianness: EmbeddedIteratorDirection
1715

1816
/// Creates a collection vending elements of the given type embedded in
1917
/// the given value,
@@ -42,8 +40,8 @@ where
4240
within container: Wrapped = 0,
4341
iteratingFrom bitRange: EmbeddedIteratorDirection
4442
) {
45-
word = container
46-
initialBitRange = bitRange
43+
self.container = container
44+
endianness = bitRange
4745
}
4846

4947
/// Creates a collection initially vending the maximum amount copies of
@@ -121,8 +119,8 @@ extension EmbeddedIntegerCollection: CustomStringConvertible {
121119
extension EmbeddedIntegerCollection: CustomDebugStringConvertible {
122120
public var debugDescription: String {
123121
var result = String(describing: Self.self)
124-
let hexValue = String(word, radix: 16, uppercase: true)
125-
switch initialBitRange {
122+
let hexValue = String(container, radix: 16, uppercase: true)
123+
switch endianness {
126124
case .mostSignificantFirst:
127125
print("(*", hexValue, ")", separator: "", terminator: "", to: &result)
128126
case .leastSignificantFirst:
@@ -159,7 +157,7 @@ extension EmbeddedIntegerCollection: RandomAccessCollection, MutableCollection {
159157

160158
@inlinable
161159
public var startIndex: Index {
162-
switch initialBitRange {
160+
switch endianness {
163161
case .mostSignificantFirst:
164162
Element.bitWidth - Wrapped.bitWidth
165163
case .leastSignificantFirst:
@@ -168,7 +166,7 @@ extension EmbeddedIntegerCollection: RandomAccessCollection, MutableCollection {
168166
}
169167
@inlinable
170168
public var endIndex: Index {
171-
switch initialBitRange {
169+
switch endianness {
172170
case .mostSignificantFirst:
173171
Element.bitWidth
174172
case .leastSignificantFirst:
@@ -178,17 +176,17 @@ extension EmbeddedIntegerCollection: RandomAccessCollection, MutableCollection {
178176

179177
public subscript(position: Index) -> Element {
180178
get {
181-
return .init(truncatingIfNeeded: word >> abs(position))
179+
return .init(truncatingIfNeeded: container >> abs(position))
182180
}
183181
set {
184182
let flipMask = self[position] ^ newValue
185-
word ^= Wrapped(flipMask) << abs(position)
183+
container ^= Wrapped(flipMask) << abs(position)
186184
}
187185
}
188186

189187
public mutating func swapAt(_ i: Int, _ j: Int) {
190188
let flipMask = Wrapped(self[i] ^ self[j])
191-
word ^= flipMask << abs(i) | flipMask << abs(j)
189+
container ^= flipMask << abs(i) | flipMask << abs(j)
192190
}
193191

194192
@inlinable
@@ -239,25 +237,25 @@ extension EmbeddedIntegerCollection: RandomAccessCollection, MutableCollection {
239237
})
240238
else { return nil }
241239

242-
assert(copy.word == word) // Check against accidental mutation
240+
assert(copy.container == container) // Check against accidental mutation
243241
return result
244242
}
245243
public mutating func withContiguousMutableStorageIfAvailable<R>(
246244
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
247245
) rethrows -> R? {
248246
guard Element.self is UInt8.Type else { return nil }
249-
guard word is _ExpressibleByBuiltinIntegerLiteral else { return nil }
247+
guard container is _ExpressibleByBuiltinIntegerLiteral else { return nil }
250248

251249
var storage =
252-
switch initialBitRange {
250+
switch endianness {
253251
case .mostSignificantFirst:
254-
word.bigEndian
252+
container.bigEndian
255253
case .leastSignificantFirst:
256-
word.littleEndian
254+
container.littleEndian
257255
}
258256
defer {
259-
word =
260-
switch initialBitRange {
257+
container =
258+
switch endianness {
261259
case .mostSignificantFirst:
262260
Wrapped(bigEndian: storage)
263261
case .leastSignificantFirst:
@@ -440,8 +438,8 @@ extension EmbeddedIntegerCollection {
440438
// Fill up the wrapping word from the most-significant element down.
441439
var remainingElements = count
442440
while remainingElements > 0, let nextEmbeddedElement = iterator.next() {
443-
word <<= Element.bitWidth
444-
word |= Wrapped(nextEmbeddedElement)
441+
container <<= Element.bitWidth
442+
container |= Wrapped(nextEmbeddedElement)
445443
remainingElements -= 1
446444
}
447445
guard remainingElements == 0 else { return nil }

Tests/EmbeddedIntegerCollectionTests/EmbeddedIntegerCollectionTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -347,29 +347,29 @@ func mutateContiguousStorage() async throws {
347347
within: 0x0123_4567 as UInt32,
348348
iteratingFrom: .mostSignificantFirst
349349
)
350-
#expect(bigOctets.word == 0x0123_4567)
350+
#expect(bigOctets.container == 0x0123_4567)
351351
#expect(bigOctets.elementsEqual([0x01, 0x23, 0x45, 0x67]))
352352

353353
let bigOctetCount = bigOctets.withContiguousMutableStorageIfAvailable(
354354
flipAndCount(_:)
355355
)
356356
#expect(bigOctetCount == 4)
357-
#expect(bigOctets.word == 0xFEDC_BA98)
357+
#expect(bigOctets.container == 0xFEDC_BA98)
358358
#expect(bigOctets.elementsEqual([0xFE, 0xDC, 0xBA, 0x98]))
359359

360360
var littleOctets = EmbeddedIntegerCollection(
361361
embedding: UInt8.self,
362362
within: 0x0123_4567 as UInt32,
363363
iteratingFrom: .leastSignificantFirst
364364
)
365-
#expect(littleOctets.word == 0x0123_4567)
365+
#expect(littleOctets.container == 0x0123_4567)
366366
#expect(littleOctets.elementsEqual([0x67, 0x45, 0x23, 0x01]))
367367

368368
let littleOctetCount = littleOctets.withContiguousMutableStorageIfAvailable(
369369
flipAndCount(_:)
370370
)
371371
#expect(littleOctetCount == 4)
372-
#expect(littleOctets.word == 0xFEDC_BA98)
372+
#expect(littleOctets.container == 0xFEDC_BA98)
373373
#expect(littleOctets.elementsEqual([0x98, 0xBA, 0xDC, 0xFE]))
374374
}
375375

@@ -398,7 +398,7 @@ func inspectContiguousStorage(wrapped: UInt32, isBigEndian: Bool) async throws {
398398
return $0.map(\.nonzeroBitCount).reduce(0, +)
399399
}
400400
)
401-
#expect(bitsCount == collection.word.nonzeroBitCount)
401+
#expect(bitsCount == collection.container.nonzeroBitCount)
402402
}
403403

404404
@Test("Inspecting non-octet contiguous storage")

0 commit comments

Comments
 (0)