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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use std::borrow::BorrowMut;
use std::cmp::Ordering;
use std::fmt;
use std::ops::{Index, IndexMut, Range, RangeTo, RangeFrom, RangeFull};

use bow::Bow;
use len_trait::{Len, SplitAtMut};
use push_trait::PushBack;

macro_rules! gen_impl {
    ($(
        $name:ident,
        $slice_name:ident,
        $str_name:ident,
        $c_str_name:ident,
        $os_str_name:ident,
        $n:expr,
    )*) => {
        $(
            /// Array of immutable strings stored on the heap in the same buffer.
            pub struct $name<T: $crate::StrLike + ?Sized> {
                buffer: ::bow::Bow<'static, T::Data>,
                split: [usize; $n],
            }

            impl<'a, T: $crate::StrLike + ?Sized> From<[&'a T; $n]> for $name<T> {
                fn from(inner: [&'a T; $n]) -> $name<T> {
                    $name::new(inner)
                }
            }

            impl<T: $crate::StrLike + ?Sized> Default for $name<T> {
                fn default() -> $name<T> {
                    let def: &'static T = ::extra_default::DefaultRef::default_ref();
                    let data = def.to_data();
                    let len = data.len();
                    let mut buffer = data.to_owned();

                    let mut split = [len; $n];
                    let mut acc = 0;
                    for s in &mut split {
                        *s = acc;
                        acc += len;
                        buffer.push_back(data);
                    }
                    $name { buffer: Bow::Boxed(buffer.into()), split }
                }
            }

            impl<T: $crate::StrLike + ?Sized> $name<T> {
                /// Creates a new `Static` from the given array of values.
                pub fn new(inner: [&T; $n]) -> $name<T> {
                    let inner: &[&T] = &inner;

                    let mut buffer: T::OwnedData = Default::default();
                    for item in inner.iter() {
                        buffer.push_back(item.to_data());
                    }
                    let buffer: Box<T::Data> = buffer.into();
                    let buffer: Bow<'static, T::Data> = buffer.into();
                    let mut split = [0; $n];
                    inner.iter().map(|s| s.len()).enumerate().fold(0, |mut curr, (i, len)| {
                        curr += len;
                        split[i] = curr;
                        curr
                    });

                    $name { buffer, split }
                }

                /// Creates a `Static` from its raw parts: a buffer and a list of split indices.
                #[inline]
                pub fn from_raw<D>(buffer: D, split: [usize; $n]) -> $name<T>
                where
                    D: Into<Bow<'static, T::Data>>
                {
                    let buffer = buffer.into();
                    let check = $crate::Split::new(&split);
                    check.check_valid(buffer.len())
                        .unwrap_or_else(|e| panic!("split indices were invalid: {}", e));
                    for idx in 0..$n {
                        T::from_data(check.get(idx).index_into(&*buffer))
                            .unwrap_or_else(|e| panic!("string {} was not valid: {}", idx, e));
                    }
                    $name { buffer, split }
                }

                /// Creates a `Static` from its raw parts (unsafe version).
                #[inline]
                pub unsafe fn from_raw_unchecked<D>(buffer: D, split: [usize; $n]) -> $name<T>
                where
                    D: Into<Bow<'static, T::Data>>,
                {
                    let buffer = buffer.into();
                    $name { buffer, split }
                }

                /// Returns an iterator over the elements in this `Static`.
                #[inline]
                pub fn iter(&self) -> $crate::Iter<T> {
                    $crate::Iter::new(&*self.buffer, &self.split)
                }
            }

            impl<T: ?Sized + $crate::StrLike> Index<usize> for $name<T> {
                type Output = T;
                fn index(&self, index: usize) -> &T {
                    assert_ne!(index, $n);
                    unsafe {
                        let split = $crate::Split::new(&self.split);
                        T::from_data_unchecked(split.get(index).index_into(&self.buffer))
                    }
                }
            }

            impl<T: ?Sized + $crate::StrLike + $crate::StrLikeMut> IndexMut<usize> for $name<T>
                where T::Data: SplitAtMut<usize>,
                      T::OwnedData: BorrowMut<T::Data>
            {
                #[inline]
                fn index_mut(&mut self, index: usize) -> &mut T {
                    assert_ne!(index, $n);
                    unsafe {
                        let idx = $crate::Split::new(&self.split).get(index);
                        T::from_data_mut_unchecked(
                            idx.index_into_mut(self.buffer.to_mut().borrow_mut())
                        )
                    }
                }
            }

            impl<T: ?Sized + $crate::DataConcat> Index<Range<usize>> for $name<T> {
                type Output = T;
                #[inline]
                fn index(&self, range: Range<usize>) -> &T {
                    unsafe {
                        let split = $crate::Split::new(&self.split);
                        T::from_data_unchecked(
                            split.get_slice(range.into()).index_into(&self.buffer)
                        )
                    }
                }
            }

            impl<T: ?Sized + $crate::DataConcat> Index<RangeFrom<usize>> for $name<T> {
                type Output = T;
                #[inline]
                fn index(&self, range: RangeFrom<usize>) -> &T {
                    unsafe {
                        let split = $crate::Split::new(&self.split);
                        T::from_data_unchecked(
                            split.get_slice(range.into()).index_into(&self.buffer)
                        )
                    }
                }
            }

            impl<T: ?Sized + $crate::DataConcat> Index<RangeTo<usize>> for $name<T> {
                type Output = T;
                #[inline]
                fn index(&self, range: RangeTo<usize>) -> &T {
                    unsafe {
                        let split = $crate::Split::new(&self.split);
                        T::from_data_unchecked(
                            split.get_slice(range.into()).index_into(&self.buffer)
                        )
                    }
                }
            }

            impl<T: ?Sized + $crate::DataConcat> Index<RangeFull> for $name<T> {
                type Output = T;
                #[inline]
                fn index(&self, _: RangeFull) -> &T {
                    unsafe {
                        T::from_data_unchecked(&self.buffer)
                    }
                }
            }


            impl<T: $crate::StrLike + ?Sized> Clone for $name<T>
                where Box<T::Data>: Clone
            {
                fn clone(&self) -> $name<T> {
                    $name {
                        buffer: self.buffer.clone(),
                        split: self.split.clone(),
                    }
                }
                fn clone_from(&mut self, source: &$name<T>) {
                    self.buffer.clone_from(&source.buffer);
                    self.split.clone_from(&source.split);
                }
            }

            impl<T: $crate::StrLike + ?Sized> ::std::hash::Hash for $name<T>
                where T::Data: ::std::hash::Hash
            {
                fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
                    self.buffer.hash(state);
                    self.split.hash(state);
                }
            }

            impl<T: $crate::StrLike + PartialEq + ?Sized> PartialEq for $name<T> {
                fn eq(&self, rhs: &$name<T>) -> bool {
                    self.iter().eq(rhs.iter())
                }
            }

            impl<T: $crate::StrLike + Eq + ?Sized> Eq for $name<T> {}

            impl<T: $crate::StrLike + PartialOrd + ?Sized> PartialOrd for $name<T> {
                fn partial_cmp(&self, rhs: &$name<T>) -> Option<Ordering> {
                    self.iter().partial_cmp(rhs.iter())
                }
                fn lt(&self, rhs: &$name<T>) -> bool {
                    self.iter().lt(rhs.iter())
                }
                fn le(&self, rhs: &$name<T>) -> bool {
                    self.iter().le(rhs.iter())
                }
                fn gt(&self, rhs: &$name<T>) -> bool {
                    self.iter().gt(rhs.iter())
                }
                fn ge(&self, rhs: &$name<T>) -> bool {
                    self.iter().ge(rhs.iter())
                }
            }

            impl<T: $crate::StrLike + Ord + ?Sized> Ord for $name<T> {
                fn cmp(&self, rhs: &$name<T>) -> Ordering {
                    self.iter().cmp(rhs.iter())
                }
            }

            impl<T: $crate::StrLike + fmt::Debug + ?Sized> fmt::Debug for $name<T> {
                fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                    f.debug_list()
                        .entries(self.iter())
                        .finish()
                }
            }

            /// Array of immutable slices stored on the heap in the same buffer.
            pub type $slice_name<T> = $name<[T]>;

            /// Array of immutable `str`s stored on the heap in the same buffer.
            pub type $str_name = $name<str>;

            /// Array of immutable `CStr`s stored on the heap in the same buffer.
            pub type $c_str_name = $name<::std::ffi::CStr>;

            ///// Array of immutable `OsStr`s stored on the heap in the same buffer.
            //pub type $os_str_name = $name<::std::ffi::OsStr>;
        )*
    }
}

gen_impl! {
    Static2, SliceArray2, StringArray2, CStringArray2, OsStringArray2, 2,
    Static3, SliceArray3, StringArray3, CStringArray3, OsStringArray3, 3,
    Static4, SliceArray4, StringArray4, CStringArray4, OsStringArray4, 4,
    Static5, SliceArray5, StringArray5, CStringArray5, OsStringArray5, 5,
    Static6, SliceArray6, StringArray6, CStringArray6, OsStringArray6, 6,
    Static7, SliceArray7, StringArray7, CStringArray7, OsStringArray7, 7,
    Static8, SliceArray8, StringArray8, CStringArray8, OsStringArray8, 8,
    Static9, SliceArray9, StringArray9, CStringArray9, OsStringArray9, 9,
    Static10, SliceArray10, StringArray10, CStringArray10, OsStringArray10, 10,
    Static11, SliceArray11, StringArray11, CStringArray11, OsStringArray11, 11,
    Static12, SliceArray12, StringArray12, CStringArray12, OsStringArray12, 12,
    Static13, SliceArray13, StringArray13, CStringArray13, OsStringArray13, 13,
    Static14, SliceArray14, StringArray14, CStringArray14, OsStringArray14, 14,
    Static15, SliceArray15, StringArray15, CStringArray15, OsStringArray15, 15,
    Static16, SliceArray16, StringArray16, CStringArray16, OsStringArray16, 16,
}

#[cfg(test)]
mod tests {
    use std::ffi::CStr;

    use super::Static3;

    #[test]
    fn debug() {
        let array = Static3::new(["English", "Français", "中文"]);
        assert_eq!(
            format!("{:?}", array),
            r#"["English", "Français", "中文"]"#
        )
    }

    #[test]
    #[should_panic]
    fn panic_oob() {
        let array = <Static3<[u8]>>::default();
        let _ = &array[3];
    }

    #[test]
    #[should_panic]
    fn panic_oob_str() {
        let array = <Static3<str>>::default();
        let _ = &array[3];
    }

    #[test]
    #[should_panic]
    fn panic_oob_c_str() {
        let array = <Static3<CStr>>::default();
        let _ = &array[3];
    }

    #[test]
    fn index() {
        let array = Static3::new(["English", "Français", "中文"]);
        assert_eq!(&array[0], "English");
        assert_eq!(&array[1], "Français");
        assert_eq!(&array[2], "中文");
        assert_eq!(&array[0..0], "");
        assert_eq!(&array[0..1], "English");
        assert_eq!(&array[0..2], "EnglishFrançais");
        assert_eq!(&array[0..3], "EnglishFrançais中文");
        assert_eq!(&array[1..1], "");
        assert_eq!(&array[1..2], "Français");
        assert_eq!(&array[1..3], "Français中文");
        assert_eq!(&array[2..2], "");
        assert_eq!(&array[2..3], "中文");
        assert_eq!(&array[3..3], "");
        assert_eq!(&array[0..], "EnglishFrançais中文");
        assert_eq!(&array[1..], "Français中文");
        assert_eq!(&array[2..], "中文");
        assert_eq!(&array[3..], "");
        assert_eq!(&array[..0], "");
        assert_eq!(&array[..1], "English");
        assert_eq!(&array[..2], "EnglishFrançais");
        assert_eq!(&array[..3], "EnglishFrançais中文");
        assert_eq!(&array[..], "EnglishFrançais中文");
    }

    #[test]
    #[should_panic]
    fn panic_left_oob() {
        let array = Static3::new(["English", "Français", "中文"]);
        let _ = &array[4..];
    }

    #[test]
    #[should_panic]
    fn panic_right_oob() {
        let array = Static3::new(["English", "Français", "中文"]);
        let _ = &array[..4];
    }
}