Id#
The id of a task, link, calendar, resource or assignment. An Id holds a number or a string and compares like a JavaScript object key, so 1 and "1" are the same id. Build one with .into() from a &str, String, f64 or i32 wherever an Id is expected, or name a variant directly.
Declaration#
Id doesn't implement Default, Copy or Ord.
Variants#
| Variant | Description |
|---|---|
Num(f64) |
Numeric id. Must be finite: a NaN or infinite id in ProjectData fails validation with an InputError of kind Validation, message id must be finite. |
Str(String) |
String id. Any string, including the empty string. |
Methods#
key#
Returns the normalized key: the string a JavaScript engine uses for this id as an object key.
Str(s)returnssunchanged.Num(n)returnsnformatted the way JavaScript formats a number. Whole numbers print without a fraction (1.0โ"1"),0.0and-0.0both print"0", and values from1e21up or below1e-6use exponent notation (1e21โ"1e+21").
Returns - the normalized key.
norm#
Same as key.
Returns - the normalized key.
Equality and hashing#
Two ids are equal when their key values are equal, and equal ids hash the same:
| Comparison | Result |
|---|---|
Id::Num(1.0) == Id::from("1") |
true |
Id::Num(-0.0) == Id::from("0") |
true |
Id::Num(1.5) == Id::from("1.5") |
true |
Id::from("01") == Id::Num(1.0) |
false (the key of 1.0 is "1") |
A HashMap<Id, _> or HashSet<Id> treats 1 and "1" as one key.
Numeric ids keep f64 precision limits. Whole numbers above 2^53 that round to the same f64 are the same id.
Display#
Display writes the key, so format!("{id}") and id.to_string() return the normalized key.
Serialization#
With the serde feature, Id implements Serialize and Deserialize:
Strserializes as a JSON string.Numserializes as a JSON integer when it's whole (1.0โ1) and as a JSON number otherwise.- Serializing a non-finite
Numfails withid must be finite. - A JSON string deserializes to
Str, and any JSON number toNum. Other JSON values fail with the expectationa string or finite number id.
Example#
use HashSet;
use Id;
Related#
TaskData- tasks are keyed and parented byIdSchedulingConfig-root_idis anIdcompared by keyProjectData- ids must be unique within each collection- Project data - how numeric and string ids name the same record
- Quick start - builds ids with
.into()in a first schedule