uazu / qcell

Statically-checked alternatives to RefCell and RwLock
Apache License 2.0
356 stars 22 forks source link

Cell type that uses address of cell owner as key #14

Closed uazu closed 2 years ago

uazu commented 3 years ago

Since Rust guarantees no dangling references or use-after-free in safe code, it should be possible to use the address of the cell-owner as the key to access the cells, storing the address as the key in the cell.

If the owner is moved, then it loses access to the cells (which would typically be a bug in the user's code). Also if the owner is dropped and another owner created in the same memory, it will gain access to all the cells previously owned by the old owner. But this doesn't cause soundness problems, because there is still just one owner at any one time. Also access to a cell requires both a pointer to the cell and also the owner's key. So it really doesn't cause any issues that some other code might get logical ownership as it can't get access unless it also has pointers to the cells.

SoniEx2 commented 3 years ago

You can get dangling references: Box::new(()). But a PCellOwner could just be assert-non-zero-size. Pin might also be relevant here.

uazu commented 3 years ago

Yes, ZSTs were in the back of my mind -- that's a good point. In any case I was thinking of making the owner contain a usize which was the owner's address when the first cell was created from it. The ownership would still be determined purely by the owner's current address, but the stored usize would be used to give a better diagnostic in the panic, e.g. "you moved the owner" or whatever. I think pinning can be left to the user. If they want to pin the structure which surrounds the owner to get that guarantee, and are willing to pay for the complexity, then that's up to them.

SoniEx2 commented 2 years ago

Is this still relevant? Looks like we have #25 now.

uazu commented 2 years ago

Yes, you're right. Thanks! I will close this as it is already implemented