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
impl !Sync for Args
impl !Sync for ArgsOs
impl Sync for GuardNoSend
impl Sync for AtomicBool
impl Sync for ZCoord
impl Sync for AtomicI8
impl Sync for AtomicI16
impl Sync for AtomicI32
impl Sync for AtomicI64
impl Sync for AtomicIsize
impl Sync for AtomicU8
impl Sync for AtomicU16
impl Sync for AtomicU32
impl Sync for AtomicU64
impl Sync for AtomicUsize
impl Sync for Waker
impl Sync for std::sync::once::Once
impl Sync for Registration
impl Sync for SetReadiness
impl Sync for Asn1BitString
impl Sync for Asn1BitStringRef
impl Sync for Asn1GeneralizedTime
impl Sync for Asn1GeneralizedTimeRef
impl Sync for Asn1Integer
impl Sync for Asn1IntegerRef
impl Sync for Asn1Object
impl Sync for Asn1ObjectRef
impl Sync for Asn1String
impl Sync for Asn1StringRef
impl Sync for Asn1Time
impl Sync for Asn1TimeRef
impl Sync for BigNum
impl Sync for BigNumContext
impl Sync for BigNumContextRef
impl Sync for BigNumRef
impl Sync for openssl::cipher::Cipher
impl Sync for CipherRef
impl Sync for CipherCtx
impl Sync for CipherCtxRef
impl Sync for CmsContentInfo
impl Sync for CmsContentInfoRef
impl Sync for Conf
impl Sync for ConfRef
impl Sync for EcGroup
impl Sync for EcGroupRef
impl Sync for EcPoint
impl Sync for EcPointRef
impl Sync for EcdsaSig
impl Sync for EcdsaSigRef
impl Sync for openssl::error::Error
impl Sync for Hasher
impl Sync for MessageDigest
impl Sync for Md
impl Sync for MdRef
impl Sync for MdCtx
impl Sync for MdCtxRef
impl Sync for OcspBasicResponse
impl Sync for OcspBasicResponseRef
impl Sync for OcspCertId
impl Sync for OcspCertIdRef
impl Sync for OcspOneReq
impl Sync for OcspOneReqRef
impl Sync for OcspRequest
impl Sync for OcspRequestRef
impl Sync for OcspResponse
impl Sync for OcspResponseRef
impl Sync for Pkcs7
impl Sync for Pkcs7Ref
impl Sync for Pkcs12
impl Sync for Pkcs12Ref
impl Sync for SrtpProtectionProfile
impl Sync for SrtpProtectionProfileRef
impl Sync for Ssl
impl Sync for SslContext
impl Sync for SslContextRef
impl Sync for SslMethod
impl Sync for SslRef
impl Sync for SslSession
impl Sync for SslSessionRef
impl Sync for OpensslString
impl Sync for OpensslStringRef
impl Sync for openssl::symm::Cipher
impl Sync for X509Store
impl Sync for X509StoreBuilder
impl Sync for X509StoreBuilderRef
impl Sync for X509StoreRef
impl Sync for AccessDescription
impl Sync for AccessDescriptionRef
impl Sync for GeneralName
impl Sync for GeneralNameRef
impl Sync for X509
impl Sync for X509Algorithm
impl Sync for X509AlgorithmRef
impl Sync for X509Extension
impl Sync for X509ExtensionRef
impl Sync for X509Name
impl Sync for X509NameEntry
impl Sync for X509NameEntryRef
impl Sync for X509NameRef
impl Sync for X509Object
impl Sync for X509ObjectRef
impl Sync for X509Ref
impl Sync for X509Req
impl Sync for X509ReqRef
impl Sync for X509StoreContext
impl Sync for X509StoreContextRef
impl Sync for X509VerifyParam
impl Sync for X509VerifyParamRef
impl Sync for AtomicWaker
impl Sync for Bytes
impl Sync for BytesMut
impl Sync for PollWatcher
impl Sync for Unparker
impl<'_> Sync for alloc::string::Drain<'_>
impl<'_> Sync for Scope<'_>
impl<'_, EntryData> Sync for IterMut<'_, EntryData> where
EntryData: Sync,
impl<'_, Fut> Sync for IterPinMut<'_, Fut> where
Fut: Sync,
impl<'_, Fut> Sync for IterPinRef<'_, Fut> where
Fut: Sync,
impl<'_, K, V> Sync for indexmap::map::core::raw::OccupiedEntry<'_, K, V> where
K: Sync,
V: Sync,
impl<'_, K, V, S, A> Sync for OccupiedEntry<'_, K, V, S, A> where
K: Sync,
V: Sync,
S: Sync,
A: Sync + Allocator + Clone,
impl<'_, K, V, S, A> Sync for RawOccupiedEntryMut<'_, K, V, S, A> where
K: Sync,
V: Sync,
S: Sync,
A: Sync + Allocator + Clone,
impl<'_, T> Sync for otter_api_tests::inventory::core::slice::Iter<'_, T> where
T: Sync,
impl<'_, T> Sync for otter_api_tests::inventory::core::slice::IterMut<'_, T> where
T: Sync,
impl<'_, T> Sync for alloc::collections::linked_list::Cursor<'_, T> where
T: Sync,
impl<'_, T> Sync for CursorMut<'_, T> where
T: Sync,
impl<'_, T> Sync for alloc::collections::linked_list::Iter<'_, T> where
T: Sync,
impl<'_, T> Sync for alloc::collections::linked_list::IterMut<'_, T> where
T: Sync,
impl<'_, T> Sync for alloc::collections::vec_deque::iter_mut::IterMut<'_, T> where
T: Sync,
impl<'_, T> Sync for std::sync::mutex::MutexGuard<'_, T> where
T: Sync + ?Sized,
impl<'_, T> Sync for std::sync::rwlock::RwLockReadGuard<'_, T> where
T: Sync + ?Sized,
impl<'_, T> Sync for std::sync::rwlock::RwLockWriteGuard<'_, T> where
T: Sync + ?Sized,
impl<'_, T> Sync for MutexGuard<'_, T> where
T: Send + Sync + ?Sized,
impl<'_, T> Sync for MutexGuard<'_, T> where
T: Sync + ?Sized,
impl<'_, T> Sync for MutexLockFuture<'_, T> where
T: ?Sized,
impl<'_, T> Sync for ReusableBoxFuture<'_, T>
impl<'_, T> Sync for RwLockMappedWriteGuard<'_, T> where
T: Send + Sync + ?Sized,
impl<'_, T> Sync for RwLockReadGuard<'_, T> where
T: Send + Sync + ?Sized,
impl<'_, T> Sync for RwLockWriteGuard<'_, T> where
T: Send + Sync + ?Sized,
impl<'_, T> Sync for ScopedJoinHandle<'_, T>
impl<'_, T> Sync for ShardedLockReadGuard<'_, T> where
T: Sync + ?Sized,
impl<'_, T> Sync for ShardedLockWriteGuard<'_, T> where
T: Sync + ?Sized,
impl<'_, T, A> Sync for alloc::collections::vec_deque::drain::Drain<'_, T, A> where
T: Sync,
A: Allocator + Sync,
impl<'_, T, A> Sync for alloc::vec::drain::Drain<'_, T, A> where
T: Sync,
A: Sync + Allocator,
impl<'_, T, A> Sync for RawDrain<'_, T, A> where
A: Allocator + Copy + Sync,
T: Sync,
impl<'_, T, U> Sync for MappedMutexGuard<'_, T, U> where
T: Sync + ?Sized,
U: Sync + ?Sized,
impl<'a> Sync for LioCb<'a>
impl<'a> Sync for IoSlice<'a>
impl<'a> Sync for IoSliceMut<'a>
impl<'a> Sync for Deriver<'a>
impl<'a> Sync for Decrypter<'a>
impl<'a> Sync for Encrypter<'a>
impl<'a> Sync for Signer<'a>
impl<'a> Sync for Verifier<'a>
impl<'a> Sync for CDict<'a>
impl<'a> Sync for DDict<'a>
impl<'a> Sync for Notified<'a>
impl<'a, 'b, K, Q, V, S, A> Sync for OccupiedEntryRef<'a, 'b, K, Q, V, S, A> where
K: Sync,
Q: Sync + ?Sized,
V: Sync,
S: Sync,
A: Sync + Allocator + Clone,
impl<'a, R, G, T> Sync for MappedReentrantMutexGuard<'a, R, G, T> where
R: 'a + RawMutex + Sync,
G: 'a + GetThreadId + Sync,
T: 'a + Sync + ?Sized,
impl<'a, R, G, T> Sync for ReentrantMutexGuard<'a, R, G, T> where
R: 'a + RawMutex + Sync,
G: 'a + GetThreadId + Sync,
T: 'a + Sync + ?Sized,
impl<'a, R, T> Sync for otter_api_tests::otter_support::crates::parking_lot::lock_api::MappedMutexGuard<'a, R, T> where
R: 'a + RawMutex + Sync,
T: 'a + Sync + ?Sized,
impl<'a, R, T> Sync for MappedRwLockReadGuard<'a, R, T> where
R: 'a + RawRwLock,
T: 'a + Sync + ?Sized,
impl<'a, R, T> Sync for MappedRwLockWriteGuard<'a, R, T> where
R: 'a + RawRwLock,
T: 'a + Sync + ?Sized,
impl<'a, R, T> Sync for otter_api_tests::otter_support::crates::parking_lot::lock_api::MutexGuard<'a, R, T> where
R: 'a + RawMutex + Sync,
T: 'a + Sync + ?Sized,
impl<'a, R, T> Sync for RwLockUpgradableReadGuard<'a, R, T> where
R: 'a + RawRwLockUpgrade,
T: 'a + Sync + ?Sized,
impl<'a, T> Sync for http::header::map::Drain<'a, T> where
T: Sync,
impl<'a, T> Sync for http::header::map::Iter<'a, T> where
T: Sync,
impl<'a, T> Sync for http::header::map::IterMut<'a, T> where
T: Sync,
impl<'a, T> Sync for ValueDrain<'a, T> where
T: Sync,
impl<'a, T> Sync for ValueIterMut<'a, T> where
T: Sync,
impl<'a, T> Sync for Drain<'a, T> where
T: Sync + Array,
impl<'a, T> Sync for MappedMutexGuard<'a, T> where
T: 'a + Sync + ?Sized,
impl<'a, T, const CAP: usize> Sync for otter_api_tests::otter_support::crates::otter_base::crates::arrayvec::Drain<'a, T, CAP> where
T: Sync,
impl<Dyn> Sync for DynMetadata<Dyn> where
Dyn: ?Sized,
impl<Fut> Sync for FuturesUnordered<Fut> where
Fut: Sync,
impl<Fut> Sync for IntoIter<Fut> where
Fut: Sync + Unpin,
impl<R, G> Sync for RawReentrantMutex<R, G> where
R: RawMutex + Sync,
G: GetThreadId + Sync,
impl<R, G, T> Sync for ReentrantMutex<R, G, T> where
R: RawMutex + Sync,
G: GetThreadId + Sync,
T: Send + ?Sized,
impl<R, T> Sync for otter_api_tests::otter_support::crates::parking_lot::lock_api::Mutex<R, T> where
R: RawMutex + Sync,
T: Send + ?Sized,
impl<R, T> Sync for otter_api_tests::otter_support::crates::parking_lot::lock_api::RwLock<R, T> where
R: RawRwLock + Sync,
T: Send + Sync + ?Sized,
impl<S> Sync for AllowStd<S> where
S: Sync,
impl<T> !Sync for *const T where
T: ?Sized,
impl<T> !Sync for *mut T where
T: ?Sized,
impl<T> !Sync for otter_api_tests::mpsc::Receiver<T>
impl<T> !Sync for otter_api_tests::mpsc::Sender<T>
impl<T> !Sync for RefCell<T> where
T: ?Sized,
impl<T> !Sync for Cell<T> where
T: ?Sized,
impl<T> !Sync for UnsafeCell<T> where
T: ?Sized,
impl<T> !Sync for Rc<T> where
T: ?Sized,
impl<T> !Sync for alloc::rc::Weak<T> where
T: ?Sized,
impl<T> !Sync for NonNull<T> where
T: ?Sized,
NonNull
pointers are not Sync
because the data they reference may be aliased.