[−][src]Macro lark_collections::index_type
Use to declare a "newtype'd" index that can be used with IndexVec
.
The simplest usage is just:
ⓘThis example is not tested
index_type! { $v struct Foo { .. } }
where $v
is whatever visibility you want (pub
, crate
, etc).
Instances offer several methods and also implement a number of convenient traits:
Foo::new(22)
-- make a newFoo
from ausize
(works in constants, too)Foo::from_u32(22)
-- make aFoo
from au32
(works in constants, too)foo.as_u32()
-- extract the inner value as au32
foo.as_usize()
-- extract the inner value as ausize
Foo: From<usize>
-- you can also use theFrom
trait to construct from a usizeFoo: From<u32>
-- ...or a u32
Index types also implement the usual suspects (Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash) so they can be used with maps and things.
Storage
Internally, index types use a NonZeroU32
for storage. This means that they
cannot exceed 2^32 - 1 in value (the code will assert this at runtime). It also means
that Option<Foo>
is just one u32
in size.
Configuring the newtype
Before the ..
you can also add some configuration options. Example:
ⓘThis example is not tested
index_type! { struct Foo { debug_name[Bar], .. // <-- NB always end with `..` } }
The options are:
debug_name[$expr]
-- change how theDebug
impl prints out. This should be a string expression, like"XXX"
. We will print"XXX"(N)
whereN
is the index. If you put justdebug_name[]
, then we will not emit anyDebug
impl at all, and you can provide your own.