Struct slid::Id

source ·
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§

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);

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());

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

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

Deserialize this value from the given Serde deserializer. Read more

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

Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
Converts this type into the (usually inferred) input type.
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.