arena_traits/
lib.rs

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
#![no_std]
extern crate alloc;
use core::ops::IndexMut;

use alloc::boxed::Box;

pub trait IndexAlloc<Idx>: IndexMut<Idx> {
    fn alloc(&mut self, a: Self::Output) -> Idx;
}
pub trait IndexIter<Idx>: IndexMut<Idx> {
    fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = Idx> + 'a>;
}
pub trait Arena<Idx>: IndexAlloc<Idx> + IndexIter<Idx> {}
impl<Idx, T: IndexAlloc<Idx> + IndexIter<Idx>> Arena<Idx> for T {}
#[cfg(feature = "id-arena")]
const _: () = {
    impl<T> IndexAlloc<id_arena::Id<T>> for id_arena::Arena<T> {
        fn alloc(&mut self, a: Self::Output) -> id_arena::Id<T> {
            self.alloc(a)
        }
    };
    impl<T> IndexIter<id_arena::Id<T>> for id_arena::Arena<T> {
        fn iter<'a>(&'a self) -> Box<(dyn Iterator<Item = id_arena::Id<T>> + 'a)> {
            Box::new(self.iter().map(|a| a.0))
        }
    }
};
impl<T> IndexIter<usize> for [T] {
    fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = usize> + 'a> {
        Box::new(self.iter().enumerate().map(|a| a.0))
    }
}
impl<T, const N: usize> IndexIter<usize> for [T; N] {
    fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = usize> + 'a> {
        Box::new(0..N)
    }
}