Struct uid::Id

source ·
pub struct Id<T>where
    T: Copy,
{ /* private fields */ }
Expand description

A struct representing IDs usable for various purposes.

Note that Rust only truly provides the implementations of the various traits we derive from when T also provides them. Note furthermore that we want all ID objects to be lightweight and, hence, require the implementation of Copy for T (which we do not for all the other, optional, traits).

Examples

A commonly seen pattern for creating of a type Id that is unique may look as follows:

use uid::Id as IdT;

#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
struct T(());

type Id = IdT<T>;

let id1 = Id::new();
let id2 = Id::new();

assert_ne!(id1, id2)

In this example the type T is just an arbitrary type, but it allows us to create distinct ID types. For example, when another ID type is required for a different purpose, that can be easily created:

#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
struct U(());

type Key = IdT<U>;

// `Key` and `Id` are fundamentally different types, with no
// allowed interaction between each other. That is, Rust's type
// system will prevent accidental usage of one in place of the
// other. The same can be said about the relationship to built-in
// numeric types such as `usize` or `u64`.

Implementations§

Create a new unique Id.

Retrieve the underlying usize value.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Format the Id into the given formatter.

Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
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

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 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.