The conversions started with MutableSpan.cells() because it has to be consuming. We do have consuming getters now, but it feels a bit awkward referencing a property on a type and it consuming the value which is why this is a function call. The only precedent for a consuming getter in Swift today (as far as I remember) is Optional.unsafelyUnwrapped which, in my opinion, should've been a function call to begin with. There is something to be said about the constCells property that could probably be a function call as well.
Better inferencing rules for BitwiseCopyable definitely sounds like a bug fix if types that are aren't correctly being inferred as such. @Andrew_Trick would you agree?
@unsafe is not a mandatory feature of Swift (which I believe it 100% should be), so it slipped my mind when writing this. The unsafe bit in my snippet is the try body(&$0.pointee) for what it's worth.
Cell guarantees address stability while under any read/write accesses. So calling a method like withUnsafeMutablePointer guarantees the address stability from the start of the method call until the end of the call. Cell itself can still be moved just like any other noncopyable type. The same is true for Mutex, it can still be moved! It's just that places where you'd typically use it prevent it from being moved i.e. globals, class ivars, etc.
There is a slight caveat to this too though because nesting a Cell in another type gives it that same guarantee which presents the following scenario:
struct Nested: ~Copyable {
let x: Cell<Int>
func something() {
// This is technically ok because 'Nested' is
// guaranteed to have a stable address, to which
// 'x' is a part of, until 'something'
// has finished.
let ptr = x.withUMP { $0 }
}
}
but the following would not be ok:
func foo() {
let x = Cell(123)
// NOT OK; DANGER
// 'x' can be moved!
let ptr = x.withUMP { $0 }
}
Hi folks, I've updated the proposal with the following changes:
Removed the Sendable conformance from Cell. It is still present on ConstCell because it can't be mutated through Swift, but could still have side effects. Let me know if this still makes sense or if it should be removed from this type as well.
Added the unsafe prefix to all of the @unsafe API we're introducing. unsafeBorrow on Cell and ConstCell and unsafeCells on Span.
Got rid of the .value property on Volatile in favor of read(), write(_:), and modify(_:) but constrained to the pointer types and integer types.
Added a section in the Alternatives Considered about Volatile's Value generic argument conforming to AtomicRepresentasble to expand the supported list of types that can be read from/written to memory. We would need to either sink the protocol or float Volatile into Synchronization (which we could do, but it's not really a Synchronization construct).
Discussed a potential UnsafeCell in the Alternatives Considered. The main argument for this type is to sever the unsafeBorrow API on Cell onto that type to prevent worrying about the law of exclusivity, but we already have this problem with the various withUnsafe*Pointer APIs on things like Array, Span, etc.
I think it’s reasonable to put it in Synchronization because it is related to dealing with memory that can be modified in ways outside the standard options available to value types.
Given the methods of Cell are likely to be inalienable, is there a semantic difference between set and replace(with:)? Why not have one method for this that always returns the previous value?
What is the upside of this conformance for ConstCell? Are there ever uses where you want to mutate the cell from another language and know reads are only from the isolation you stored it in to avoid a race? I think a programmer can always introduce @unchecked Sendable but can't really remove it for a type they don't control, so it might be better to assume less about the way this type will always be used. On the other hand, is it desirable to store ConstCells in Sendablestructs and final classes? I'm not sure I understand how this type would be used.
There's not really a semantic difference between the two besides being more clear with intent. We could drop the @discardableResult from replace if we had set.
ConstCell should almost never be written in Swift directly, however its main purpose is for C/C++ interop. Types coming into Swift may have ConstCell members or Span<ConstCell<T>> members and for those types it would be nice for them to be Sendable (especially when nesting some bridged type into your own types).