Field

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 both Diesel and SQLx 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.

Source

pub fn decode_uuid(uuid: Uuid) -> Result<Self, Error>

Decrypts a Uuid value back into a Field<T>.

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

Available on crate feature diesel only.
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 + Sync + Send>>

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

impl<T: TypeMarker> FromStr for Field<T>

Source§

type Err = Error

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

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

Source§

fn eq(&self, other: &Field<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Queryable<BigInt, Pg> for Field<T>
where T: TypeMarker,

Available on crate feature diesel only.
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>

Available on crate feature diesel only.
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>

Source§

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

Source§

impl<T: TypeMarker> StructuralPartialEq 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> AggregateExpressionMethods for T

Source§

fn aggregate_distinct(self) -> Self::Output
where Self: DistinctDsl,

DISTINCT modifier for aggregate functions Read more
Source§

fn aggregate_all(self) -> Self::Output
where Self: AllDsl,

ALL modifier for aggregate functions Read more
Source§

fn aggregate_filter<P>(self, f: P) -> Self::Output
where P: AsExpression<Bool>, Self: FilterDsl<<P as AsExpression<Bool>>::Expression>,

Add an aggregate function filter Read more
Source§

fn aggregate_order<O>(self, o: O) -> Self::Output
where Self: OrderAggregateDsl<O>,

Add an aggregate function order Read more
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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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 + Sync + Send>>

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 + Sync + Send>>

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> WindowExpressionMethods for T

Source§

fn over(self) -> Self::Output
where Self: OverDsl,

Turn a function call into a window function call Read more
Source§

fn window_filter<P>(self, f: P) -> Self::Output
where P: AsExpression<Bool>, Self: FilterDsl<<P as AsExpression<Bool>>::Expression>,

Add a filter to the current window function Read more
Source§

fn partition_by<E>(self, expr: E) -> Self::Output
where Self: PartitionByDsl<E>,

Add a partition clause to the current window function Read more
Source§

fn window_order<E>(self, expr: E) -> Self::Output
where Self: OrderWindowDsl<E>,

Add a order clause to the current window function Read more
Source§

fn frame_by<E>(self, expr: E) -> Self::Output
where Self: FrameDsl<E>,

Add a frame clause to the current window function Read more
Source§

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