pub struct RwLock<R, T>
where T: ?Sized,
{ /* private fields */ }
Expand description

A reader-writer lock

This type of lock allows a number of readers or at most one writer at any point in time. The write portion of this lock typically allows modification of the underlying data (exclusive access) and the read portion of this lock typically allows for read-only access (shared access).

The type parameter T represents the data that this lock protects. It is required that T satisfies Send to be shared across threads and Sync to allow concurrent access through readers. The RAII guards returned from the locking methods implement Deref (and DerefMut for the write methods) to allow access to the contained of the lock.

Implementations§

§

impl<R, T> RwLock<R, T>
where R: RawRwLock,

pub const fn new(val: T) -> RwLock<R, T>

Creates a new instance of an RwLock<T> which is unlocked.

pub fn into_inner(self) -> T

Consumes this RwLock, returning the underlying data.

§

impl<R, T> RwLock<R, T>

pub const fn const_new(raw_rwlock: R, val: T) -> RwLock<R, T>

Creates a new new instance of an RwLock<T> based on a pre-existing RawRwLock<T>.

This allows creating a RwLock<T> in a constant context on stable Rust.

§

impl<R, T> RwLock<R, T>
where R: RawRwLock, T: ?Sized,

pub fn read(&self) -> RwLockReadGuard<'_, R, T>

Locks this RwLock with shared read access, blocking the current thread until it can be acquired.

The calling thread will be blocked until there are no more writers which hold the lock. There may be other readers currently inside the lock when this method returns.

Note that attempts to recursively acquire a read lock on a RwLock when the current thread already holds one may result in a deadlock.

Returns an RAII guard which will release this thread’s shared access once it is dropped.

pub fn try_read(&self) -> Option<RwLockReadGuard<'_, R, T>>

Attempts to acquire this RwLock with shared read access.

If the access could not be granted at this time, then None is returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.

This function does not block.

pub fn write(&self) -> RwLockWriteGuard<'_, R, T>

Locks this RwLock with exclusive write access, blocking the current thread until it can be acquired.

This function will not return while other writers or other readers currently have access to the lock.

Returns an RAII guard which will drop the write access of this RwLock when dropped.

pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, R, T>>

Attempts to lock this RwLock with exclusive write access.

If the lock could not be acquired at this time, then None is returned. Otherwise, an RAII guard is returned which will release the lock when it is dropped.

This function does not block.

pub fn get_mut(&mut self) -> &mut T

Returns a mutable reference to the underlying data.

Since this call borrows the RwLock mutably, no actual locking needs to take place—the mutable borrow statically guarantees no locks exist.

pub fn is_locked(&self) -> bool

Checks whether this RwLock is currently locked in any way.

pub fn is_locked_exclusive(&self) -> bool

Check if this RwLock is currently exclusively locked.

pub unsafe fn force_unlock_read(&self)

Forcibly unlocks a read lock.

This is useful when combined with mem::forget to hold a lock without the need to maintain a RwLockReadGuard object alive, for example when dealing with FFI.

Safety

This method must only be called if the current thread logically owns a RwLockReadGuard but that guard has be discarded using mem::forget. Behavior is undefined if a rwlock is read-unlocked when not read-locked.

pub unsafe fn force_unlock_write(&self)

Forcibly unlocks a write lock.

This is useful when combined with mem::forget to hold a lock without the need to maintain a RwLockWriteGuard object alive, for example when dealing with FFI.

Safety

This method must only be called if the current thread logically owns a RwLockWriteGuard but that guard has be discarded using mem::forget. Behavior is undefined if a rwlock is write-unlocked when not write-locked.

pub unsafe fn raw(&self) -> &R

Returns the underlying raw reader-writer lock object.

Note that you will most likely need to import the RawRwLock trait from lock_api to be able to call functions on the raw reader-writer lock.

Safety

This method is unsafe because it allows unlocking a mutex while still holding a reference to a lock guard.

pub fn data_ptr(&self) -> *mut T

Returns a raw pointer to the underlying data.

This is useful when combined with mem::forget to hold a lock without the need to maintain a RwLockReadGuard or RwLockWriteGuard object alive, for example when dealing with FFI.

Safety

You must ensure that there are no data races when dereferencing the returned pointer, for example if the current thread logically owns a RwLockReadGuard or RwLockWriteGuard but that guard has been discarded using mem::forget.

§

impl<R, T> RwLock<R, T>
where R: RawRwLockFair, T: ?Sized,

pub unsafe fn force_unlock_read_fair(&self)

Forcibly unlocks a read lock using a fair unlock procotol.

This is useful when combined with mem::forget to hold a lock without the need to maintain a RwLockReadGuard object alive, for example when dealing with FFI.

Safety

This method must only be called if the current thread logically owns a RwLockReadGuard but that guard has be discarded using mem::forget. Behavior is undefined if a rwlock is read-unlocked when not read-locked.

pub unsafe fn force_unlock_write_fair(&self)

Forcibly unlocks a write lock using a fair unlock procotol.

This is useful when combined with mem::forget to hold a lock without the need to maintain a RwLockWriteGuard object alive, for example when dealing with FFI.

Safety

This method must only be called if the current thread logically owns a RwLockWriteGuard but that guard has be discarded using mem::forget. Behavior is undefined if a rwlock is write-unlocked when not write-locked.

§

impl<R, T> RwLock<R, T>
where R: RawRwLockTimed, T: ?Sized,

pub fn try_read_for( &self, timeout: <R as RawRwLockTimed>::Duration ) -> Option<RwLockReadGuard<'_, R, T>>

Attempts to acquire this RwLock with shared read access until a timeout is reached.

If the access could not be granted before the timeout expires, then None is returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.

pub fn try_read_until( &self, timeout: <R as RawRwLockTimed>::Instant ) -> Option<RwLockReadGuard<'_, R, T>>

Attempts to acquire this RwLock with shared read access until a timeout is reached.

If the access could not be granted before the timeout expires, then None is returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.

pub fn try_write_for( &self, timeout: <R as RawRwLockTimed>::Duration ) -> Option<RwLockWriteGuard<'_, R, T>>

Attempts to acquire this RwLock with exclusive write access until a timeout is reached.

If the access could not be granted before the timeout expires, then None is returned. Otherwise, an RAII guard is returned which will release the exclusive access when it is dropped.

pub fn try_write_until( &self, timeout: <R as RawRwLockTimed>::Instant ) -> Option<RwLockWriteGuard<'_, R, T>>

Attempts to acquire this RwLock with exclusive write access until a timeout is reached.

If the access could not be granted before the timeout expires, then None is returned. Otherwise, an RAII guard is returned which will release the exclusive access when it is dropped.

§

impl<R, T> RwLock<R, T>
where R: RawRwLockRecursive, T: ?Sized,

pub fn read_recursive(&self) -> RwLockReadGuard<'_, R, T>

Locks this RwLock with shared read access, blocking the current thread until it can be acquired.

The calling thread will be blocked until there are no more writers which hold the lock. There may be other readers currently inside the lock when this method returns.

Unlike read, this method is guaranteed to succeed without blocking if another read lock is held at the time of the call. This allows a thread to recursively lock a RwLock. However using this method can cause writers to starve since readers no longer block if a writer is waiting for the lock.

Returns an RAII guard which will release this thread’s shared access once it is dropped.

pub fn try_read_recursive(&self) -> Option<RwLockReadGuard<'_, R, T>>

Attempts to acquire this RwLock with shared read access.

If the access could not be granted at this time, then None is returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.

This method is guaranteed to succeed if another read lock is held at the time of the call. See the documentation for read_recursive for details.

This function does not block.

§

impl<R, T> RwLock<R, T>

pub fn try_read_recursive_for( &self, timeout: <R as RawRwLockTimed>::Duration ) -> Option<RwLockReadGuard<'_, R, T>>

Attempts to acquire this RwLock with shared read access until a timeout is reached.

If the access could not be granted before the timeout expires, then None is returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.

This method is guaranteed to succeed without blocking if another read lock is held at the time of the call. See the documentation for read_recursive for details.

pub fn try_read_recursive_until( &self, timeout: <R as RawRwLockTimed>::Instant ) -> Option<RwLockReadGuard<'_, R, T>>

Attempts to acquire this RwLock with shared read access until a timeout is reached.

If the access could not be granted before the timeout expires, then None is returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.

§

impl<R, T> RwLock<R, T>
where R: RawRwLockUpgrade, T: ?Sized,

pub fn upgradable_read(&self) -> RwLockUpgradableReadGuard<'_, R, T>

Locks this RwLock with upgradable read access, blocking the current thread until it can be acquired.

The calling thread will be blocked until there are no more writers or other upgradable reads which hold the lock. There may be other readers currently inside the lock when this method returns.

Returns an RAII guard which will release this thread’s shared access once it is dropped.

pub fn try_upgradable_read(&self) -> Option<RwLockUpgradableReadGuard<'_, R, T>>

Attempts to acquire this RwLock with upgradable read access.

If the access could not be granted at this time, then None is returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.

This function does not block.

§

impl<R, T> RwLock<R, T>

pub fn try_upgradable_read_for( &self, timeout: <R as RawRwLockTimed>::Duration ) -> Option<RwLockUpgradableReadGuard<'_, R, T>>

Attempts to acquire this RwLock with upgradable read access until a timeout is reached.

If the access could not be granted before the timeout expires, then None is returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.

pub fn try_upgradable_read_until( &self, timeout: <R as RawRwLockTimed>::Instant ) -> Option<RwLockUpgradableReadGuard<'_, R, T>>

Attempts to acquire this RwLock with upgradable read access until a timeout is reached.

If the access could not be granted before the timeout expires, then None is returned. Otherwise, an RAII guard is returned which will release the shared access when it is dropped.

Trait Implementations§

source§

impl<const N: usize> AsBufExtend for RwLock<RawRwLock, [u8; N]>

source§

fn extend_lock(&self) -> ExtendGuard<'_>

Obtain access to extend the underlying buffer. Warning: Depending on the underlying data type, each new ExtendGuard could be a new cursor… i.e. pulling a new extend lock could overwrite previous data.
source§

impl AsBufExtend for RwLock<RawRwLock, Box<[u8]>>

source§

fn extend_lock(&self) -> ExtendGuard<'_>

Obtain access to extend the underlying buffer. Warning: Depending on the underlying data type, each new ExtendGuard could be a new cursor… i.e. pulling a new extend lock could overwrite previous data.
source§

impl AsBufExtend for RwLock<RawRwLock, Vec<u8>>

source§

fn extend_lock(&self) -> ExtendGuard<'_>

Obtain access to extend the underlying buffer. Warning: Depending on the underlying data type, each new ExtendGuard could be a new cursor… i.e. pulling a new extend lock could overwrite previous data.
source§

impl<const N: usize> AsBufRead for RwLock<RawRwLock, [u8; N]>

source§

fn len(&self) -> usize

The length of this buffer.
source§

fn is_empty(&self) -> bool

Is this buffer empty?
source§

fn read_lock(&self) -> ReadGuard<'_>

Obtain read access to the underlying buffer.
source§

fn try_unwrap( self: Arc<RwLock<RawRwLock, [u8; N]>> ) -> Result<Box<[u8]>, BufRead>

Attempt to extract the inner contents of this buf without cloning. If this memory is locked or there are clones of this reference, the unwrap will fail, returning a BufRead instance.
source§

impl AsBufRead for RwLock<RawRwLock, Box<[u8]>>

source§

fn len(&self) -> usize

The length of this buffer.
source§

fn is_empty(&self) -> bool

Is this buffer empty?
source§

fn read_lock(&self) -> ReadGuard<'_>

Obtain read access to the underlying buffer.
source§

fn try_unwrap( self: Arc<RwLock<RawRwLock, Box<[u8]>>> ) -> Result<Box<[u8]>, BufRead>

Attempt to extract the inner contents of this buf without cloning. If this memory is locked or there are clones of this reference, the unwrap will fail, returning a BufRead instance.
source§

impl AsBufRead for RwLock<RawRwLock, Vec<u8>>

source§

fn len(&self) -> usize

The length of this buffer.
source§

fn is_empty(&self) -> bool

Is this buffer empty?
source§

fn read_lock(&self) -> ReadGuard<'_>

Obtain read access to the underlying buffer.
source§

fn try_unwrap( self: Arc<RwLock<RawRwLock, Vec<u8>>> ) -> Result<Box<[u8]>, BufRead>

Attempt to extract the inner contents of this buf without cloning. If this memory is locked or there are clones of this reference, the unwrap will fail, returning a BufRead instance.
source§

impl<const N: usize> AsBufReadSized<N> for RwLock<RawRwLock, [u8; N]>

source§

fn read_lock_sized(&self) -> ReadGuardSized<'_, N>

Obtain read access to the underlying buffer.
source§

fn into_read_unsized(self: Arc<RwLock<RawRwLock, [u8; N]>>) -> BufRead

Convert to an unsized BufRead instance without cloning internal data and without changing memory locking strategy.
source§

fn try_unwrap_sized( self: Arc<RwLock<RawRwLock, [u8; N]>> ) -> Result<[u8; N], BufReadSized<N>>

Attempt to extract the inner contents of this buf without cloning. If this memory is locked or there are clones of this reference, the unwrap will fail, returning a BufRead instance.
source§

impl<const N: usize> AsBufWrite for RwLock<RawRwLock, [u8; N]>

source§

fn write_lock(&self) -> WriteGuard<'_>

Obtain write access to the underlying buffer.
source§

fn into_read(self: Arc<RwLock<RawRwLock, [u8; N]>>) -> BufRead

Downgrade this to a read-only reference without cloning internal data and without changing memory locking strategy.
source§

fn into_extend(self: Arc<RwLock<RawRwLock, [u8; N]>>) -> BufExtend

Transform this buffer into an extendable type.
source§

impl AsBufWrite for RwLock<RawRwLock, Box<[u8]>>

source§

fn write_lock(&self) -> WriteGuard<'_>

Obtain write access to the underlying buffer.
source§

fn into_read(self: Arc<RwLock<RawRwLock, Box<[u8]>>>) -> BufRead

Downgrade this to a read-only reference without cloning internal data and without changing memory locking strategy.
source§

fn into_extend(self: Arc<RwLock<RawRwLock, Box<[u8]>>>) -> BufExtend

Transform this buffer into an extendable type.
source§

impl AsBufWrite for RwLock<RawRwLock, Vec<u8>>

source§

fn write_lock(&self) -> WriteGuard<'_>

Obtain write access to the underlying buffer.
source§

fn into_read(self: Arc<RwLock<RawRwLock, Vec<u8>>>) -> BufRead

Downgrade this to a read-only reference without cloning internal data and without changing memory locking strategy.
source§

fn into_extend(self: Arc<RwLock<RawRwLock, Vec<u8>>>) -> BufExtend

Transform this buffer into an extendable type.
source§

impl<const N: usize> AsBufWriteSized<N> for RwLock<RawRwLock, [u8; N]>

source§

fn write_lock_sized(&self) -> WriteGuardSized<'_, N>

Obtain write access to the underlying buffer.
source§

fn into_read_sized(self: Arc<RwLock<RawRwLock, [u8; N]>>) -> BufReadSized<N>

Downgrade this to a read-only reference without cloning internal data and without changing memory locking strategy.
source§

fn into_write_unsized(self: Arc<RwLock<RawRwLock, [u8; N]>>) -> BufWrite

Convert to an unsized BufWrite instance without cloning internal data and without changing memory locking strategy.
§

impl<R, T> Debug for RwLock<R, T>
where R: RawRwLock, T: Debug + ?Sized,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<R, T> Default for RwLock<R, T>
where R: RawRwLock, T: Default + ?Sized,

§

fn default() -> RwLock<R, T>

Returns the “default value” for a type. Read more
§

impl<R, T> From<T> for RwLock<R, T>
where R: RawRwLock,

§

fn from(t: T) -> RwLock<R, T>

Converts to this type from the input type.
§

impl<R, T> Send for RwLock<R, T>
where R: RawRwLock + Send, T: Send + ?Sized,

§

impl<R, T> Sync for RwLock<R, T>
where R: RawRwLock + Sync, T: Send + Sync + ?Sized,

Auto Trait Implementations§

§

impl<R, T> !RefUnwindSafe for RwLock<R, T>

§

impl<R, T: ?Sized> Unpin for RwLock<R, T>
where R: Unpin, T: Unpin,

§

impl<R, T: ?Sized> UnwindSafe for RwLock<R, T>
where R: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Any for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

§

fn type_name(&self) -> &'static str

§

impl<T> AnySync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<F, W, T, D> Deserialize<With<T, W>, D> for F
where W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

§

fn deserialize( &self, deserializer: &mut D ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromFd for T
where T: From<OwnedFd>,

§

fn from_fd(owned_fd: OwnedFd) -> T

👎Deprecated since 1.0.0: FromFd::from_fd is replaced by From<OwnedFd>::from
Constructs a new instance of Self from the given file descriptor. Read more
§

fn from_into_fd<Owned>(into_owned: Owned) -> Self
where Owned: Into<OwnedFd>, Self: Sized + From<OwnedFd>,

Constructs a new instance of Self from the given file descriptor converted from into_owned. Read more
§

impl<T> FromFilelike for T
where T: From<OwnedFd>,

§

fn from_filelike(owned: OwnedFd) -> T

Constructs a new instance of Self from the given filelike object. Read more
§

fn from_into_filelike<Owned>(owned: Owned) -> T
where Owned: IntoFilelike,

Constructs a new instance of Self from the given filelike object converted from into_owned. Read more
§

impl<T> FromSocketlike for T
where T: From<OwnedFd>,

§

fn from_socketlike(owned: OwnedFd) -> T

Constructs a new instance of Self from the given socketlike object.
§

fn from_into_socketlike<Owned>(owned: Owned) -> T
where Owned: IntoSocketlike,

Constructs a new instance of Self from the given socketlike object converted from into_owned.
§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcastable for T
where T: Any + Send + Sync + 'static,

§

fn upcast_any_ref(&self) -> &(dyn Any + 'static)

upcast ref
§

fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)

upcast mut ref
§

fn upcast_any_box(self: Box<T>) -> Box<dyn Any>

upcast boxed dyn
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more