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
//! # Forwards a trait on references
//!
//! Combined with the `#[cglue_forward]` macro forward implementation will be generated on `Fwd`
//! type.  Whether `Fwd` implements the trait depends purely on whether the trait has
//! functions with mutable references or not.

use crate::trait_group::Opaquable;
use ::core::ops::{Deref, DerefMut};

#[repr(transparent)]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "abi_stable", derive(::abi_stable::StableAbi))]
pub struct Fwd<T>(pub T);

impl<T: Deref<Target = F>, F> Deref for Fwd<T> {
    type Target = F;

    fn deref(&self) -> &Self::Target {
        self.0.deref()
    }
}

impl<T: DerefMut + Deref<Target = F>, F> DerefMut for Fwd<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.0.deref_mut()
    }
}

pub trait Forward: Sized {
    fn forward(self) -> Fwd<Self> {
        Fwd(self)
    }
}

pub trait ForwardMut: Sized {
    fn forward_mut(self) -> Fwd<Self> {
        Fwd(self)
    }
}

impl<T: Deref> Forward for T {}
impl<T: DerefMut> ForwardMut for T {}

unsafe impl<T: Opaquable> Opaquable for Fwd<T> {
    type OpaqueTarget = Fwd<T::OpaqueTarget>;
}