Struct Field

Source
pub struct Field<T: TypeMarker> { /* private fields */ }
Expand description

An generic type-safe object ID field (a wrapped u64).

When serialized with Serde, the number is automatically encrypted and encoded into a URL safe string. Deserialization decodes and decrypts the string back to an integer. The string has an object type specific prefix defined in the type marker’s fn name().

Traits are also provided for Diesel compatibility with Postgres BigInt fields.

§Examples

use cryptid_rs;
use serde::{Serialize, Deserialize};
use serde_json;

#[derive(Clone, Copy, Debug)]
pub struct ExampleIdMarker;
impl cryptid_rs::TypeMarker for ExampleIdMarker {
    fn name() -> &'static str { "example" }
}

type ExampleId = cryptid_rs::Field<ExampleIdMarker>;

#[derive(serde::Serialize)]
struct Example {
    pub id: ExampleId,
}

cryptid_rs::Config::set_global(cryptid_rs::Config::new(b"your-secure-key"));
let obj = Example {id: ExampleId::from(12345)};
let obj_str = serde_json::to_string(&obj).unwrap();
assert_eq!(obj_str, "{\"id\":\"example_VgwPy6rwatl\"}");

Implementations§

Source§

impl<T: TypeMarker> Field<T>

Source

pub fn from(id: u64) -> Self

Creates a Field<T> value from a u64.

This method converts a u64 into a Field<T>, effectively changing its type.

Source

pub fn encode_uuid(self) -> Uuid

Encrypts the ID into a Uuid value.

Trait Implementations§

Source§

impl<'__expr, T: TypeMarker> AsExpression<BigInt> for &'__expr Field<T>

Source§

type Expression = Bound<BigInt, &'__expr Field<T>>

The expression being returned
Source§

fn as_expression(self) -> <Self as AsExpression<BigInt>>::Expression

Perform the conversion
Source§

impl<T: TypeMarker> AsExpression<BigInt> for Field<T>

Source§

type Expression = Bound<BigInt, Field<T>>

The expression being returned
Source§

fn as_expression(self) -> <Self as AsExpression<BigInt>>::Expression

Perform the conversion
Source§

impl<'__expr, T: TypeMarker> AsExpression<Nullable<BigInt>> for &'__expr Field<T>

Source§

type Expression = Bound<Nullable<BigInt>, &'__expr Field<T>>

The expression being returned
Source§

fn as_expression(self) -> <Self as AsExpression<Nullable<BigInt>>>::Expression

Perform the conversion
Source§

impl<T: TypeMarker> AsExpression<Nullable<BigInt>> for Field<T>

Source§

type Expression = Bound<Nullable<BigInt>, Field<T>>

The expression being returned
Source§

fn as_expression(self) -> <Self as AsExpression<Nullable<BigInt>>>::Expression

Perform the conversion
Source§

impl<T: Clone + TypeMarker> Clone for Field<T>

Source§

fn clone(&self) -> Field<T>

Returns a duplicate 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<T: Debug + TypeMarker> Debug for Field<T>

Source§

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

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

impl<'de, T: TypeMarker> Deserialize<'de> for Field<T>

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<T: TypeMarker> Display for Field<T>

Source§

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

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

impl<T: TypeMarker> From<Field<T>> for u64

Source§

fn from(field: Field<T>) -> Self

Returns the raw u64 value.

Source§

impl<T: TypeMarker> FromSql<BigInt, Pg> for Field<T>

Source§

fn from_sql(bytes: PgValue<'_>) -> 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<T> Queryable<BigInt, Pg> for Field<T>
where T: TypeMarker,

Source§

type Row = <i64 as Queryable<BigInt, Pg>>::Row

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<T: TypeMarker> Serialize for Field<T>

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<T: TypeMarker> ToSql<BigInt, Pg> for Field<T>

Source§

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

See the trait documentation.
Source§

impl<T: TypeMarker, __DB> ToSql<Nullable<BigInt>, __DB> for Field<T>
where __DB: Backend, Self: ToSql<BigInt, __DB>,

Source§

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

See the trait documentation.
Source§

impl<T: Copy + TypeMarker> Copy for Field<T>

Auto Trait Implementations§

§

impl<T> Freeze for Field<T>

§

impl<T> RefUnwindSafe for Field<T>
where T: RefUnwindSafe,

§

impl<T> Send for Field<T>
where T: Send,

§

impl<T> Sync for Field<T>
where T: Sync,

§

impl<T> Unpin for Field<T>
where T: Unpin,

§

impl<T> UnwindSafe for Field<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, ST, DB> FromSqlRow<ST, DB> for T
where T: Queryable<ST, DB>, ST: SqlTypeOrSelectable, DB: Backend, <T as Queryable<ST, DB>>::Row: FromStaticSqlRow<ST, DB>,

Source§

fn build_from_row<'a>( row: &impl Row<'a, DB>, ) -> Result<T, Box<dyn Error + Send + Sync>>

See the trait documentation.
Source§

impl<T, ST, DB> FromStaticSqlRow<ST, DB> for T
where DB: Backend, T: FromSql<ST, DB>, ST: SingleValue,

Source§

fn build_from_row<'a>( row: &impl Row<'a, DB>, ) -> Result<T, Box<dyn Error + Send + Sync>>

See the trait documentation
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
where &'a Self: AsExpression<T>, T: SqlType + TypedExpressionType,

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, ST, DB> StaticallySizedRow<ST, DB> for T
where ST: SqlTypeOrSelectable + TupleSize, T: Queryable<ST, DB>, DB: Backend,

Source§

const FIELD_COUNT: usize = <ST as crate::util::TupleSize>::SIZE

The number of fields that this type will consume.
Source§

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

Source§

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§

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

Source§

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

Source§

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