multi-array-list 0.2.1

A `MultiArrayList` stores a list of a struct.
Documentation
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
//! A `MultiArrayList` stores a list of a struct.
//!
//! **Experimental**: Only a small subset of the array list API is implemented.
//!
//! ---
//!
//! > Instead of storing a single list of items, `MultiArrayList` stores separate lists for each field of the struct.
//! > This allows for memory savings if the struct has padding,
//! > and also improves cache usage if only some fields are needed for a computation.
//!
//! The primary API for accessing fields is the [`items::<name>`][`MultiArrayList::items()`] function.
//!
//! ---
//! _inspired by [Zig's `MultiArrayList`](https://ziglang.org/documentation/master/std/#std.MultiArrayList)._
//!
//! # Example
//! ```rust
//! #![allow(incomplete_features)]
//! #![feature(generic_const_exprs)]
//! # use multi_array_list::MultiArrayList;
//! struct Pizza {
//!     radius: u32,
//!     toppings: Vec<Topping>,
//! }
//!
//! enum Topping {
//!     Tomato,
//!     Mozzarella,
//!     Anchovies,
//! }
//!
//! let mut order = MultiArrayList::<Pizza>::new();
//!
//! let margherita = Pizza {
//!     radius: 12,
//!     toppings: vec![Topping::Tomato],
//! };
//! order.push(margherita);
//!
//! let napoli = Pizza {
//!     radius: 12,
//!     toppings: vec![Topping::Tomato, Topping::Anchovies],
//! };
//! order.push(napoli);
//!
//! for topping in order.items_mut::<"toppings", Vec<Topping>>() {
//!     topping.push(Topping::Mozzarella);
//! }
//! ```
#![allow(incomplete_features)]
#![feature(adt_const_params)]
#![feature(generic_const_exprs)]
#![feature(type_info)]
#![feature(unsized_const_params)]
#![feature(const_cmp)]
#![feature(const_trait_impl)]
#![warn(missing_docs)]

use std::alloc::{self, Layout};
use std::any::TypeId;
use std::marker::PhantomData;
use std::mem::type_info::{Field, Type, TypeKind};
use std::mem::{self, ManuallyDrop, MaybeUninit};
use std::ptr;
use std::slice;

/// Alignment of various types, imprecise for anything but numbers, chars and bools.
const fn align_of(kind: TypeKind) -> usize {
    use TypeKind::*;
    match kind {
        Int(int) => (int.bits / 8) as usize,
        Float(float) => (float.bits / 8) as usize,
        Char(_) => 4,
        Bool(_) => 1,
        _ => mem::size_of::<usize>(),
    }
}

/// Number of fields in `T`
pub const fn field_len<T>() -> usize {
    assert_valid_fields::<T>();
    let typeinfo = Type::of::<T>();

    typeinfo.size.unwrap();
    let TypeKind::Struct(struct_info) = typeinfo.kind else {
        panic!("MultiArrayList only works for structs");
    };
    struct_info.fields.len()
}

const fn assert_valid_fields<T>() {
    let typeinfo = Type::of::<T>();

    typeinfo.size.unwrap();
    let TypeKind::Struct(struct_info) = typeinfo.kind else {
        panic!("MultiArrayList only works for structs");
    };

    assert!(struct_info.generics.is_empty());

    let mut i = 0;
    while i < struct_info.fields.len() {
        let field = &struct_info.fields[i];
        let field_info = field.ty.info();

        assert!(field_info.size.is_some(), "known size required");
        assert!(matches!(
            field_info.kind,
            TypeKind::Bool(_)
                | TypeKind::Int(_)
                | TypeKind::Char(_)
                | TypeKind::Float(_)
                | TypeKind::Tuple(_)
                | TypeKind::Struct(_)
                | TypeKind::Enum(_)
        ));

        i += 1;
    }
}

const fn field_sizes<T>() -> [usize; field_len::<T>()] {
    let typeinfo = Type::of::<T>();

    let TypeKind::Struct(struct_info) = typeinfo.kind else {
        panic!("MultiArrayList only works for structs");
    };

    let fields = struct_info.fields;
    let mut new_fields = [0; field_len::<T>()];

    let mut i = 0;
    while i < fields.len() {
        let field = &fields[i];
        let field_info = field.ty.info();
        let element_size = field_info.size.unwrap();
        new_fields[i] = element_size;
        i += 1;
    }

    new_fields
}

const fn field_aligns<T>() -> [usize; field_len::<T>()] {
    let typeinfo = Type::of::<T>();

    let TypeKind::Struct(struct_info) = typeinfo.kind else {
        panic!("MultiArrayList only works for structs");
    };

    let fields = struct_info.fields;
    let mut new_fields = [0; field_len::<T>()];

    let mut i = 0;
    while i < fields.len() {
        let field = &fields[i];
        let align = align_of(field.ty.info().kind);
        new_fields[i] = align;
        i += 1;
    }

    new_fields
}

/// A `MultiArrayList` stores a list of a struct.
///
/// > Instead of storing a single list of items, `MultiArrayList` stores separate lists for each field of the struct.
/// > This allows for memory savings if the struct has padding,
/// > and also improves cache usage if only some fields are needed for a computation.
///
/// The primary API for accessing fields is the [`items(name)`][`MultiArrayList::items()`] function.
#[derive(Debug)]
pub struct MultiArrayList<T>
where
    T: 'static,
    [(); field_len::<T>()]:,
{
    elems: [*mut u8; field_len::<T>()],
    cap: usize,
    len: usize,

    _t: PhantomData<T>,
}

impl<T> MultiArrayList<T>
where
    T: 'static,
    [(); field_len::<T>()]:,
{
    /// Constructs a new, empty `MultiArrayList<T>`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// #![allow(incomplete_features)]
    /// #![feature(generic_const_exprs)]
    /// # use multi_array_list::MultiArrayList;
    /// struct Point {
    ///     x: i32,
    ///     y: i32
    /// }
    /// let mut list: MultiArrayList<Point> = MultiArrayList::new();
    /// ```
    ///
    /// # Type can't be not a struct
    ///
    /// ```compile_fail
    /// #![allow(incomplete_features)]
    /// #![feature(generic_const_exprs)]
    /// use multi_array_list::MultiArrayList;
    /// #[repr(u8)]
    /// enum Random {
    ///     Four,
    /// }
    /// let m = MultiArrayList::<Random>::new();
    /// ```
    ///
    /// # Type can't be empty
    ///
    /// ```compile_fail
    /// #![allow(incomplete_features)]
    /// #![feature(generic_const_exprs)]
    /// use multi_array_list::MultiArrayList;
    /// let m = MultiArrayList::<()>::new();
    /// ```
    pub fn new() -> MultiArrayList<T> {
        let elems = [ptr::null_mut(); field_len::<T>()];

        MultiArrayList {
            elems,
            cap: 0,
            len: 0,
            _t: PhantomData,
        }
    }

    /// Constructs a new, empty `MultiArrayList<T>` with at least the specified capacity.
    ///
    /// # Examples
    ///
    /// ```rust
    /// #![allow(incomplete_features)]
    /// #![feature(generic_const_exprs)]
    /// # use multi_array_list::MultiArrayList;
    /// struct Point {
    ///     x: i32,
    ///     y: i32
    /// }
    /// let mut list: MultiArrayList<Point> = MultiArrayList::with_capacity(10);
    /// ```
    pub fn with_capacity(capacity: usize) -> MultiArrayList<T> {
        let mut list = MultiArrayList::<T>::new();
        list.grow(capacity);
        list
    }

    fn elem_ptrs(&self) -> &[*mut u8] {
        &self.elems[..]
    }

    const fn fields() -> &'static [Field] {
        let typeinfo = Type::of::<T>();

        let TypeKind::Struct(struct_info) = typeinfo.kind else {
            panic!("MultiArrayList only works for structs");
        };

        struct_info.fields
    }

    /// Returns the total number of elements the vector can hold without reallocating.
    pub fn capacity(&self) -> usize {
        self.cap
    }

    /// Returns the number of elements in the `MultiArrayList`, also referred to as its 'length'.
    pub fn len(&self) -> usize {
        self.len
    }

    /// Appends an element to the back of a collection.
    pub fn push(&mut self, value: T) {
        let len = self.len;

        // We will consume this value, don't drop it.
        let value = ManuallyDrop::new(value);

        if len == self.capacity() {
            self.grow(1);
        }

        let sizes = const { field_sizes::<T>() };

        unsafe {
            let value: *const T = &*value as *const _;
            let value_ptr: *const u8 = value.cast();

            let elem_ptrs = self.elem_ptrs();
            let fields = const { Self::fields() };

            for (idx, (field, elem)) in fields.iter().zip(elem_ptrs).enumerate() {
                let offset = field.offset;
                let size = sizes[idx];

                if size > 0 {
                    let field_elem = elem.offset((size * self.len()) as isize);
                    let src = value_ptr.offset(offset as isize);
                    ptr::copy_nonoverlapping(src, field_elem, size);
                }
            }
        }

        self.len += 1;
    }

    /// Removes the last element from the `MultiArrayList` and returns it, or `None` if it is empty.
    pub fn pop(&mut self) -> Option<Box<T>> {
        if self.len() == 0 {
            return None;
        }
        let idx = self.len() - 1;
        let wip: Box<MaybeUninit<T>> = Box::new(MaybeUninit::uninit());

        let wip = unsafe {
            let elem_ptrs = self.elem_ptrs();
            let wip = Box::into_raw(wip);
            let wip_ptr: *mut u8 = wip.cast();

            let sizes = const { field_sizes::<T>() };

            for (i, (field, elem)) in Self::fields().iter().zip(elem_ptrs).enumerate() {
                let offset = field.offset;
                let size = sizes[i];
                if size > 0 {
                    let field_elem = elem.offset((size * idx) as isize);
                    let dst_ptr = wip_ptr.offset(offset as isize);
                    ptr::copy_nonoverlapping(field_elem, dst_ptr, size);
                }
            }

            Box::from_raw(wip)
        };

        self.len -= 1;

        // SAFETY: We initialized all fields at their correct offset and size
        // based on values we previously copied into our element array.
        unsafe { Some(wip.assume_init()) }
    }

    /// Returns an iterator over the `MultiArrayList`.
    ///
    /// The iterator yields all items from start to end.
    ///
    /// ```
    /// #![allow(incomplete_features)]
    /// #![feature(generic_const_exprs)]
    /// use multi_array_list::MultiArrayList;
    ///
    /// struct Point {
    ///     x: i32,
    ///     y: i32,
    /// }
    ///
    /// let mut points = MultiArrayList::<Point>::new();
    /// points.push(Point { x: 1, y: 0 });
    /// for point in points.iter() {
    ///     assert_eq!(1, *point.get::<"x", _>());
    ///     assert_eq!(0, *point.get::<"y", _>());
    /// }
    /// ```
    pub fn iter<'a>(&'a self) -> Iter<'a, T> {
        Iter {
            array: self,
            pos: 0,
        }
    }

    const fn get_field_by_name<const NAME: &'static str, V: 'static>() -> (usize, usize) {
        let mut idx = 0;
        while idx < Self::fields().len() {
            let field = &Self::fields()[idx];
            if field.name.eq_ignore_ascii_case(NAME) {
                break;
            }
            idx += 1
        }

        if idx == Self::fields().len() {
            panic!("unknown item name");
        }

        assert!(idx < Self::fields().len());
        let field = &Self::fields()[idx];
        assert!(TypeId::of::<V>() == field.ty, "types don't match");

        let elem_size = mem::size_of::<V>();
        (idx, elem_size)
    }

    /// Get an iterator of values for a specified field.
    ///
    /// # Compile errors
    ///
    /// * Fails to compile if the requested field does not exist.
    /// * Fails to compile if the requested type does not match the found field (by size).
    ///
    /// ```compile_fail
    /// #![allow(incomplete_features)]
    /// #![feature(generic_const_exprs)]
    /// use multi_array_list::MultiArrayList;
    /// struct HereBeDragons {
    ///     x: u32,
    ///     y: (u8, u16),
    /// }
    /// let mut list = MultiArrayList::<HereBeDragons>::new();
    /// _ = list.items::<"y", (u16, u8)>(); // will not compile
    /// ```
    pub fn items<'a, const NAME: &'static str, V: 'static>(&'a self) -> Slice<'a, V> {
        let (idx, elem_size) = const { Self::get_field_by_name::<NAME, V>() };

        unsafe {
            let elem_ptrs = self.elem_ptrs();
            let ptr = elem_ptrs[idx];
            let end = ptr.offset((self.len * elem_size) as isize);

            return Slice {
                ptr,
                end,
                typ: PhantomData,
            };
        }
    }

    /// Get an iterator of mutable values for a specified field.
    ///
    /// # Compile errors
    ///
    /// * Fails to compile if the requested field does not exist.
    /// * Fails to compile if the requested type does not match the found field (by size).
    pub fn items_mut<'a, const NAME: &'static str, V: 'static>(&'a mut self) -> SliceMut<'a, V> {
        let (idx, elem_size) = const { Self::get_field_by_name::<NAME, V>() };

        unsafe {
            let elem_ptrs = self.elem_ptrs();
            let ptr = elem_ptrs[idx];
            let end = ptr.offset((self.len * elem_size) as isize);

            return SliceMut {
                ptr,
                end,
                typ: PhantomData,
            };
        }
    }

    fn grow(&mut self, additional: usize) {
        let old_cap = self.capacity();
        let new_cap = old_cap + additional;

        let sizes = const { field_sizes::<T>() };
        let aligns = const { field_aligns::<T>() };

        for idx in 0..Self::fields().len() {
            let element_size = sizes[idx];
            let align = aligns[idx];
            let old_array_size = element_size * old_cap;
            let new_array_size = element_size * new_cap;

            if new_array_size == 0 {
                continue;
            }

            unsafe {
                let ptr = &mut self.elems[idx];
                if old_cap == 0 {
                    let layout = Layout::from_size_align_unchecked(new_array_size, align);
                    *ptr = alloc::alloc(layout);
                } else {
                    let layout = Layout::from_size_align_unchecked(old_array_size, align);
                    *ptr = alloc::realloc(*ptr, layout, new_array_size);
                }
            }
        }

        self.cap = new_cap;
    }

    /// Shrinks the capacity of the vector as much as possible.
    pub fn shrink_to_fit(&mut self) {
        let cur_cap = self.capacity();
        let len = self.len();
        if len == cur_cap {
            return;
        }

        let sizes = const { field_sizes::<T>() };
        let aligns = const { field_aligns::<T>() };

        unsafe {
            for idx in 0..Self::fields().len() {
                let element_size = sizes[idx];
                let align = aligns[idx];
                let old_array_size = element_size * cur_cap;
                let new_array_size = element_size * len;

                if element_size == 0 {
                    continue;
                }

                let ptr = &mut self.elems[idx];
                let layout = Layout::from_size_align_unchecked(old_array_size, align);
                if new_array_size == 0 {
                    alloc::dealloc(*ptr, layout);
                    *ptr = ptr::null_mut();
                } else {
                    *ptr = alloc::realloc(*ptr, layout, new_array_size);
                }
            }
        }

        self.cap = len;
    }
}

impl<T> Drop for MultiArrayList<T>
where
    T: 'static,
    [(); field_len::<T>()]:,
{
    fn drop(&mut self) {
        while let Some(elem) = self.pop() {
            drop(elem);
        }

        self.shrink_to_fit();
    }
}

/// An iterator over all values of a specified field.
pub struct Slice<'a, V> {
    ptr: *const u8,
    end: *const u8,
    typ: PhantomData<&'a V>,
}

impl<'a, V> Iterator for Slice<'a, V> {
    type Item = &'a V;

    fn next(&mut self) -> Option<Self::Item> {
        if self.ptr >= self.end {
            return None;
        }
        let elem_size = mem::size_of::<V>();

        unsafe {
            let slice: &[u8] = slice::from_raw_parts(self.ptr, elem_size);
            let val = mem::transmute_copy::<&[u8], &V>(&slice);
            self.ptr = self.ptr.offset(elem_size as isize);
            return Some(val);
        }
    }
}

/// An iterator over mutable values of a specified field.
pub struct SliceMut<'a, V> {
    ptr: *mut u8,
    end: *mut u8,
    typ: PhantomData<&'a mut V>,
}

impl<'a, V> Iterator for SliceMut<'a, V> {
    type Item = &'a mut V;

    fn next(&mut self) -> Option<Self::Item> {
        if self.ptr >= self.end {
            return None;
        }
        let elem_size = mem::size_of::<V>();

        unsafe {
            let mut slice: &mut [u8] = slice::from_raw_parts_mut(self.ptr, elem_size);
            let val = mem::transmute_copy::<&mut [u8], &mut V>(&mut slice);
            self.ptr = self.ptr.offset(elem_size as isize);
            return Some(val);
        }
    }
}

/// An iterator over all elements of the `MultiArrayList`.
pub struct Iter<'a, T>
where
    T: 'static,
    [(); field_len::<T>()]:,
{
    array: &'a MultiArrayList<T>,
    pos: usize,
}

impl<'a, T> Iterator for Iter<'a, T>
where
    T: 'static,
    [(); field_len::<T>()]:,
{
    type Item = IterRef<'a, T>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pos >= self.array.len() {
            return None;
        }

        let elems_slice = &self.array.elems[..];

        self.pos += 1;
        Some(IterRef {
            ptr: elems_slice,
            idx: self.pos - 1,
            elem: PhantomData,
        })
    }
}

/// A reference to a single element of `T`.
///
/// Use [`get(field)`][`IterRef::get`] to access a single field.
pub struct IterRef<'a, T>
where
    T: 'static,
{
    ptr: &'a [*mut u8],
    idx: usize,
    elem: PhantomData<&'a T>,
}

impl<'a, T> IterRef<'a, T>
where
    [(); field_len::<T>()]:,
{
    /// Get a specified field of `T`.
    ///
    /// # Compile errors
    ///
    /// * Fails to compile if the requested field does not exist.
    /// * Fails to compile if the requested type does not match the found field (by size).
    ///
    /// ## It needs the correct type or it fails to compile
    ///
    /// ```compile_fail
    /// #![allow(incomplete_features)]
    /// #![feature(generic_const_exprs)]
    /// use multi_array_list::MultiArrayList;
    ///
    /// struct Point {
    ///     x: i32,
    ///     y: i32,
    /// }
    ///
    /// let mut points = MultiArrayList::<Point>::new();
    /// points.push(Point { x: 1, y: 0 });
    /// for point in points.iter() {
    ///     point.get::<"x", bool>();
    /// }
    /// ```
    pub fn get<const NAME: &'static str, V: 'static>(&self) -> &V {
        let (idx, elem_size) = const { MultiArrayList::<T>::get_field_by_name::<NAME, V>() };

        unsafe {
            let ptr = self.ptr[idx].offset((self.idx * elem_size) as isize);
            let slice: &[u8] = slice::from_raw_parts(ptr, elem_size);
            let val = mem::transmute_copy::<&[u8], &V>(&slice);
            return val;
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::mem;

    #[derive(Debug, PartialEq, Eq)]
    struct Point {
        x: i32,
        y: i32,
    }

    #[repr(u8)]
    #[allow(dead_code)]
    enum Random {
        Four,
    }

    #[test]
    fn size() {
        assert_eq!(32, mem::size_of::<MultiArrayList<Point>>());
    }

    #[test]
    fn empty() {
        let list = MultiArrayList::<Point>::new();
        assert_eq!(0, list.len());
        assert_eq!(0, list.capacity());
    }

    #[test]
    fn push() {
        let mut list = MultiArrayList::<Point>::new();
        assert_eq!(0, list.len());
        assert_eq!(0, list.capacity());

        let p1 = Point { x: 2, y: 8 };
        list.push(p1);
        assert_eq!(1, list.len());
        assert!(0 < list.capacity());
    }

    #[test]
    fn pop() {
        let mut list = MultiArrayList::<Point>::new();
        assert_eq!(0, list.len());
        assert_eq!(0, list.capacity());

        let p1 = Point { x: 2, y: 8 };
        list.push(p1);
        assert_eq!(1, list.len());
        assert!(0 < list.capacity());

        let p2 = list.pop().unwrap();
        assert_eq!(0, list.len());
        assert_eq!(&Point { x: 2, y: 8 }, &*p2);
    }

    #[test]
    fn capacity() {
        let mut list = MultiArrayList::<Point>::with_capacity(10);
        assert_eq!(0, list.len());
        assert_eq!(10, list.capacity());

        let p1 = Point { x: 2, y: 8 };
        list.push(p1);
        assert_eq!(1, list.len());
        assert_eq!(10, list.capacity());
    }

    #[test]
    fn zst() {
        #[allow(dead_code)]
        #[derive(Debug, PartialEq, Eq)]
        struct Empty {
            x: (),
        }

        let mut list = MultiArrayList::<Empty>::new();
        assert_eq!(0, list.len());
        assert_eq!(0, list.capacity());

        list.push(Empty { x: () });

        let elem = list.pop().unwrap();
        assert_eq!(Empty { x: () }, *elem);
    }
}

#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;