Skip to main content

GType

Struct GType 

Source
pub struct GType<T, V = NoValidation> { /* private fields */ }
Expand description

A validated value.

GType<T, V> wraps a value of type T and guarantees that it satisfied validator V at construction time.

Validation is performed by GType::try_new. Once constructed, the contained value cannot be modified directly, preserving the validator’s invariants.

§Examples

use g_type::GType;

let value = GType::<u32>::try_new(42).unwrap();

assert_eq!(*value.as_ref(), 42);

With a custom validator:

use core::convert::Infallible;
use g_type::{GType, Validator};

struct Percent;

impl Validator<u8> for Percent {
    type Target = u8;
    type Error = Infallible;

    fn max() -> Option<&'static Self::Target> {
        Some(&100)
    }
}

assert!(GType::<u8, Percent>::try_new(50).is_ok());
assert!(GType::<u8, Percent>::try_new(150).is_err());

Implementations§

Source§

impl<T: PartialOrd<V::Target>, V: Validator<T>> GType<T, V>

Source

pub fn try_new(value: T) -> Result<Self, GTypeError<V::Error>>

Attempts to create a validated value.

Returns an error if the value violates the validator’s minimum bound, maximum bound, or custom validation rules.

Source

pub const fn as_ref(&self) -> &T

Returns a shared reference to the underlying value.

Source

pub fn into_inner(self) -> T

Consumes the wrapper and returns the underlying value.

Source

pub fn inspect<F>(self, func: F) -> Self
where F: FnOnce(&T),

Calls a function with a reference to the contained value.

Returns self unchanged.

This is primarily useful for debugging and logging in method chains.

Source

pub fn map<U, UV, F>( self, func: F, ) -> Result<GType<U, UV>, GTypeError<UV::Error>>
where U: PartialOrd<UV::Target>, UV: Validator<U>, F: FnOnce(T) -> U,

Transforms the contained value into another validated type.

The transformed value is validated using the destination validator before being returned.

This behaves similarly to Option::map and Result::map.

Source

pub fn and_then<U, UV, F>( self, func: F, ) -> Result<GType<U, UV>, GTypeError<UV::Error>>
where U: PartialOrd<UV::Target>, UV: Validator<U>, F: FnOnce(T) -> Result<GType<U, UV>, GTypeError<UV::Error>>,

Chains another fallible validated transformation.

This behaves similarly to Option::and_then and Result::and_then.

Trait Implementations§

Source§

impl<T, V> AsRef<T> for GType<T, V>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T, V> Borrow<T> for GType<T, V>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T, V> Clone for GType<T, V>
where T: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T, V> Copy for GType<T, V>
where T: Copy,

Source§

impl<T, V> Debug for GType<T, V>
where T: Debug,

Source§

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

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

impl<'de, T, V> Deserialize<'de> for GType<T, V>
where T: PartialOrd<V::Target> + Deserialize<'de>, V: Validator<T>, V::Target: PartialOrd<T> + Debug, V::Error: Display,

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, V> Display for GType<T, V>
where T: Display,

Source§

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

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

impl<T, V> Eq for GType<T, V>
where T: Eq,

Source§

impl<T, V> Hash for GType<T, V>
where T: Hash,

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, V> Ord for GType<T, V>
where T: Ord,

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

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

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

impl<T, U, LHSV, RHSV> PartialEq<GType<U, RHSV>> for GType<T, LHSV>
where T: PartialEq<U>, U: PartialEq<T>,

Source§

fn eq(&self, other: &GType<U, RHSV>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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, U, LHSV, RHSV> PartialOrd<GType<U, RHSV>> for GType<T, LHSV>
where T: PartialOrd<U>, U: PartialOrd<T>,

Source§

fn partial_cmp(&self, other: &GType<U, RHSV>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T, V> Serialize for GType<T, V>
where T: Serialize,

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

Auto Trait Implementations§

§

impl<T, V> Freeze for GType<T, V>
where T: Freeze,

§

impl<T, V> RefUnwindSafe for GType<T, V>

§

impl<T, V> Send for GType<T, V>
where T: Send, V: Send,

§

impl<T, V> Sync for GType<T, V>
where T: Sync, V: Sync,

§

impl<T, V> Unpin for GType<T, V>
where T: Unpin, V: Unpin,

§

impl<T, V> UnsafeUnpin for GType<T, V>
where T: UnsafeUnpin,

§

impl<T, V> UnwindSafe for GType<T, V>
where T: UnwindSafe, V: 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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