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
Trait Implementations
sourceimpl<T: Ord> Ord for Id<T>where
T: Copy,
impl<T: Ord> Ord for Id<T>where
T: Copy,
1.21.0 · sourcefn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl<T: PartialEq> PartialEq<Id<T>> for Id<T>where
T: Copy,
impl<T: PartialEq> PartialEq<Id<T>> for Id<T>where
T: Copy,
sourceimpl<T: PartialOrd> PartialOrd<Id<T>> for Id<T>where
T: Copy,
impl<T: PartialOrd> PartialOrd<Id<T>> for Id<T>where
T: Copy,
sourcefn partial_cmp(&self, other: &Id<T>) -> Option<Ordering>
fn partial_cmp(&self, other: &Id<T>) -> Option<Ordering>
1.0.0 · sourcefn 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 moreimpl<T: Copy> Copy for Id<T>where
T: Copy,
impl<T: Eq> Eq for Id<T>where
T: Copy,
impl<T> StructuralEq for Id<T>where
T: Copy,
impl<T> StructuralPartialEq for Id<T>where
T: Copy,
Auto Trait Implementations
impl<T> RefUnwindSafe for Id<T>where
T: RefUnwindSafe,
impl<T> Send for Id<T>where
T: Send,
impl<T> Sync for Id<T>where
T: Sync,
impl<T> Unpin for Id<T>where
T: Unpin,
impl<T> UnwindSafe for Id<T>where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more