Trait WriteApi

Source
pub trait WriteApi: GuardedTarget {
    type WriteGuard<'a>: DerefMut<Target = Self::Target>
       where Self: 'a;

    // Required method
    fn write(&mut self) -> Self::WriteGuard<'_>;
}
Expand description

Provides a mutable part of the RwApi interface.

§Example

use std::ops::Deref;
use parking_lot::RwLock;
use read_write_api::{RwApi, RwApiWrapper, RwApiWrapperOwned, WriteApi};

fn do_something(mut x: impl RwApi<Target=u64>) -> u64 {
    if *x.read() == 1 {
        *write_x(&mut x) = 2;
        *x.read()
    } else {
        *x.read()
    }
}

fn write_x<T: WriteApi>(x: &mut T) -> T::WriteGuard<'_> {
    x.write()
}

assert_eq!(do_something(RwApiWrapperOwned(1)), 2);
assert_eq!(do_something(RwApiWrapperOwned(3)), 3);
assert_eq!(do_something(&mut RwApiWrapperOwned(1)), 2);
assert_eq!(do_something(&mut RwApiWrapperOwned(3)), 3);

assert_eq!(do_something(RwLock::new(1)), 2);
assert_eq!(do_something(RwLock::new(3)), 3);
assert_eq!(do_something(&RwLock::new(1)), 2);
assert_eq!(do_something(&RwLock::new(3)), 3);
assert_eq!(do_something(&mut RwLock::new(1)), 2);
assert_eq!(do_something(&mut RwLock::new(3)), 3);

fn do_something_ref<'a>(mut x: impl RwApi<Target=&'a mut u64>) -> u64 {
    if **x.read() == 1 {
        **write_x(&mut x) = 2;
        **x.read()
    } else {
        **x.read()
    }
}

assert_eq!(do_something_ref(RwApiWrapper(&mut 1)), 2);
assert_eq!(do_something_ref(RwApiWrapper(&mut 3)), 3);
assert_eq!(do_something_ref(&mut RwApiWrapper(&mut 1)), 2);
assert_eq!(do_something_ref(&mut RwApiWrapper(&mut 3)), 3);

assert_eq!(do_something_ref(RwLock::new(&mut 1)), 2);
assert_eq!(do_something_ref(RwLock::new(&mut 3)), 3);
assert_eq!(do_something_ref(&RwLock::new(&mut 1)), 2);
assert_eq!(do_something_ref(&RwLock::new(&mut 3)), 3);
assert_eq!(do_something_ref(&mut RwLock::new(&mut 1)), 2);
assert_eq!(do_something_ref(&mut RwLock::new(&mut 3)), 3);

Required Associated Types§

Source

type WriteGuard<'a>: DerefMut<Target = Self::Target> where Self: 'a

Self::write return type.

Required Methods§

Source

fn write(&mut self) -> Self::WriteGuard<'_>

Generalizes RwLock::write.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> WriteApi for &RwLock<T>

Source§

type WriteGuard<'a> = RwLockWriteGuard<'a, RawRwLock, T> where Self: 'a

Source§

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

Source§

impl<T> WriteApi for &mut RwLock<T>

Source§

type WriteGuard<'a> = &'a mut T where Self: 'a

Source§

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

Source§

impl<T> WriteApi for RwLock<T>

Source§

type WriteGuard<'a> = &'a mut T where Self: 'a

Source§

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

Implementors§

Source§

impl<'a, T: ?Sized> WriteApi for &mut RwApiWrapper<'a, T>

Source§

type WriteGuard<'i> = &'i mut &'a mut T where Self: 'i

Source§

impl<'a, T: ?Sized> WriteApi for RwApiWrapper<'a, T>

Source§

type WriteGuard<'i> = &'i mut &'a mut T where Self: 'i

Source§

impl<T> WriteApi for &mut RwApiWrapperOwned<T>

Source§

type WriteGuard<'i> = &'i mut T where Self: 'i

Source§

impl<T> WriteApi for RwApiWrapperOwned<T>

Source§

type WriteGuard<'i> = &'i mut T where Self: 'i