pub unsafe auto trait Sync { }
std
only.Expand description
Types for which it is safe to share references between threads.
This trait is automatically implemented when the compiler determines it’s appropriate.
The precise definition is: a type T
is Sync
if and only if &T
is
Send
. In other words, if there is no possibility of
undefined behavior (including data races) when passing
&T
references between threads.
As one would expect, primitive types like u8
and f64
are all Sync
, and so are simple aggregate types containing them,
like tuples, structs and enums. More examples of basic Sync
types include “immutable” types like &T
, and those with simple
inherited mutability, such as Box<T>
, Vec<T>
and
most other collection types. (Generic parameters need to be Sync
for their container to be Sync
.)
A somewhat surprising consequence of the definition is that &mut T
is Sync
(if T
is Sync
) even though it seems like that might
provide unsynchronized mutation. The trick is that a mutable
reference behind a shared reference (that is, & &mut T
)
becomes read-only, as if it were a & &T
. Hence there is no risk
of a data race.
A shorter overview of how Sync
and Send
relate to referencing:
&T
isSend
if and only ifT
isSync
&mut T
isSend
if and only ifT
isSend
&T
and&mut T
areSync
if and only ifT
isSync
Types that are not Sync
are those that have “interior
mutability” in a non-thread-safe form, such as Cell
and RefCell
. These types allow for mutation of
their contents even through an immutable, shared reference. For
example the set
method on Cell<T>
takes &self
, so it requires
only a shared reference &Cell<T>
. The method performs no
synchronization, thus Cell
cannot be Sync
.
Another example of a non-Sync
type is the reference-counting
pointer Rc
. Given any reference &Rc<T>
, you can clone
a new Rc<T>
, modifying the reference counts in a non-atomic way.
For cases when one does need thread-safe interior mutability,
Rust provides atomic data types, as well as explicit locking via
sync::Mutex
and sync::RwLock
. These types
ensure that any mutation cannot cause data races, hence the types
are Sync
. Likewise, sync::Arc
provides a thread-safe
analogue of Rc
.
Any types with interior mutability must also use the
cell::UnsafeCell
wrapper around the value(s) which
can be mutated through a shared reference. Failing to doing this is
undefined behavior. For example, transmute
-ing
from &T
to &mut T
is invalid.
See the Nomicon for more details about Sync
.
Implementors§
impl !Sync for LocalWaker
impl !Sync for Arguments<'_>
impl !Sync for Args
impl !Sync for ArgsOs
impl Sync for ExampleEnumIntU8
doc
only.impl Sync for RawId
impl Sync for devela::_core::ffi::c_str::Bytes<'_>
impl Sync for devela::_core::sync::atomic::AtomicBool
impl Sync for devela::_core::sync::atomic::AtomicI8
impl Sync for devela::_core::sync::atomic::AtomicI16
impl Sync for devela::_core::sync::atomic::AtomicI32
impl Sync for devela::_core::sync::atomic::AtomicI64
impl Sync for devela::_core::sync::atomic::AtomicIsize
impl Sync for devela::_core::sync::atomic::AtomicU8
impl Sync for devela::_core::sync::atomic::AtomicU16
impl Sync for devela::_core::sync::atomic::AtomicU32
impl Sync for devela::_core::sync::atomic::AtomicU64
impl Sync for devela::_core::sync::atomic::AtomicUsize
impl Sync for devela::work::AtomicBool
impl Sync for Waker
impl Sync for devela::_dep::_alloc::string::Drain<'_>
impl Sync for PyDateTime_CAPI
impl Sync for PyBackedBytes
impl Sync for PyBackedStr
impl Sync for devela::_dep::rodio::cpal::platform::AlsaDevices
impl Sync for Bytes
impl Sync for BytesMut
impl Sync for Collector
impl Sync for GuardNoSend
impl Sync for Scope<'_>
impl Sync for Unparker
impl<'a> Sync for IoSlice<'a>
impl<'a> Sync for IoSliceMut<'a>
impl<'a, 'bump> Sync for devela::_dep::bumpalo::collections::string::Drain<'a, 'bump>
impl<'a, 'bump, T> Sync for devela::_dep::bumpalo::collections::vec::Drain<'a, 'bump, T>where
T: Sync,
impl<'a, R, G, T> Sync for MappedReentrantMutexGuard<'a, R, G, T>
impl<'a, R, G, T> Sync for ReentrantMutexGuard<'a, R, G, T>
impl<'a, R, T> Sync for MappedMutexGuard<'a, R, T>
impl<'a, R, T> Sync for MappedRwLockReadGuard<'a, R, T>
impl<'a, R, T> Sync for MappedRwLockWriteGuard<'a, R, T>
impl<'a, R, T> Sync for MutexGuard<'a, R, T>
impl<'a, R, T> Sync for RwLockUpgradableReadGuard<'a, R, T>
impl<'a, T> Sync for Drain<'a, T>where
T: Sync + Array,
impl<'a, T> Sync for OnceRef<'a, T>where
T: Sync,
impl<'a, T, const CAP: usize> Sync for arrayvec::arrayvec::Drain<'a, T, CAP>where
T: Sync,
impl<'bump, T> Sync for devela::_dep::bumpalo::collections::vec::IntoIter<'bump, T>where
T: Sync,
impl<Dyn> Sync for devela::_core::ptr::DynMetadata<Dyn>where
Dyn: ?Sized,
impl<Dyn> Sync for devela::_dep::rkyv::ptr_meta::DynMetadata<Dyn>where
Dyn: ?Sized,
impl<K, V, S, A> Sync for devela::_dep::hashbrown::hash_map::OccupiedEntry<'_, K, V, S, A>
impl<K, V, S, A> Sync for devela::_dep::hashbrown::hash_map::RawOccupiedEntryMut<'_, K, V, S, A>
impl<R, G> Sync for RawReentrantMutex<R, G>
impl<R, G, T> Sync for ReentrantMutex<R, G, T>
impl<R, T> Sync for Mutex<R, T>
impl<R, T> Sync for RwLock<R, T>
impl<R, T> Sync for RwLockReadGuard<'_, R, T>
impl<R, T> Sync for RwLockWriteGuard<'_, R, T>
impl<T> !Sync for *const Twhere
T: ?Sized,
impl<T> !Sync for *mut Twhere
T: ?Sized,
impl<T> !Sync for Cell<T>where
T: ?Sized,
impl<T> !Sync for OnceCell<T>
impl<T> !Sync for NonNull<T>where
T: ?Sized,
NonNull
pointers are not Sync
because the data they reference may be aliased.
impl<T> !Sync for RefCell<T>where
T: ?Sized,
impl<T> !Sync for UnsafeCell<T>where
T: ?Sized,
impl<T> !Sync for devela::_dep::_std::sync::mpsc::Receiver<T>
impl<T> Sync for SyncUnsafeCell<T>
impl<T> Sync for devela::_core::slice::ChunksExactMut<'_, T>where
T: Sync,
impl<T> Sync for devela::_core::slice::ChunksMut<'_, T>where
T: Sync,
impl<T> Sync for devela::_core::slice::Iter<'_, T>where
T: Sync,
impl<T> Sync for devela::_core::slice::IterMut<'_, T>where
T: Sync,
impl<T> Sync for devela::_core::slice::RChunksExactMut<'_, T>where
T: Sync,
impl<T> Sync for devela::_core::slice::RChunksMut<'_, T>where
T: Sync,
impl<T> Sync for devela::_core::sync::atomic::AtomicPtr<T>
impl<T> Sync for Exclusive<T>where
T: ?Sized,
impl<T> Sync for NonZero<T>where
T: ZeroablePrimitive + Sync,
impl<T> Sync for devela::work::Atomic<T>
impl<T> Sync for devela::work::Mutex<T>
impl<T> Sync for devela::work::MutexGuard<'_, T>
impl<T> Sync for OnceLock<T>
impl<T> Sync for devela::work::RwLock<T>
impl<T> Sync for devela::work::RwLockReadGuard<'_, T>
impl<T> Sync for devela::work::RwLockWriteGuard<'_, T>
impl<T> Sync for JoinHandle<T>
impl<T> Sync for ThinBox<T>
ThinBox<T>
is Sync
if T
is Sync
because the data is owned.
impl<T> Sync for devela::_dep::_alloc::collections::linked_list::Iter<'_, T>where
T: Sync,
impl<T> Sync for devela::_dep::_alloc::collections::linked_list::IterMut<'_, T>where
T: Sync,
impl<T> Sync for PyBuffer<T>
impl<T> Sync for Py<T>
impl<T> Sync for GILOnceCell<T>
impl<T> Sync for GILProtected<T>where
T: Send,
impl<T> Sync for SerVec<T>where
T: Sync,
impl<T> Sync for ReadHalf<T>where
T: Sync,
impl<T> Sync for WriteHalf<T>where
T: Sync,
impl<T> Sync for devela::_dep::_std::sync::mpmc::Receiver<T>where
T: Send,
impl<T> Sync for devela::_dep::_std::sync::mpmc::Sender<T>where
T: Send,
impl<T> Sync for devela::_dep::_std::sync::mpsc::Sender<T>where
T: Send,
impl<T> Sync for devela::_dep::_std::sync::MappedMutexGuard<'_, T>
impl<T> Sync for devela::_dep::_std::sync::MappedRwLockReadGuard<'_, T>
impl<T> Sync for devela::_dep::_std::sync::MappedRwLockWriteGuard<'_, T>
impl<T> Sync for ReentrantLock<T>
impl<T> Sync for ReentrantLockGuard<'_, T>
impl<T> Sync for Atomic<T>
impl<T> Sync for AtomicCell<T>where
T: Send,
impl<T> Sync for CachePadded<T>where
T: Sync,
impl<T> Sync for Channel<T>where
T: Send,
impl<T> Sync for Injector<T>where
T: Send,
impl<T> Sync for OnceBox<T>
impl<T> Sync for ScopedJoinHandle<'_, T>
impl<T> Sync for ShardedLock<T>
impl<T> Sync for ShardedLockReadGuard<'_, T>
impl<T> Sync for ShardedLockWriteGuard<'_, T>
impl<T> Sync for Stealer<T>where
T: Send,
impl<T, A> !Sync for Rc<T, A>
impl<T, A> !Sync for devela::all::RcWeak<T, A>
impl<T, A> !Sync for UniqueRc<T, A>
impl<T, A> Sync for LinkedList<T, A>
impl<T, A> Sync for Arc<T, A>
impl<T, A> Sync for devela::work::ArcWeak<T, A>
impl<T, A> Sync for devela::_dep::_alloc::collections::linked_list::Cursor<'_, T, A>
impl<T, A> Sync for devela::_dep::_alloc::collections::linked_list::CursorMut<'_, T, A>
impl<T, A> Sync for devela::_dep::_alloc::collections::vec_deque::Drain<'_, T, A>
impl<T, A> Sync for devela::_dep::_alloc::vec::Drain<'_, T, A>
impl<T, A> Sync for devela::_dep::_alloc::vec::IntoIter<T, A>
impl<T, A> Sync for devela::_dep::hashbrown::hash_table::OccupiedEntry<'_, T, A>
impl<T, F> Sync for LazyLock<T, F>
impl<T, F> Sync for Lazy<T, F>
impl<T, F, S> Sync for ScopeGuard<T, F, S>
impl<T, const N: usize> Sync for InlineVec<T, N>where
T: Sync,
impl<T: Sync> Sync for BareBox<T>
unsafe_sync
only.impl<T: Sync> Sync for CacheAlign<T>
unsafe_sync
only.