pub struct Id<Label, const SIZE: usize = 16> { /* private fields */ }
Expand description
An identifier with a statically associated label type
Use the Label
generic to describe what this Id
is for. If you have an existing struct, you can use that:
struct User {
name: String,
id: Id<User>,
}
// It might be convenient to define a type alias:
type UserId = Id<User>;
By default, Id
s are 16 bytes long. This should provide good collision resistance – i.e. you can assume that 2 randomly generated Id
s are distinct for all practical purposes. You can customize the Id
size with the SIZE
const generic:
type TinyId<Label> = Id<Label, 4>;
let _id: TinyId<User> = [0, 0, 0, 42].into();
Implementations§
source§impl<Label, const SIZE: usize> Id<Label, SIZE>
impl<Label, const SIZE: usize> Id<Label, SIZE>
sourcepub fn new_random() -> Self
pub fn new_random() -> Self
Generate a new random Id
struct User;
let a = Id::<User>::new_random();
let b = Id::<User>::new_random();
// For all practical purposes, we can assume that 2 random IDs will be different
// (As long as you use the default ID size or larger)
assert_ne!(a, b);
sourcepub fn as_bytes(&self) -> &[u8; SIZE]
pub fn as_bytes(&self) -> &[u8; SIZE]
The bytes representing this Id
struct Product;
let id: Id<Product, 4> = [1,2,3,4].into();
assert_eq!(id.as_bytes(), [1,2,3,4].as_slice());
sourcepub fn cast_label<NewLabel>(self) -> Id<NewLabel, SIZE>
pub fn cast_label<NewLabel>(self) -> Id<NewLabel, SIZE>
Change the label type of this Id
struct User;
struct Player;
let user_id :Id<User>= Id::new_random();
let player_id: Id<Player> = user_id.cast_label();
assert_eq!(user_id.as_bytes(), player_id.as_bytes());
Trait Implementations§
source§impl<Label, const SIZE: usize> Debug for Id<Label, SIZE>
impl<Label, const SIZE: usize> Debug for Id<Label, SIZE>
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the Id
for debugging purposes
let id: Id<String> = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255].into();
assert_eq!(
format!("{:?}", id),
"Id<alloc::string::String>(000000000000000000000000000000ff)"
);
Note: the Debug representation may change in the future
source§impl<'de, Label, const SIZE: usize> Deserialize<'de> for Id<Label, SIZE>
impl<'de, Label, const SIZE: usize> Deserialize<'de> for Id<Label, SIZE>
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl<Label, const SIZE: usize> Display for Id<Label, SIZE>
impl<Label, const SIZE: usize> Display for Id<Label, SIZE>
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Produces the hex representation of the Id
let id: Id<String> = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255].into();
assert_eq!(
format!("{:}", id),
"000000000000000000000000000000ff"
);
Note: the Display representation may change in the future
source§impl<Label, const SIZE: usize> Ord for Id<Label, SIZE>
impl<Label, const SIZE: usize> Ord for Id<Label, SIZE>
source§impl<Label, const SIZE: usize> PartialEq<Id<Label, SIZE>> for Id<Label, SIZE>
impl<Label, const SIZE: usize> PartialEq<Id<Label, SIZE>> for Id<Label, SIZE>
source§impl<Label, const SIZE: usize> PartialOrd<Id<Label, SIZE>> for Id<Label, SIZE>
impl<Label, const SIZE: usize> PartialOrd<Id<Label, SIZE>> for Id<Label, SIZE>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read more