Sub topic 6
Auto traits: how the compiler derives Send and Sync
This chapter assumes you know trait bounds, generics, and the idea that Send allows moving a value to another thread and Sync allows sharing &T between threads.
You never write `impl Send`
You rarely see impl Send for MyType. Send and Sync are auto traits. The compiler implements them for a type automatically when every part of the type implements them.
The rule is structural:
- A struct, enum, tuple, or array is
Sendif all its fields areSend. - It is
Syncif all its fields areSync. - One field that lacks the trait removes it from the whole type.
Seeing the derivation
The helper functions below let you ask the compiler about a type. They have empty bodies and exist only for their bounds.
use std::rc::Rc;
struct Config {
name: String,
retries: u32,
}
struct Session {
id: u64,
shared: Rc<Config>,
}
struct Wrapper<T>(T);
fn assert_send<T: Send>() {}
fn assert_sync<T: Sync>() {}
fn main() {
assert_send::<Config>();
assert_sync::<Config>();
assert_send::<Wrapper<String>>();
// Would fail: Session contains an Rc, so it is neither Send nor Sync.
// assert_send::<Session>();
}Config holds a String and a u32, both Send and Sync, so Config is too. Session adds an Rc<Config>. That one field makes Session neither Send nor Sync, and you did not have to declare anything.
Generics get conditional implementations
For a generic type, the derivation depends on the type parameter. Wrapper<T> is Send exactly when T: Send. So Wrapper<String> passes and Wrapper<Rc<i32>> does not. You get the equivalent of impl<T: Send> Send for Wrapper<T> for free.
References follow the relation from the previous chapter. &T is Send only when T is Sync, and &mut T is Send when T is Send.
Where the opt-outs come from
Auto traits start from "yes" and need an explicit "no". The standard library supplies these negative cases:
- Raw pointers (
*const T,*mut T) are neitherSendnorSync. Rc<T>is neitherSendnorSync.Cell<T>andRefCell<T>are notSync.MutexGuard<T>is notSend.
Negative implementations such as impl<T> !Send for Rc<T> are unstable, and so is the auto trait keyword. On stable Rust you cannot define your own auto trait or write a negative impl. Your options are to contain a field that already opts out, or to use unsafe impl to opt in, which later chapters cover.
PhantomData<T> takes part in the derivation as if the struct owned a T. That makes it a way to control the result without storing data.
Common mistakes
- Assuming a trait object is thread-safe.
Box<dyn Trait>isSendonly if you writeBox<dyn Trait + Send>. - Adding one
Rcor raw pointer field to a large struct. The error then appears far away, where the struct is passed tothread::spawn.
Exercise
A struct Pair<T> { a: T, p: *const u8 } is declared. For which types T is Pair<T> Send, and why?
Show answer
For none. The raw pointer field *const u8 is not Send, and auto traits require every field to be Send. T does not matter. Only an explicit unsafe impl<T: Send> Send for Pair<T> {} could make it Send.