Expand description
This crate provide the numid!
macro for creating numerical id.
Each new instance of your id is guaranteed to have a higher value than
the previous one created. However, the value range less than or equal
to INITIAL_VALUE
is free to use, and multiple instances with the same value
can be created.
Use new
, default
or create_maybe
for creating unique id.
Use create_lower
or const_create_lower
for creating free id.
Id created with reproduce
are unique or free depending on the id used.
§Syntax
numid!([pub] struct NAME [(TYPE)] [-> CONSTANT]);
If not indicated, TYPE=u64
and CONSTANT=0
.
§Attributes
Attributes can be attached to the generated struct
by placing them
before the struct
keyword (or pub
if public).
§Examples
use numid::numid;
numid!(pub struct MyId -> 10);
fn main() {
let id1 = MyId::new();
let id2 = MyId::new();
assert!(id2 > id1);
assert_eq!(id1.value(), 11);
assert_eq!(id2.value(), 12);
}
See example
for documentation of code generated by numid!
.
§Trait implementations
The Copy
, Clone
, PartialEq
, Eq
, PartialOrd
, Ord
, Hash
and Debug
traits automatically derived for the struct
using the derive
attribute.
Additional traits can be derived by providing an explicit derive
attribute.
The Display
, Binary
, Octal
, LowerHex
, UpperHex
and Default
traits are implemented for
the struct
. When calling default()
, the struct is initialized with a new value instead of 0
.
Your own version of Display
can be implemented by disabling the display
default feature.
The behavior of the Clone
trait can be modified with built-in attribut :
@CloneIsClone
: default behavior, can be ignored@CloneIsNew
:clone()
callnew()
@CloneIsReproduce
:clone()
callreproduce()
This built-in attribut must be placed between the attributs and the visibility argument.
§Methods
The following methods are defined for the generated struct
(only value
and reproduce
need a instance) :
new
: create a new idvalue
: get the id valuecurrent_value
: get the value of the last id or initial_value if no id createdinitial_value
: get the value defined when callingnumid!
replace_current_value
: seeexample::NumId::replace_current_value
create_maybe
: seeexample::NumId::create_maybe
create_lower
: seeexample::NumId::create_lower
const_create_lower
: seeexample::NumId::const_create_lower
reproduce
: seeexample::NumId::reproduce
See example::NumId
for more documentation of methods generated by numid!
.
§Crate feature
This crate provides the display
feature enabled by default who automatically implemente the Display
trait
in the structure generated by the numid!
macro. If you want to implemente your own version of Display
,
add default-features = false
in the dependencies.numid
section of your Cargo.toml
.
Modules§
- example
- This module shows an example of code generated by the
numid
macro. IT MUST NOT BE USED OUTSIDE THIS CRATE.
Macros§
- numid
- Examples