pub trait Idx: Copy + 'static + Ord + Debug + Hash {
// Required methods
fn from_usize(idx: usize) -> Self;
fn index(self) -> usize;
}
Expand description
Represents a wrapped value convertable to and from a usize
.
Generally you implement this via the define_index_type!
macro, rather
than manually implementing it.
Overflow
Idx
impls are allowed to be smaller than usize
, which means converting
usize
to an Idx
implementation might have to handle overflow.
The way overflow is handled is up to the implementation of Idx
, but it’s
generally panicing, unless it was turned off via the
DISABLE_MAX_INDEX_CHECK
option in define_index_type!
. If you need more
subtle handling than this, then you’re on your own (or, well, either handle
it earlier, or pick a bigger index type).
Note: I’m open for suggestions on how to handle this case, but do not want the typical cases (E.g. Idx is a newtyped usize or u32), to become more complex.
Required Methods§
sourcefn from_usize(idx: usize) -> Self
fn from_usize(idx: usize) -> Self
Construct an Index from a usize. This is equivalent to From
Note that this will panic if idx
does not fit (unless checking has
been disabled, as mentioned above). Also note that Idx
implementations
are free to define what “fit” means as they desire.