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
// Send and Sync
// All the iterators are thread safe the same way the slice's iterator are

// read-only iterators use Sync => Send rules, same as `std::slice::Iter`.
macro_rules! send_sync_read_only {
    ($name:ident) => {
        unsafe impl<'a, A, D> Send for $name<'a, A, D>
        where
            A: Sync,
            D: Send,
        {
        }
        unsafe impl<'a, A, D> Sync for $name<'a, A, D>
        where
            A: Sync,
            D: Sync,
        {
        }
    };
}

// read-write iterators use Send => Send rules, same as `std::slice::IterMut`.
macro_rules! send_sync_read_write {
    ($name:ident) => {
        unsafe impl<'a, A, D> Send for $name<'a, A, D>
        where
            A: Send,
            D: Send,
        {
        }
        unsafe impl<'a, A, D> Sync for $name<'a, A, D>
        where
            A: Sync,
            D: Sync,
        {
        }
    };
}

macro_rules! impl_ndproducer {
    (
    [$($typarm:tt)*]
    [Clone => $($cloneparm:tt)*]
     $typename:ident {
         $base:ident,
         $(
             $fieldname:ident,
         )*
     }
     $fulltype:ty {
        $(
            type $atyn:ident = $atyv:ty;
        )*

        unsafe fn item(&$self_:ident, $ptr:pat) {
            $refexpr:expr
        }
    }) => {
impl<$($typarm)*> NdProducer for $fulltype {
    $(
        type $atyn = $atyv;
    )*
    type Ptr = *mut A;
    type Stride = isize;

    #[doc(hidden)]
    fn raw_dim(&self) -> D {
        self.$base.raw_dim()
    }

    #[doc(hidden)]
    fn layout(&self) -> Layout {
        self.$base.layout()
    }

    #[doc(hidden)]
    fn as_ptr(&self) -> *mut A {
        self.$base.as_ptr() as *mut _
    }

    #[doc(hidden)]
    fn contiguous_stride(&self) -> isize {
        self.$base.contiguous_stride()
    }

    #[doc(hidden)]
    unsafe fn as_ref(&$self_, $ptr: *mut A) -> Self::Item {
        $refexpr
    }

    #[doc(hidden)]
    unsafe fn uget_ptr(&self, i: &Self::Dim) -> *mut A {
        self.$base.uget_ptr(i)
    }

    #[doc(hidden)]
    fn stride_of(&self, axis: Axis) -> isize {
        self.$base.stride_of(axis)
    }

    #[doc(hidden)]
    fn split_at(self, axis: Axis, index: usize) -> (Self, Self) {
        let (a, b) = self.$base.split_at(axis, index);
        ($typename {
            $base: a,
            $(
            $fieldname: self.$fieldname.clone(),
            )*
        },
        $typename {
            $base: b,
            $(
            $fieldname: self.$fieldname,
            )*
        })
    }
    private_impl!{}
}

expand_if!(@nonempty [$($cloneparm)*]
    impl<$($cloneparm)*> Clone for $fulltype {
        fn clone(&self) -> Self {
            $typename {
                $base: self.base.clone(),
                $(
                $fieldname: self.$fieldname.clone(),
                )*
            }
        }
    }
);

    }
}

macro_rules! impl_iterator {
    (
    [$($typarm:tt)*]
    [Clone => $($cloneparm:tt)*]
     $typename:ident {
         $base:ident,
         $(
             $fieldname:ident,
         )*
     }
     $fulltype:ty {
        type Item = $ity:ty;

        fn item(&mut $self_:ident, $elt:pat) {
            $refexpr:expr
        }
    }) => {
         expand_if!(@nonempty [$($cloneparm)*]

            impl<$($cloneparm)*> Clone for $fulltype {
                fn clone(&self) -> Self {
                    $typename {
                        $base: self.$base.clone(),
                        $(
                            $fieldname: self.$fieldname.clone(),
                        )*
                    }
                }
            }

         );
        impl<$($typarm)*> Iterator for $fulltype {
            type Item = $ity;

            fn next(&mut $self_) -> Option<Self::Item> {
                $self_.$base.next().map(|$elt| {
                    $refexpr
                })
            }

            fn size_hint(&self) -> (usize, Option<usize>) {
                self.$base.size_hint()
            }
        }
    }
}