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
use std::{fmt::Debug, ops::Add};
use crate::PrintableIndex;
/// Trait for types that can be used as vector indices.
///
/// This trait is automatically implemented for any type that satisfies the
/// required bounds. No manual implementation is needed.
pub trait VecIndex
where
Self: Debug
+ Default
+ Copy
+ Clone
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ From<usize>
+ Into<usize>
+ Add<usize, Output = Self>
+ Send
+ Sync
+ PrintableIndex
+ 'static,
{
/// Converts this index to a `usize`.
#[inline]
fn to_usize(self) -> usize {
self.into()
}
/// Returns the previous index, or `None` if this is zero.
#[inline]
fn decremented(self) -> Option<Self> {
self.to_usize().checked_sub(1).map(Self::from)
}
}
impl<I> VecIndex for I where
I: Debug
+ Default
+ Copy
+ Clone
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ From<usize>
+ Into<usize>
+ Add<usize, Output = Self>
+ Send
+ Sync
+ PrintableIndex
+ 'static
{
}