pub unsafe auto trait Sync { }
Expand description
core
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 FltkError
impl Sync for RawId
impl Sync for devela::_dep::sdl2::event::Event
This does not auto-derive because User
’s data
fields can be used to
store pointers to types that are !Sync
. Dereferencing these as pointers
requires using unsafe
and ensuring your own safety guarantees.
impl Sync for devela::_dep::sdl3::event::Event
This does not auto-derive because User
’s data
fields can be used to
store pointers to types that are !Sync
. Dereferencing these as pointers
requires using unsafe
and ensuring your own safety guarantees.
impl Sync for ExampleEnumIntU8
doc
or test
only.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::_dep::_alloc::string::Drain<'_>
impl Sync for Browser
impl Sync for CheckBrowser
impl Sync for FileBrowser
impl Sync for HoldBrowser
impl Sync for MultiBrowser
impl Sync for SelectBrowser
impl Sync for devela::_dep::fltk::button::Button
impl Sync for CheckButton
impl Sync for LightButton
impl Sync for RadioButton
impl Sync for RadioLightButton
impl Sync for RadioRoundButton
impl Sync for RepeatButton
impl Sync for ReturnButton
impl Sync for RoundButton
impl Sync for ShortcutButton
impl Sync for ToggleButton
impl Sync for Offscreen
impl Sync for devela::_dep::fltk::frame::Frame
impl Sync for ColorChooser
impl Sync for Flex
impl Sync for Grid
impl Sync for devela::_dep::fltk::group::Group
impl Sync for Pack
impl Sync for Scroll
impl Sync for Tabs
impl Sync for Tile
impl Sync for Wizard
impl Sync for AnimGifImage
impl Sync for BmpImage
impl Sync for GifImage
impl Sync for IcoImage
impl Sync for Image
impl Sync for JpegImage
impl Sync for Pixmap
impl Sync for PngImage
impl Sync for PnmImage
impl Sync for RgbImage
impl Sync for SvgImage
impl Sync for TiledImage
impl Sync for XbmImage
impl Sync for XpmImage
impl Sync for FileInput
impl Sync for FloatInput
impl Sync for Input
impl Sync for IntInput
impl Sync for MultilineInput
impl Sync for SecretInput
impl Sync for Choice
impl Sync for MenuBar
impl Sync for MenuButton
impl Sync for MenuItem
impl Sync for SysMenuBar
impl Sync for Chart
impl Sync for Clock
impl Sync for HelpView
impl Sync for InputChoice
impl Sync for Progress
impl Sync for Spinner
impl Sync for MultilineOutput
impl Sync for devela::_dep::fltk::output::Output
impl Sync for devela::_dep::fltk::table::Table
impl Sync for TableRow
impl Sync for Terminal
impl Sync for SimpleTerminal
impl Sync for TextBuffer
impl Sync for TextDisplay
impl Sync for TextEditor
impl Sync for Tree
impl Sync for TreeItem
impl Sync for Adjuster
impl Sync for Counter
impl Sync for Dial
impl Sync for FillDial
impl Sync for FillSlider
impl Sync for HorFillSlider
impl Sync for HorNiceSlider
impl Sync for HorSlider
impl Sync for HorValueSlider
impl Sync for LineDial
impl Sync for NiceSlider
impl Sync for Roller
impl Sync for Scrollbar
impl Sync for Slider
impl Sync for ValueInput
impl Sync for ValueOutput
impl Sync for ValueSlider
impl Sync for Widget
impl Sync for WidgetTracker
impl Sync for DoubleWindow
impl Sync for MenuWindow
impl Sync for OverlayWindow
impl Sync for SingleWindow
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 devela::_dep::sdl2::EventSubsystem
impl Sync for devela::_dep::sdl2::SensorSubsystem
impl Sync for TimerSubsystem
impl Sync for devela::_dep::sdl3::EventSubsystem
impl Sync for AbortHandle
impl Sync for devela::all::AtomicBool
impl Sync for Waker
impl Sync for Bytes
impl Sync for BytesMut
impl Sync for Collector
impl Sync for GuardNoSend
impl Sync for Scope<'_>
impl Sync for Select<'_>
impl Sync for Unparker
impl<'a> Sync for Notified<'a>
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 devela::_dep::tokio::sync::MappedMutexGuard<'a, T>
impl<'a, T> Sync for devela::_dep::ureq::http::header::Drain<'a, T>where
T: Sync,
impl<'a, T> Sync for devela::_dep::ureq::http::header::Iter<'a, T>where
T: Sync,
impl<'a, T> Sync for devela::_dep::ureq::http::header::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 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 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 devela::_dep::_std::sync::mpsc::Receiver<T>
impl<T> !Sync for Cell<T>where
T: ?Sized,
impl<T> !Sync for devela::all::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 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 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 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 devela::_dep::fltk::app::Receiver<T>
impl<T> Sync for devela::_dep::fltk::app::Sender<T>
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 ReadHalf<T>where
T: Sync,
impl<T> Sync for WriteHalf<T>where
T: Sync,
impl<T> Sync for devela::_dep::tokio::sync::broadcast::Receiver<T>where
T: Send,
impl<T> Sync for devela::_dep::tokio::sync::broadcast::Sender<T>where
T: Send,
impl<T> Sync for devela::_dep::tokio::sync::Mutex<T>
impl<T> Sync for devela::_dep::tokio::sync::MutexGuard<'_, T>
impl<T> Sync for devela::_dep::tokio::sync::OnceCell<T>
impl<T> Sync for OwnedMutexGuard<T>
impl<T> Sync for OwnedRwLockWriteGuard<T>
impl<T> Sync for devela::_dep::tokio::sync::RwLock<T>
impl<T> Sync for RwLockMappedWriteGuard<'_, T>
impl<T> Sync for devela::_dep::tokio::sync::RwLockReadGuard<'_, T>
impl<T> Sync for devela::_dep::tokio::sync::RwLockWriteGuard<'_, T>
impl<T> Sync for devela::_dep::tokio::task::JoinHandle<T>where
T: Send,
impl<T> Sync for devela::all::Arc<T>
impl<T> Sync for devela::all::ArcWeak<T>
impl<T> Sync for devela::all::Atomic<T>
impl<T> Sync for devela::all::Mutex<T>
T
must be Send
for Mutex
to be Sync
.
This ensures that the protected data can be accessed safely from multiple threads
without causing data races or other unsafe behavior.
Mutex<T>
provides mutable access to T
to one thread at a time. However, it’s essential
for T
to be Send
because it’s not safe for non-Send
structures to be accessed in
this manner. For instance, consider Rc
, a non-atomic reference counted smart pointer,
which is not Send
. With Rc
, we can have multiple copies pointing to the same heap
allocation with a non-atomic reference count. If we were to use Mutex<Rc<_>>
, it would
only protect one instance of Rc
from shared access, leaving other copies vulnerable
to potential data races.
Also note that it is not necessary for T
to be Sync
as &T
is only made available
to one thread at a time if T
is not Sync
.
impl<T> Sync for devela::all::MutexGuard<'_, T>
T
must be Sync
for a MutexGuard<T>
to be Sync
because it is possible to get a &T
from &MutexGuard
(via Deref
).
impl<T> Sync for NonZero<T>where
T: ZeroablePrimitive + Sync,
impl<T> Sync for OnceLock<T>
impl<T> Sync for devela::all::RwLock<T>
impl<T> Sync for devela::all::RwLockReadGuard<'_, T>
impl<T> Sync for devela::all::RwLockWriteGuard<'_, T>
impl<T> Sync for devela::all::ThreadJoinHandle<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 Receiver<T>where
T: Send,
impl<T> Sync for ScopedJoinHandle<'_, T>
impl<T> Sync for Sender<T>where
T: Send,
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 UniqueRc<T, A>
impl<T, A> !Sync for Rc<T, A>
impl<T, A> !Sync for devela::all::RcWeak<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::sync::Arc<T, A>
impl<T, A> Sync for devela::_dep::_alloc::sync::Weak<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, A> Sync for LinkedList<T, A>
impl<T, F> Sync for Lazy<T, F>
impl<T, F> Sync for LazyLock<T, F>
impl<T, F, S> Sync for scopeguard::ScopeGuard<T, F, S>
impl<T, U> Sync for OwnedMappedMutexGuard<T, U>
impl<T, U> Sync for OwnedRwLockMappedWriteGuard<T, U>
impl<T, U> Sync for OwnedRwLockReadGuard<T, U>
impl<T: Sync> Sync for BareBox<T>
unsafe_sync
only.impl<T: Sync> Sync for CacheAlign<T>
unsafe_sync
only.