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
// TODO: Quite messy macro stuff, needs documenting/tidying up

// Define the iterator struct and constructor from raw data.
// Same for all data types.
macro_rules! define_iter {
    ($(#[$iter_attr:meta])* struct $name:ident -> $raw:ty) => (
        $(#[$iter_attr])*
        pub struct $name<'a> {
            ptr: $raw,
            idx: isize,
            len: usize,
            _mk: ::std::marker::PhantomData<&'a ()>
        }

        #[doc(hidden)]
        impl<'a> $name<'a> {
            pub fn new(ptr: $raw, len: usize) -> $name<'a> {
                $name { ptr: ptr, idx: 0, len: len, _mk: ::std::marker::PhantomData }
            }
        }
    )
}

macro_rules! impl_iterator {
    ($name:ident, $item:ident) => (
        impl<'a> Iterator for $name<'a> {
            type Item = $item<'a>;
            fn next(&mut self) -> Option<$item<'a>> {
                if self.idx < self.len as isize {
                    let item = $item::from_raw(unsafe { self.ptr.offset(self.idx) });
                    self.idx = self.idx + 1;
                    Some(item)
                } else {
                    None
                }
            }
        }

        impl<'a> ExactSizeIterator for $name<'a> {
            fn len(&self) -> usize { self.len }
        }
    )
}

macro_rules! impl_iterator_indirect {
    ($name:ident, $item:ident) => (
        impl<'a> Iterator for $name<'a> {
            type Item = $item<'a>;
            fn next(&mut self) -> Option<$item<'a>> {
                if self.idx < self.len as isize {
                    let item = $item::from_raw(unsafe { *self.ptr.offset(self.idx) });
                    self.idx = self.idx + 1;
                    Some(item)
                } else {
                    None
                }
            }
        }

        impl<'a> ExactSizeIterator for $name<'a> {
            fn len(&self) -> usize { self.len }
        }
    )
}

macro_rules! impl_iterator_pod {
    ($name:ident, $item:ident) => (
        impl<'a> Iterator for $name<'a> {
            type Item = $item;
            fn next(&mut self) -> Option<$item> {
                if self.idx < self.len as isize {
                    let item = $item::from_raw(unsafe { self.ptr.offset(self.idx) });
                    self.idx = self.idx + 1;
                    Some(item)
                } else {
                    None
                }
            }
        }
    )
}

macro_rules! define_type {
    // Reference type
    ($(#[$type_attr:meta])* struct $name:ident(&$raw:ty)) => (
        $(#[$type_attr])*
        pub struct $name<'a>(&'a $raw);

        #[doc(hidden)]
        impl<'a> $name<'a> {
            pub fn from_raw(raw: *const $raw) -> $name<'a> {
                unsafe { $name(&*raw) }
            }
            pub fn to_raw(&self) -> *const $raw {
                self.0
            }
        }

        impl<'a> ::std::ops::Deref for $name<'a> {
            type Target = $raw;
            fn deref<'b>(&'b self) -> &'b $raw { &self.0 }
        }
    );
    // Non-reference type = POD
    ($(#[$type_attr:meta])* struct $name:ident($raw:ty)) => (
        $(#[$type_attr])*
        pub struct $name($raw);

        #[doc(hidden)]
        impl $name {
            pub fn from_raw(raw: *const $raw) -> $name {
                unsafe { $name(*raw) }
            }
        }

        impl ::std::ops::Deref for $name {
            type Target = $raw;
            fn deref<'a>(&'a self) -> &'a $raw { &self.0 }
        }
    );
}

macro_rules! define_type_and_iterator {
    (
        $(#[$type_attr:meta])* struct $type_name:ident(&$raw:ty)
        $(#[$iter_attr:meta])* struct $iter_name:ident
    ) => (
        define_type!($(#[$type_attr])* struct $type_name(&$raw));
        define_iter!($(#[$iter_attr])* struct $iter_name -> *const $raw);
        impl_iterator!($iter_name, $type_name);
    );
    (
        $(#[$type_attr:meta])* struct $type_name:ident($raw:ty)
        $(#[$iter_attr:meta])* struct $iter_name:ident
    ) => (
        define_type!($(#[$type_attr])* struct $type_name($raw));
        define_iter!($(#[$iter_attr])* struct $iter_name -> *const $raw);
        impl_iterator_pod!($iter_name, $type_name);
    );
}

macro_rules! define_type_and_iterator_indirect {
    (
        $(#[$type_attr:meta])* struct $type_name:ident(&$raw:ty)
        $(#[$iter_attr:meta])* struct $iter_name:ident
    ) => (
        define_type!($(#[$type_attr])* struct $type_name(&$raw));
        define_iter!($(#[$iter_attr])* struct $iter_name -> *const *const $raw);
        impl_iterator_indirect!($iter_name, $type_name);
    );
}