[][src]Struct drone_core::sync::RwLock

pub struct RwLock<T: ?Sized> { /* fields omitted */ }

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).

In comparison, a Mutex does not distinguish between readers or writers that acquire the lock, therefore blocking any threads waiting for the lock to become available. An RwLock will allow any number of readers to acquire the lock as long as a writer is not holding the lock.

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 content of the lock.

Methods

impl<T> RwLock<T>[src]

pub const fn new(data: T) -> Self[src]

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

Examples

use drone_core::sync::RwLock;

let lock = RwLock::new(5);

pub fn into_inner(self) -> T[src]

Consumes this RwLock, returning the underlying data.

Examples

use drone_core::sync::RwLock;

let lock = RwLock::new(String::new());
{
    let mut s = lock.try_write().unwrap();
    *s = "modified".to_owned();
}
assert_eq!(lock.into_inner(), "modified");

impl<T: ?Sized> RwLock<T>[src]

pub fn try_read(&self) -> Option<RwLockReadGuard<T>>[src]

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 provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.

Examples

use drone_core::sync::RwLock;

let lock = RwLock::new(1);

match lock.try_read() {
    Some(n) => assert_eq!(*n, 1),
    None => unreachable!(),
};

pub fn try_write(&self) -> Option<RwLockWriteGuard<T>>[src]

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 provide any guarantees with respect to the ordering of whether contentious readers or writers will acquire the lock first.

Examples

use drone_core::sync::RwLock;

let lock = RwLock::new(1);

let n = lock.try_read().unwrap();
assert_eq!(*n, 1);

assert!(lock.try_write().is_none());

pub fn get_mut(&mut self) -> &mut T[src]

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.

Examples

use drone_core::sync::RwLock;

let mut lock = RwLock::new(0);
*lock.get_mut() = 10;
assert_eq!(*lock.try_read().unwrap(), 10);

Trait Implementations

impl<T> From<T> for RwLock<T>[src]

fn from(data: T) -> Self[src]

Creates a new instance of an RwLock<T> which is unlocked. This is equivalent to RwLock::new.

impl<T: ?Sized + Debug> Debug for RwLock<T>[src]

impl<T: ?Sized + Send> Send for RwLock<T>[src]

impl<T: ?Sized + Send + Sync> Sync for RwLock<T>[src]

impl<T: ?Sized + Default> Default for RwLock<T>[src]

fn default() -> Self[src]

Creates a new RwLock<T>, with the Default value for T.

Auto Trait Implementations

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

Blanket Implementations

impl<T> From<!> for T[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]