repr_offset/
privacy.rs

1//! Type-level encoding of `enum Privacy { IsPublic, IsPrivate }`
2
3/// A marker type representing that a `FieldOffsetWithVis` is for a public field.
4#[derive(Debug, Copy, Clone)]
5pub struct IsPublic;
6
7/// A marker type representing that a `FieldOffsetWithVis` is for a private field.
8#[derive(Debug, Copy, Clone)]
9pub struct IsPrivate;
10
11mod sealed {
12    use super::{IsPrivate, IsPublic};
13    pub trait Sealed {}
14
15    impl Sealed for IsPublic {}
16    impl Sealed for IsPrivate {}
17}
18use self::sealed::Sealed;
19
20/// Marker trait for types that represents the privacy of a `FieldOffsetWithVis`.
21///
22/// This is only implemented by [`IsPublic`] and [`IsPrivate`]
23///
24/// [`IsPublic`]:  ./struct.IsPublic.html
25/// [`IsPrivate`]: ./struct.IsPrivate.html
26pub trait Privacy: Sealed {}
27
28impl Privacy for IsPublic {}
29impl Privacy for IsPrivate {}
30
31/// Combines two [`Privacy`] types.
32///
33/// This is used to compute the `Privacy` associated type of the `GetFieldOffset` trait in
34/// impls for accessing nested fields.
35///
36/// [`Privacy`]: ./trait.Privacy.html
37pub type CombinePrivacyOut<Lhs, Rhs> = <Lhs as CombinePrivacy<Rhs>>::Output;
38
39/// Trait that combines two [`Privacy`] types.
40///
41/// [`Privacy`]: ./trait.Privacy.html
42pub trait CombinePrivacy<Rhs: Privacy> {
43    /// This is [`IsPublic`] if both `Self` and the `Rhs` parameter are [`IsPublic`],
44    /// otherwise it is [`IsPrivate`].
45    ///
46    /// [`Privacy`]: ./trait.Privacy.html
47    /// [`IsPublic`]:  ./struct.IsPublic.html
48    /// [`IsPrivate`]: ./struct.IsPrivate.html
49    type Output: Privacy;
50}
51
52impl<A: Privacy> CombinePrivacy<A> for IsPublic {
53    type Output = A;
54}
55impl<A: Privacy> CombinePrivacy<A> for IsPrivate {
56    type Output = IsPrivate;
57}
58
59macro_rules! tuple_impls {
60    (small=> $ty:ty = $output:ty) => {
61        impl<Carry: Privacy> CombinePrivacy<Carry> for $ty {
62            type Output = $output;
63        }
64    };
65    (large=>
66        $( ($t0:ident,$t1:ident,$t2:ident,$t3:ident,), )*
67        $($trailing:ident,)*
68    )=>{
69        #[allow(non_camel_case_types)]
70        impl<A: Privacy, $($t0,$t1,$t2,$t3,)* $($trailing,)* CombTuples >
71            CombinePrivacy<A>
72        for ($($t0,$t1,$t2,$t3,)* $($trailing,)*)
73        where
74            ($($trailing,)*): CombinePrivacy<A>,
75            $( ($t0,$t1,$t2,$t3): CombinePrivacy<IsPublic>, )*
76            (
77                $( CombinePrivacyOut<($t0,$t1,$t2,$t3), IsPublic>, )*
78            ):CombinePrivacy<
79                CombinePrivacyOut<($($trailing,)*), A>,
80                Output = CombTuples,
81            >,
82            CombTuples: Privacy,
83        {
84            type Output = CombTuples;
85        }
86    };
87}
88
89impl_all_trait_for_tuples! {
90    macro = tuple_impls,
91    true = IsPublic,
92    false = IsPrivate,
93}