Struct id30::Id30

source ·
pub struct Id30(/* private fields */);
Expand description

An implementation of the Id30 encoding scheme as documented at the crate root.

This type holds a 30 bit integer as a 32 bit integer, of which the two most significant bits are always zero. The type has the same representation as u32. Debug-formatting includes both the Id30 encoding and the integer representation.

Ordering, as implemented for the PartialOrd and Ord traits, orders by numerical value of the underlying integer. This is the same order as the lexicographical ordering of the string representation.

There are many ways to create instances of Id30:

  • Via the TryFrom trait from either a u32 or an i32:
    let id: Id30 = 1234.try_into().expect("1234 is in range");
    assert_eq!(Id30::try_from(1 << 31), Err(id30::OutOfRangeError));
  • Parsing, via the FromStr trait:
    let id: id30::Id30 = "hrga2q".parse().unwrap();
    Tip: Parse to the Id30Parse type if you need to know whether the text representation is canonical or alternate.
  • With feature rand, via the Distribution trait:
    use rand::{distributions::Standard, prelude::*};
    let mut rng = rand::thread_rng();
    
    let id: Id30 = rng.gen();
    let ids: Vec<Id30> = Standard.sample_iter(rng).take(10).collect();
  • With feature serde, via deserialization
  • With feature diesel, as output from queries

An Id30 can be converted directly to u32 and i32 via the From trait:

let id: Id30 = 1234.try_into().unwrap();
assert_eq!(u32::from(id), 1234);

To generate the text representation:

  • format an Id30 value via the Display trait:
    let id: id30::Id30 = "j9yceq".parse().unwrap();
    assert_eq!(&id.to_string(), "j9yceq");
    assert_eq!(&format!("/path/to/{id}"), "/path/to/j9yceq");
  • or, with feature serde, via serialization

§Integrations with other crates

  • crate rand via feature rand08 (alias rand):

    Id30 implements Distribution, enabling generation of random Id30 values.

  • crate serde via feature serde1 (alias serde):

    Id30 implements Serialize and Deserialize, enabling serialization and deserialization of Id30 values as text through serde.

  • crate diesel via feature diesel2 (alias diesel):

    Id30 implements FromSql and Queryable, enabling deserialization of integers from the database as Id30 values, and ToSql, enabling serialization of Id30 values as integers in the database. Additionally, AsExpression is implemented, enabling the usage of Id30 values in diesel query builder expressions.

Trait Implementations§

source§

impl<'expr> AsExpression<Integer> for &'expr Id30

§

type Expression = Bound<Integer, &'expr Id30>

The expression being returned
source§

fn as_expression(self) -> Self::Expression

Perform the conversion
source§

impl AsExpression<Integer> for Id30

§

type Expression = Bound<Integer, Id30>

The expression being returned
source§

fn as_expression(self) -> Self::Expression

Perform the conversion
source§

impl<'expr> AsExpression<Nullable<Integer>> for &'expr Id30

§

type Expression = Bound<Nullable<Integer>, &'expr Id30>

The expression being returned
source§

fn as_expression(self) -> Self::Expression

Perform the conversion
source§

impl AsExpression<Nullable<Integer>> for Id30

§

type Expression = Bound<Nullable<Integer>, Id30>

The expression being returned
source§

fn as_expression(self) -> Self::Expression

Perform the conversion
source§

impl Clone for Id30

source§

fn clone(&self) -> Id30

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Id30

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Id30

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Id30

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Distribution<Id30> for Standard

source§

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Id30

Generate a random value of T, using rng as the source of randomness.
source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
where R: Rng, Self: Sized,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more
source§

fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F Read more
source§

impl From<Id30> for i32

source§

fn from(value: Id30) -> Self

Converts to this type from the input type.
source§

impl From<Id30> for u32

source§

fn from(value: Id30) -> Self

Converts to this type from the input type.
source§

impl<DB> FromSql<Integer, DB> for Id30
where DB: Backend, i32: FromSql<Integer, DB>,

source§

fn from_sql(bytes: DB::RawValue<'_>) -> Result<Self>

See the trait documentation.
source§

fn from_nullable_sql( bytes: Option<<DB as Backend>::RawValue<'_>> ) -> Result<Self, Box<dyn Error + Send + Sync>>

A specialized variant of from_sql for handling null values. Read more
source§

impl FromStr for Id30

§

type Err = ParseError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Id30

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Id30

source§

fn cmp(&self, other: &Id30) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Id30

source§

fn eq(&self, other: &Id30) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Id30

source§

fn partial_cmp(&self, other: &Id30) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<__ST, __DB> Queryable<__ST, __DB> for Id30
where __DB: Backend, __ST: SingleValue, Self: FromSql<__ST, __DB>,

§

type Row = Id30

The Rust type you’d like to map from. Read more
source§

fn build(row: Self::Row) -> Result<Self>

Construct an instance of this type
source§

impl Serialize for Id30

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<DB> ToSql<Integer, DB> for Id30
where DB: Backend, i32: ToSql<Integer, DB>,

source§

fn to_sql(&self, out: &mut Output<'_, '_, DB>) -> Result

See the trait documentation.
source§

impl<__DB> ToSql<Nullable<Integer>, __DB> for Id30
where __DB: Backend, Self: ToSql<Integer, __DB>,

source§

fn to_sql<'__b>(&'__b self, out: &mut Output<'__b, '_, __DB>) -> Result

See the trait documentation.
source§

impl TryFrom<i32> for Id30

§

type Error = OutOfRangeError

The type returned in the event of a conversion error.
source§

fn try_from(value: i32) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<u32> for Id30

§

type Error = OutOfRangeError

The type returned in the event of a conversion error.
source§

fn try_from(value: u32) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Copy for Id30

source§

impl Eq for Id30

source§

impl StructuralPartialEq for Id30

Auto Trait Implementations§

§

impl Freeze for Id30

§

impl RefUnwindSafe for Id30

§

impl Send for Id30

§

impl Sync for Id30

§

impl Unpin for Id30

§

impl UnwindSafe for Id30

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> IntoSql for T

source§

fn into_sql<T>(self) -> Self::Expression

Convert self to an expression for Diesel’s query builder. Read more
source§

fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression

Convert &self to an expression for Diesel’s query builder. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,