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§

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

Consumes this RwLock, returning the underlying data.

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.

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.

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.

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.

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.

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.

Checks whether this RwLock is currently locked in any way.

Check if this RwLock is currently exclusively locked.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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§

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.
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.
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.
The length of this buffer.
Is this buffer empty?
Obtain read access to the underlying buffer.
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.
The length of this buffer.
Is this buffer empty?
Obtain read access to the underlying buffer.
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.
The length of this buffer.
Is this buffer empty?
Obtain read access to the underlying buffer.
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.
Obtain read access to the underlying buffer.
Convert to an unsized BufRead instance without cloning internal data and without changing memory locking strategy.
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.
Obtain write access to the underlying buffer.
Downgrade this to a read-only reference without cloning internal data and without changing memory locking strategy.
Transform this buffer into an extendable type.
Obtain write access to the underlying buffer.
Downgrade this to a read-only reference without cloning internal data and without changing memory locking strategy.
Transform this buffer into an extendable type.
Obtain write access to the underlying buffer.
Downgrade this to a read-only reference without cloning internal data and without changing memory locking strategy.
Transform this buffer into an extendable type.
Obtain write access to the underlying buffer.
Downgrade this to a read-only reference without cloning internal data and without changing memory locking strategy.
Convert to an unsized BufWrite instance without cloning internal data and without changing memory locking strategy.
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
TODO: once 1.33.0 is the minimum supported compiler version, remove Any::type_id_compat and use StdAny::type_id instead. https://github.com/rust-lang/rust/issues/27745
The archived version of the pointer metadata for this type.
Converts some archived metadata to the pointer metadata for itself.
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Deserializes using the given deserializer
Converts to this type from the input type.

Returns the argument unchanged.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Attaches the current Context to this type, returning a WithContext wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The type for metadata in pointers and references to Self.
Should always be Self
The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Checks if self is actually part of its subset T (and can be converted to it).
Use with care! Same as self.to_subset but without any property checks. Always succeeds.
The inclusion map: converts self to the equivalent element of its superset.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
upcast ref
upcast mut ref
upcast boxed dyn
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more