pub trait ReadApi: GuardedTarget {
    type ReadGuard<'a>: Deref<Target = Self::Target>
       where Self: 'a;

    // Required method
    fn read(&self) -> Self::ReadGuard<'_>;
}
Expand description

Provides a constant part of the RwApi interface.

Example

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

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

fn read_x(x: &impl ReadApi<Target=u64>) -> u64 {
    *x.read()
}

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 {
        **x.write() = 2;
        read_x_ref(&x)
    } else {
        read_x_ref(&x)
    }
}

fn read_x_ref<T>(x: &impl ReadApi<Target=T>) -> u64
    where
        T: Deref<Target=u64>
{
    **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 ReadGuard<'a>: Deref<Target = Self::Target> where Self: 'a

Self::read return type.

Required Methods§

source

fn read(&self) -> Self::ReadGuard<'_>

RwLock::read analogue.

Implementations on Foreign Types§

source§

impl<T> ReadApi for RwLock<T>

§

type ReadGuard<'a> = RwLockReadGuard<'a, RawRwLock, T> where Self: 'a

source§

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

source§

impl<T> ReadApi for &RwLock<T>

§

type ReadGuard<'a> = RwLockReadGuard<'a, RawRwLock, T> where Self: 'a

source§

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

source§

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

§

type ReadGuard<'a> = RwLockReadGuard<'a, RawRwLock, T> where Self: 'a

source§

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

Implementors§

source§

impl<'a, T> ReadApi for &ReadApiWrapper<'a, T>

§

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

source§

impl<'a, T> ReadApi for &RwApiWrapper<'a, T>

§

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

source§

impl<'a, T> ReadApi for &mut ReadApiWrapper<'a, T>

§

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

source§

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

§

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

source§

impl<'a, T> ReadApi for ReadApiWrapper<'a, T>

§

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

source§

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

§

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

source§

impl<T> ReadApi for &RwApiWrapperOwned<T>

§

type ReadGuard<'i> = &'i T where Self: 'i

source§

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

§

type ReadGuard<'i> = &'i T where Self: 'i

source§

impl<T> ReadApi for RwApiWrapperOwned<T>

§

type ReadGuard<'i> = &'i T where Self: 'i