-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why does impl Collect for Lock need Copy? #93
Comments
Some more context for where I came across this issue, in case that's helpful for answering my question: I'm trying to implement a runtime based on 2-word nodes, that looks something like this: #[repr(align(4))]
pub struct Node<'gc>(Lock<NodeInner<'gc>>); // Lock wanted for interior mutability
struct NodeInner<'gc>(Word<'gc>, Word<'gc>);
union Word {
word: usize, // also used to store type tag, with LSB = 1
ptr: Gc<'gc, Node<'gc>>, // managed node pointer
string: ManuallyDrop<ThinStr>, // owned string
int: isize, // by value
}
const _: () = assert!(mem::size_of::<Word>() == mem::size_of::<usize>());
unsafe impl<'gc> Collect for NodeInner<'gc> { .. }
impl<'gc> Drop for NodeInner<'gc> { .. } I use a bit-stealing scheme based on this Haskell runtime. The first word is usually used to hold a type tag, e.g., I need a custom I'm quite hesitant to implement Taking a step back, and asking a broader question: is there something I'm missing here? I know that this kind of (And I know that gc-arena's |
The main issue is of safety due to If we passed a I think making this change sound is exactly equivalent to having I just got back from a trip and I've had a lot of personal things happening in the last month or so, so I haven't been thinking about this for a while, @moulins should probably back me up on this (or tell me if I've gotten something wrong). |
That's my understanding as well :) @j-hui For your specific use-case, I'd suggest implementing #[repr(align(4))]
pub struct Node<'gc>(NodeInner<'gc>);
struct NodeInner<'gc> {
fst: Lock<Word<'gc>>,
snd: Lock<Word<'gc>>,
} Then you can implement |
Okay, one thing I forgot is that since the |
That's not actually true, |
Ah heck I forgot about |
Thanks so much for the really informative response and discussion. And sorry for not ACKing sooner!
For now, I've been using the "naive" but idiomatic solution based on regular
Though I understand how having both a |
No worries!
It might be pretty torturous to actually exploit, but the idea is that somewhere you have a field of type |
So I saw the comment here:
gc-arena/src/gc-arena/src/lock.rs
Lines 162 to 177 in c70f838
I was wondering why this
impl
needsT
to implementCopy
in the first place? I get that it is common forCell
contents (and thusLock
s) to implementCopy
, but I'm not sure why that is needed here forLock
to safely implementCollect
.The
trace()
method can still be implemented usingCell::as_ptr()
:The text was updated successfully, but these errors were encountered: