I know that the compiler will not automatically implement Send
and Sync
for my type.
In fact, the compiler automatically implements Send
and Sync
for you only when it can determine that it is safe.
This small program:
use std::cell::Cell; use std::sync::atomic::AtomicUsize; fn ensure_sync<T: Sync>(_: T) {} struct Automatic(AtomicUsize); impl Automatic { fn new() -> Automatic { Automatic(AtomicUsize::new(0)) } } fn main() { ensure_sync(AtomicUsize::new(0)); ensure_sync(Automatic::new()); ensure_sync(Cell::new(0)); }
Only errors on the line Cell::new(0)
, Automatic
make up Sync
, because all its fields are Sync
.
As for Foo
, Rc
is neither Sync
nor Send
, so really the compiler will not implement it for you.
Could Foo
be Sync
?
I believe 1 so. Until another operation is added to the module that works with immutable links. Now or in the future.
Could Foo
be Send
?
I agree with your conclusion, but I think you skipped another method that modifies Cell
: drop
.
So you seem to have come up with the Sync
type, not Send
, using the basic Send
type, not Sync
. It may be my botanical meaning, I find it pretty funny :)
1 When working with unsafe
code, I'm never sure of anything. It's very easy to fool yourself into thinking that something is safe, simply because a tiny little thing avoided attention.
source share