swift-collections icon indicating copy to clipboard operation
swift-collections copied to clipboard

Add `subscript(unsafe:)` to `OrderedSet` to mutate `_elements` via `_modify` when the index is available and the uniqueness invariant upheld

Open b8591340 opened this issue 2 years ago • 0 comments

There’s seems to be no way to modify the _elements memory in place when we want to alter a property that does not partake in the Equatable and Hashable conformance. Specifically, I have a struct that’s compared and hashed only on the immutable id, so unless the struct itself is refactored to include other fields, no safety mishap appears to be possible.

if let index = set.firstIndex(of: element) {
    var element = set[index]
    element.nonHashedValue = …
    set.updateOrAppend(element)
}
if let index = set.firstIndex(of: element) {
    set[unsafe: index].nonHashedValue = …
}

b8591340 avatar Jun 18 '22 10:06 b8591340