In Rust, it's uncommon but possible to find scenarios where &T is Send but T is not Sync. This can occur when T employs interior mutability—a pattern that allows mutation of a value even when the value is accessed through an immutable reference. The canonical examples of types offering interior mutability in Rust are Cell and RefCell.
Here's an explanation of how this dichotomy can happen:
-
Cell<T> and RefCell<T>: These types use interior mutability, which allows changes to their contents even when they are accessed through immutable references. It's possible for &Cell<T> or &RefCell<T> to be Send because you can safely send a reference to another thread as long as you ensure it's used properly (e.g., not accessed concurrently). However, since Cell<T> and RefCell<T> allow for mutable access without requiring mutable references, they are not Sync because it's not safe for multiple threads to interact with the same instance concurrently—this could lead to data races.
Example: If T inside Cell<T> or RefCell<T> is Copy (and hence Send when not borrowed), a &Cell<T> can be sent across threads safely, assuming you coordinate the access properly.
-
Custom types with unsafe code: You can construct your own types that use unsafe code to mutate their state even when accessed through an immutable reference. If such a type ensures that it won't cause undefined behavior when a reference is sent to another thread—but it also doesn't protect against concurrent access—it could be that &T is Send but T is not Sync.
These cases require caution—the Rust language and the standard library take care to ensure that Sync and Send are implemented only for types that uphold the safety guarantees required for multithreading. If you create types that subvert these guarantees using unsafe code, you assume the responsibility for ensuring that your code upholds Rust's safety invariants.
Keep in mind that the standard library types that use interior mutability, such as Cell<T> and RefCell<T>, do not implement Send for &T unless T itself is Copy (which implies that it is also Send).
Here's a small code snippet that illustrates that Cell<T> is not Sync but a shared reference &Cell<T> can be Send if T is Copy:
use std::cell::Cell;
use std::thread;
// Assuming `T` is `i32`, which is `Copy`.
let cell = Cell::new(42);
// This is safe because `i32` is `Copy`, hence `Send`.
let cell_ref = &cell;
// We can send `cell_ref` to another thread safely.
thread::spawn(move || {
let value = cell_ref.get(); // Accessing the value is safe from this thread.
// ...
}).join().unwrap();
// However, `Cell<T>` is not `Sync`, which is why the following line would not compile:
// thread::spawn(move || {
// let c = &cell; // This would not compile because `Cell<T>` is not `Sync`.
// // ...
// }).join().unwrap();
In this example, Cell<i32> can have &Cell<i32> sent to another thread because i32 is Copy, meaning there are no issues with it being moved across threads (Send). However, Cell<T> itself is not Sync because it allows mutation through &Cell<T>, and there's no inherent synchronization mechanism to protect from concurrent mutations.