Sub topic 2
Send and Sync are marker traits
This chapter assumes you know ownership and borrowing, trait bounds and generics, move closures, and basic use of std::thread::spawn.
What a marker trait is
A marker trait has no methods and no associated items. Implementing it adds no behavior. It records a fact about a type, and the compiler can check that fact wherever a trait bound asks for it.
trait Marker {}
struct Plain;
impl Marker for Plain {}
fn require<T: Marker>(_value: &T) {}
fn main() {
require(&Plain);
println!("Plain satisfies Marker");
}Marker has nothing to call. The bound T: Marker only works as a gate: the call compiles if Plain implements the trait and fails otherwise.
Send and Sync work the same way. Both live in std::marker, and the compiler uses them to decide which values may cross thread boundaries.
The two facts they record
| Trait | Fact it records | Informal meaning |
|---|---|---|
| `Send` | Ownership of a `T` can move to another thread. | `T` can be sent. |
| `Sync` | A `&T` can be shared with other threads. | `T` can be shared. |
Later chapters cover each trait in detail. For now, treat them as two separate permissions the compiler tracks for every type.
Two properties that set them apart
Both traits are unsafe traits. Implementing one by hand is a promise to the compiler that you have checked the thread-safety rules yourself, and the compiler cannot verify that promise. This is why a manual implementation needs the unsafe keyword.
Both are also auto traits. You rarely write impl Send for MyType. The compiler implements them for you when every field of your type implements them. A struct made only of i32 and String fields is Send and Sync with no code from you. A single field that opts out, such as an Rc, removes the trait from the whole struct.
Wait, replace the diagram below.
Send and Sync are marker traits
This chapter assumes you know ownership and borrowing, trait bounds and generics, move closures, and basic use of std::thread::spawn.
What a marker trait is
A marker trait has no methods and no associated items. Implementing it adds no behavior. It records a fact about a type, and the compiler can check that fact wherever a trait bound asks for it.
trait Marker {}
struct Plain;
impl Marker for Plain {}
fn require<T: Marker>(_value: &T) {}
fn main() {
require(&Plain);
println!("Plain satisfies Marker");
}Marker has nothing to call. The bound T: Marker works only as a gate. The call compiles if Plain implements the trait and fails otherwise.
Send and Sync work the same way. Both live in std::marker, and the compiler uses them to decide which values may cross thread boundaries.
The two facts they record
| Trait | Fact it records | Informal meaning |
|---|---|---|
| `Send` | Ownership of a `T` can move to another thread. | `T` can be sent. |
| `Sync` | A `&T` can be shared with other threads. | `T` can be shared. |
Later chapters cover each trait in detail. For now, treat them as two separate permissions that the compiler tracks for every type.
Two properties that set them apart
Both traits are unsafe traits. Implementing one by hand is a promise that you have checked the thread-safety rules yourself, because the compiler cannot verify that promise. This is why a manual implementation needs the unsafe keyword.
Both are also auto traits. You rarely write impl Send for MyType. The compiler implements them for you when every field of your type implements them. A struct made only of i32 and String fields is Send and Sync with no code from you. A single field that opts out, such as an Rc, removes the trait from the whole struct.
Where the bounds show up
You mostly meet these traits as bounds on functions that move work between threads. thread::spawn requires its closure to be Send, so everything the closure captures must be Send as well. The function below uses the same bounds.
use std::thread;
fn run_elsewhere<F>(f: F)
where
F: FnOnce() + Send + 'static,
{
thread::spawn(f).join().unwrap();
}
fn main() {
let name = String::from("worker");
run_elsewhere(move || println!("hello from {name}"));
}String is Send, so the closure that captures it is Send, and the program compiles. If the closure captured an Rc<String>, the compiler would reject the call at the Send bound. The program is rejected before it can run, so a data race cannot happen at runtime.
Exercise
Why can a struct with i32 and String fields be sent to another thread without any impl Send in your code? What happens if you add an Rc<i32> field?
Show answer
Send is an auto trait, so the compiler implements it for a struct when all of its fields are Send. i32 and String are both Send, so the struct is too. Adding an Rc<i32> field makes the struct not Send, because Rc is not Send. Passing such a struct to thread::spawn would then fail to compile.