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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
macro_rules! declare_enabled_traits {
    (declare_index;($value:expr,$ty:ty);$which_impl0:ident,$which_impl1:ident $(,$rest:ident)* )=>{
        pub const $which_impl0:$ty=$value;
        pub const $which_impl1:$ty=$value << 1;
        
        declare_enabled_traits!{declare_index;($value << 2,$ty); $($rest),* }
    };
    (declare_index;($value:expr,$ty:ty);$which_impl:ident)=>{
        pub const $which_impl:$ty=$value;
    };
    (declare_index;($value:expr,$ty:ty);)=>{

    };

    (
        auto_traits[
            $($auto_trait:ident),* $(,)*
        ]

        regular_traits[
            $($regular_trait:ident),* $(,)*
        ]
    ) => (
        use std::fmt::{self,Debug,Display};

        use crate::{
            abi_stability::extra_checks::{
                TypeCheckerMut,ExtraChecks,
                ForExtraChecksImplementor,ExtraChecksError,
            },
            type_layout::TypeLayout,
            std_types::{RCow,RResult},
            StableAbi,
        };

        use core_extensions::strings::StringExt;

        #[allow(non_upper_case_globals)]
        pub mod auto_trait_mask{
            declare_enabled_traits!{declare_index;(1,u16); $($auto_trait),* }
        }

        #[allow(non_upper_case_globals)]
        pub mod regular_trait_mask{
            declare_enabled_traits!{declare_index;(1,u64); $($regular_trait),* }
        }


        #[repr(C)]
        #[derive(Copy,Clone,StableAbi)]
        pub struct EnabledTraits{
            pub auto_traits:u16,
            pub regular_traits:u64,
        }

        impl Debug for EnabledTraits{
            fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
                use self::debug_impl_details::{EnabledAutoTraits,EnabledRegularTraits};

                f.debug_struct("EnabledTraits")
                 .field("auto_traits_bits",&self.auto_traits)
                 .field("auto_traits",&EnabledAutoTraits{traits:self.auto_traits})
                 .field("regular_traits_bits",&self.regular_traits)
                 .field("regular_traits",&EnabledRegularTraits{traits:self.regular_traits})
                 .finish()
            }
        }

        impl Display for EnabledTraits{
            fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
                f.write_str("EnabledTraits\n")?;
                
                f.write_str("Auto traits:")?;
                if self.auto_traits==0 {
                    f.write_str("<no_traits>")?;
                }else{
                    $(
                        if (self.auto_traits&auto_trait_mask::$auto_trait)!=0 {
                            f.write_str(concat!(" ",stringify!($auto_trait)))?;
                        }
                    )*
                }
                writeln!(f,)?;

                f.write_str("Impld traits:")?;
                if self.regular_traits==0 {
                    f.write_str("<no_traits>")?;
                }else{
                    $(
                        if (self.regular_traits&regular_trait_mask::$regular_trait)!=0 {
                            f.write_str(concat!(" ",stringify!($regular_trait)))?;
                        }
                    )*
                }
                writeln!(f,)?;

                Ok(())
            }
        }


        impl ExtraChecks for EnabledTraits {
            fn type_layout(&self)->&'static TypeLayout{
                <Self as StableAbi>::LAYOUT
            }

            fn check_compatibility(
                &self,
                _layout_containing_self:&'static TypeLayout,
                layout_containing_other:&'static TypeLayout,
                checker:TypeCheckerMut<'_>,
            )->RResult<(), ExtraChecksError> {
                Self::downcast_with_layout(layout_containing_other,checker,|other,_|{
                    if self.auto_traits!=other.auto_traits {
                        Err(ImpldTraitsError{
                            kind:ImpldTraitsErrorKind::MismatchedAutoTraits,
                            expected:self.clone(),
                            found:other.clone(),
                        })
                    }else if (self.regular_traits&other.regular_traits)!=self.regular_traits {
                        Err(ImpldTraitsError{
                            kind:ImpldTraitsErrorKind::UnimpldTraits,
                            expected:self.clone(),
                            found:other.clone(),
                        })
                    }else{
                        Ok(())
                    }
                })
            }

            fn nested_type_layouts(&self)->RCow<'_,[&'static TypeLayout]>{
                RCow::from_slice(&[])
            }
        }

        mod debug_impl_details{
            use super::*;

            pub(super) struct EnabledAutoTraits{
                pub(super) traits:u16,
            }

            impl Debug for EnabledAutoTraits{
                fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
                    let mut ds=f.debug_set();
                    $(
                        if (self.traits&auto_trait_mask::$auto_trait)!=0 {
                            ds.entry(&stringify!( $auto_trait ));
                        }
                    )*
                    ds.finish()
                }
            }


            pub(super) struct EnabledRegularTraits{
                pub(super) traits:u64,
            }

            impl Debug for EnabledRegularTraits{
                fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{
                    let mut ds=f.debug_set();
                    $(
                        if (self.traits&regular_trait_mask::$regular_trait)!=0 {
                            ds.entry(&stringify!( $regular_trait ));
                        }
                    )*
                    ds.finish()
                }
            }
        }


        ////////////////////////////////////////////////////////////////////////


        #[derive(Debug,Clone)]
        pub struct ImpldTraitsError{
            kind:ImpldTraitsErrorKind,
            expected:EnabledTraits,
            found:EnabledTraits,
        }

        #[derive(Debug,Clone)]
        pub enum ImpldTraitsErrorKind{
            MismatchedAutoTraits,
            UnimpldTraits,
        }

        impl Display for ImpldTraitsError{
            fn fmt(&self,f:&mut fmt::Formatter<'_>)->fmt::Result{

                let msg=match self.kind {
                    ImpldTraitsErrorKind::MismatchedAutoTraits=>
                        "Expected auto traits to be exactly the same",
                    ImpldTraitsErrorKind::UnimpldTraits=>
                        "`Expected` does not contain a subset of the traits in`Found`",
                };
                f.write_str(msg)?;
                writeln!(f,)?;

                writeln!(f,"Expected:\n{}",self.expected.to_string().left_padder(4))?;
                writeln!(f,"Found:\n{}",self.found.to_string().left_padder(4))?;

                Ok(())
            }
        }

        impl std::error::Error for ImpldTraitsError{}

    )
}