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<'_>

RwLock::write analogue.

Implementations on Foreign Types§

source§

impl<T> WriteApi for &RwLock<T>

§

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>

§

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

source§

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

source§

impl<T> WriteApi for RwLock<T>

§

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

source§

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

Implementors§

source§

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

§

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

source§

impl<'a, T> WriteApi for RwApiWrapper<'a, T>

§

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

source§

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

§

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

source§

impl<T> WriteApi for RwApiWrapperOwned<T>

§

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