Trait core::marker::Sync

1.0.0 · source · []
pub unsafe auto trait Sync { }
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.

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

NonNull pointers are not Sync because the data they reference may be aliased.

impl<T: ?Sized + Sync> Sync for ThinBox<T>

impl Sync for Drain<'_>

impl<T: Sync, A: Sync + Allocator> Sync for Drain<'_, T, A>

impl<T: Sync, A: Allocator + Sync> Sync for IntoIter<T, A>

impl<T: ?Sized + Sync + Send> Sync for Arc<T>

impl<T: ?Sized + Sync + Send> Sync for StaticArc<T>

impl Sync for CondVar

impl<L, I> Sync for Guard<'_, L, I>where
    L: Lock<I> + ?Sized,
    L::Inner: Sync,
    I: LockInfo,

impl<T: ?Sized + Send, L: Lock + ?Sized> Sync for LockedBy<T, L>

impl<T: ?Sized + Send> Sync for Mutex<T>

impl<T: ?Sized + Send> Sync for NoWaitLock<T>

impl<T: ?Sized + Send + Sync> Sync for RwSemaphore<T>

impl<L: Lock + Sync> Sync for SeqLock<L>where
    L::Inner: Sync,

impl<T: ?Sized + Send> Sync for Mutex<T>

impl<T: ?Sized + Send> Sync for SpinLock<T>

impl<T: ?Sized + Send> Sync for RawSpinLock<T>

impl<const N: usize> Sync for Registration<{ N }>

impl Sync for Device

impl<T: DriverOps> Sync for Registration<T>

impl<T: Chip> Sync for Registration<T>

impl<T: Operations> Sync for Registration<T>

impl Sync for Executor

impl<T: Operations> Sync for Registration<T>

impl<T: Filter> Sync for Registration<T>

impl Sync for TcpListener

impl Sync for TcpStream

impl<T: Sync + Send> Sync for Revocable<T>

impl<T: Sync + Send> Sync for AsyncRevocable<T>

impl Sync for Task

impl Sync for Queue

impl<A: Adapter + ?Sized> Sync for List<A>where
    A::EntryType: Sync,

impl<T: ?Sized> Sync for Links<T>

impl<T: SysctlStorage> Sync for Sysctl<T>

impl Sync for ThisModule

Auto implementors

impl Sync for Global

impl<T: ?Sized, A> Sync for Box<T, A>where
    A: Sync,
    T: Sync,

impl<'a, B: ?Sized> Sync for Cow<'a, B>where
    B: Sync,
    <B as ToOwned>::Owned: Sync,

impl Sync for String

impl<'a, T, F, A> Sync for DrainFilter<'a, T, F, A>where
    A: Sync,
    F: Sync,
    T: Sync,

impl<T, A> Sync for Vec<T, A>where
    A: Sync,
    T: Sync,

impl Sync for Error

impl Sync for CStr

impl Sync for CString

impl<'a, T> !Sync for ArcBorrow<'a, T>

impl<T: ?Sized> Sync for UniqueArc<T>where
    T: Send + Sync,

impl Sync for ReadLock

impl Sync for WriteLock

impl<'a, T: ?Sized> Sync for NoWaitLockGuard<'a, T>where
    T: Send,

impl !Sync for Guard

impl<F, T> Sync for Revocable<F, T>where
    <F as LockFactory>::LockedType<Inner<T>>: Sync,

impl<'a, F, T, I> Sync for RevocableGuard<'a, F, T, I>where
    T: Sync,

impl<'a, L> Sync for SeqLockReadGuard<'a, L>where
    L: Sync,
    <L as Lock<WriteLock>>::Inner: Sync,

impl Sync for Mode

impl<T, F> Sync for ScopeGuard<T, F>where
    F: Sync,
    T: Sync,

impl<T> !Sync for Opaque<T>

impl<T> Sync for Bit<T>where
    T: Sync,

impl Sync for True

impl Sync for False

impl<T> !Sync for ARef<T>

impl<L, R> Sync for Either<L, R>where
    L: Sync,
    R: Sync,

impl !Sync for Clk

impl !Sync for EnabledClk

impl !Sync for Credential

impl<T, U, V> Sync for Data<T, U, V>where
    T: Send,
    U: Send + Sync,
    V: Sync,

impl<T, U, const N: usize> Sync for IdArray<T, U, N>where
    U: Sync,
    <T as RawDeviceId>::RawType: Sync,

impl<'a, T, U> Sync for IdTable<'a, T, U>where
    U: Sync,
    <T as RawDeviceId>::RawType: Sync,

impl<T> Sync for Module<T>

impl !Sync for File

impl !Sync for PollTable

impl Sync for SeekFrom

impl !Sync for IoctlCommand

impl<'a> !Sync for Value<'a>

impl !Sync for Spec

impl<const N: usize, S> !Sync for SpecArray<N, S>

impl<'a, S> !Sync for SpecTable<'a, S>

impl<const N: usize> !Sync for ConstantArray<N>

impl<'a> !Sync for ConstantTable<'a>

impl Sync for Super

impl Sync for NeedsInit

impl Sync for NeedsRoot

impl Sync for SuperParams

impl<'a, T, S = NeedsInit> !Sync for NewSuperBlock<'a, T, S>

impl<T> !Sync for SuperBlock<T>

impl !Sync for INode

impl !Sync for DEntry

impl !Sync for Filename

impl<T> Sync for Module<T>where
    T: Sync,

impl !Sync for IrqData

impl Sync for ExtraResult

impl Sync for Type

impl !Sync for Descriptor

impl<H> !Sync for Registration<H>

impl<H> !Sync for ThreadedRegistration<H>

impl Sync for Return

impl<'a> !Sync for ChainedGuard<'a>

impl !Sync for Domain

impl<T: ?Sized> Sync for AutoStopHandle<T>

impl Sync for TcpListener

impl Sync for TcpStream

impl<'a> !Sync for Options<'a>

impl<T> Sync for Module<T>

impl !Sync for Area

impl Sync for Hook

impl Sync for Hook

impl Sync for Hook

impl Sync for Disposition

impl Sync for Family

impl !Sync for Device

impl !Sync for Namespace

impl !Sync for SkBuff

impl Sync for Ipv4Addr

impl Sync for Ipv6Addr

impl Sync for SocketAddr

impl<const ORDER: u32> !Sync for Pages<ORDER>

impl<'a, T> !Sync for RevocableGuard<'a, T>

impl<'a, T> Sync for AsyncRevocableGuard<'a, T>where
    T: Send + Sync,

impl<'a> !Sync for TaskRef<'a>

impl !Sync for Work

impl !Sync for BoxedQueue

impl<'a, G> !Sync for Cursor<'a, G>

impl<T> !Sync for Links<T>

impl<G> !Sync for List<G>

impl<'a, G> !Sync for CursorMut<'a, G>

impl<K, V> !Sync for RBTree<K, V>

impl<'a, K, V> !Sync for RBTreeIterator<'a, K, V>

impl<'a, K, V> !Sync for RBTreeIteratorMut<'a, K, V>

impl<K, V> !Sync for RBTreeNodeReservation<K, V>

impl<K, V> !Sync for RBTreeNode<K, V>

impl<'a, A> !Sync for Iterator<'a, A>

impl<'a, A> !Sync for Cursor<'a, A>

impl Sync for Resource

impl<const SIZE: usize> Sync for IoMem<SIZE>

impl !Sync for IovIter

impl Sync for DeviceId

impl<T> Sync for Adapter<T>where
    T: Sync,

impl !Sync for Device

impl !Sync for UserSlicePtr

impl<'a> Sync for KParamGuard<'a>

This documentation is an old archive. Please see https://rust.docs.kernel.org instead.