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, Ids are 16 bytes long. This should provide good collision resistance – i.e. you can assume that 2 randomly generated Ids 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> PartialOrd for Id<Label, SIZE>
impl<Label, const SIZE: usize> PartialOrd for Id<Label, SIZE>
impl<Label, const SIZE: usize> Copy for Id<Label, SIZE>
impl<Label, const SIZE: usize> Eq for Id<Label, SIZE>
impl<Label, const SIZE: usize> Send for Id<Label, SIZE>
impl<Label, const SIZE: usize> Sync for Id<Label, SIZE>
Auto Trait Implementations§
impl<Label, const SIZE: usize> Freeze for Id<Label, SIZE>
impl<Label, const SIZE: usize> RefUnwindSafe for Id<Label, SIZE>where
Label: RefUnwindSafe,
impl<Label, const SIZE: usize> Unpin for Id<Label, SIZE>where
Label: Unpin,
impl<Label, const SIZE: usize> UnwindSafe for Id<Label, SIZE>where
Label: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more