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§
source§impl<T> Ord for Id<T>where
T: Copy + Ord,
impl<T> Ord for Id<T>where
T: Copy + Ord,
source§impl<T> PartialEq<Id<T>> for Id<T>where
T: Copy + PartialEq,
impl<T> PartialEq<Id<T>> for Id<T>where
T: Copy + PartialEq,
source§impl<T> PartialOrd<Id<T>> for Id<T>where
T: Copy + PartialOrd,
impl<T> PartialOrd<Id<T>> for Id<T>where
T: Copy + PartialOrd,
1.0.0 · source§fn 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 more