async_ringbuf/wrap/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
mod cons;
mod prod;

use crate::rb::AsyncRbRef;
use ringbuf::{
    traits::{observer::DelegateObserver, Based},
    wrap::{direct::Direct, Wrap},
    Obs,
};

pub struct AsyncWrap<R: AsyncRbRef, const P: bool, const C: bool> {
    base: Option<Direct<R, P, C>>,
}

pub type AsyncProd<R> = AsyncWrap<R, true, false>;
pub type AsyncCons<R> = AsyncWrap<R, false, true>;

impl<R: AsyncRbRef, const P: bool, const C: bool> AsyncWrap<R, P, C> {
    pub unsafe fn new(rb: R) -> Self {
        Self {
            base: Some(Direct::new(rb)),
        }
    }

    pub fn observe(&self) -> Obs<R> {
        self.base().observe()
    }
}

impl<R: AsyncRbRef, const P: bool, const C: bool> Based for AsyncWrap<R, P, C> {
    type Base = Direct<R, P, C>;
    fn base(&self) -> &Self::Base {
        self.base.as_ref().unwrap()
    }
    fn base_mut(&mut self) -> &mut Self::Base {
        self.base.as_mut().unwrap()
    }
}

impl<R: AsyncRbRef, const P: bool, const C: bool> Wrap for AsyncWrap<R, P, C> {
    type RbRef = R;
    fn rb_ref(&self) -> &R {
        self.base().rb_ref()
    }
    fn into_rb_ref(self) -> R {
        self.base.unwrap().into_rb_ref()
    }
}

impl<R: AsyncRbRef, const P: bool, const C: bool> Unpin for AsyncWrap<R, P, C> {}

impl<R: AsyncRbRef, const P: bool, const C: bool> DelegateObserver for AsyncWrap<R, P, C> {}

impl<R: AsyncRbRef, const P: bool, const C: bool> AsRef<Self> for AsyncWrap<R, P, C> {
    fn as_ref(&self) -> &Self {
        self
    }
}
impl<R: AsyncRbRef, const P: bool, const C: bool> AsMut<Self> for AsyncWrap<R, P, C> {
    fn as_mut(&mut self) -> &mut Self {
        self
    }
}