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
/// Conveniance macro for creating annotation types combining several annotations
#[macro_export]
macro_rules! annotation {
    {
        $(#[$outer:meta])*
        $pub:vis struct $struct_name:ident $( < $( $param:ident ),* > )*
        {
            $( $ann_key:ident : $ann_type:ty ),* $( , )?
        }
        $( where $( $whereclause:tt )* )?

    } => {

        use std::borrow::Borrow as __Borrow;
        use $crate::annotations::ErasedAnnotation as __ErasedAnnotation;
        use $crate::annotations::Combine as __Combine;
        use $crate::{
            Content as __Content,
            Sink as __Sink,
            Source as __Source,
            ByteHash as __ByteHash
        };

        $(#[$outer])*
        $pub struct $struct_name $( < $( $param ),* > )* {
            $ ( $ann_key : $ann_type ),*
        }

        impl<__T, $( $( $param ),* )* > From<__T> for $struct_name $( < $( $param ),* > )*
        where
            __T: Clone,
            $( $ann_type : From<__T> ),*
            $( , $( $whereclause )* )?

        {
            fn from(t: __T) -> Self {
                $struct_name {
                    $( $ann_key : t.clone().into() ),*
                }
            }
        }

        impl<__H, $( $( $param ),* )* > __Content<__H> for $struct_name $( < $( $param ),* > )*
        where
            __H: __ByteHash,
            $( $ann_type : __Content<__H> ),*
            $( , $( $whereclause )* )?

        {
            fn persist(&mut self, sink: &mut __Sink<__H>) -> io::Result<()> {
                $( self.$ann_key.persist(sink)? ; )*
                Ok(())
            }

            fn restore(source: &mut __Source<__H>) -> io::Result<Self> {
                Ok($struct_name {
                    $( $ann_key : < $ann_type as __Content<__H> >::restore(source)? , )*
                })

            }
        }

        impl<$( $( $param ),* )* > Clone for $struct_name $( < $( $param ),* > )*
        where
            $( $ann_type : Clone ),*
            $( , $( $whereclause )* )?

        {
            fn clone(&self) -> Self {
                $struct_name {
                    $( $ann_key : self.$ann_key.clone() ),*
                }
            }
        }

        impl<__A, $( $( $param ),* )* > __Combine<__A> for $struct_name $( < $( $param ),* > )*
        where
            $( __A: __Borrow<$ann_type> ),* ,
            $( $( $whereclause )* )?
        {
            fn combine<__E>(elements: &[__E] ) -> Option<Self>     where
                __A: __Borrow<Self> + Clone,
                __E: __ErasedAnnotation<__A> {
                Some($struct_name {
                    $(
                        $ann_key : if let Some(combined) = < $ann_type >::combine(elements) {
                            combined
                        } else {
                            return None
                        }
                    ),*
                })
            }
        }

        macro_rules! impl_borrow {
            ($sub_ann_key:ident : $sub_ann_type:ty) => {
                impl<$( $( $param ),* )* > __Borrow<$sub_ann_type>

                    for $struct_name $( < $( $param ),* > )*
                    $( where $( $whereclause )* )? {
                    fn borrow(&self) -> & $sub_ann_type {
                        &self.$sub_ann_key
                    }
                }
            }
        }

        macro_rules! impl_as_ref {
            ($sub_ann_key:ident : $sub_ann_type:ty) => {
                impl<$( $( $param ),* )* > AsRef<$sub_ann_type>
                    for $struct_name $( < $( $param ),* > )*
                    $( where $( $whereclause )* )?
                {
                    fn as_ref(&self) -> & $sub_ann_type {
                        &self.$sub_ann_key
                    }
                }
            }
        }

        macro_rules! impl_borrow_ref {
            ($sub_ann_key:ident : $sub_ann_type:ty) => {
                impl<'__a, $( $( $param ),* )* > __Borrow<$sub_ann_type>

                    for &'__a $struct_name $( < $( $param ),* > )*
                {
                    fn borrow(&self) -> & $sub_ann_type {
                        &self.$sub_ann_key
                    }
                }
            }
        }

        $(
            impl_as_ref! { $ann_key : $ann_type }
            impl_borrow! { $ann_key : $ann_type }
            impl_borrow_ref! { $ann_key : $ann_type }
        )*
    }
}