Struct uid::Id[][src]

#[repr(transparent)]
pub struct Id<T> { /* fields omitted */ }
Expand description

A struct representing IDs usable for various purposes.

Except for Debug and Display which are implemented unconditionally, the type will only implement Clone, Copy, Eq, Ord, PartialEq, PartialOrd, and Hash if the provided T implements them.

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, Eq, PartialEq)]
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)]
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 ID using the given value.

Panics

This constructor panics if an overflow of the underlying counter occurred.

Safety
  • id must not be zero
  • id should be unique with respect to other IDs created for this T to preserve the invariant that IDs are unique

Create a new unique ID.

Panics

This constructor panics if an overflow of the underlying counter occurred.

Retrieve the underlying integer 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

Create a new unique ID.

Format the ID with 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 !=.

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

Performs the conversion.

Performs the conversion.

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.