Skip to main content

Validator

Trait Validator 

Source
pub trait Validator<T> {
    type Target: PartialOrd<Self::Target> + PartialOrd<T> + ?Sized + 'static;
    type Error;

    // Provided methods
    fn min() -> Option<&'static Self::Target> { ... }
    fn max() -> Option<&'static Self::Target> { ... }
    fn validate(_: &T) -> Result<(), Self::Error> { ... }
}
Expand description

Validation strategy for GType.

A validator can:

Validation is performed when constructing a GType using GType::try_new.

§Note

Validator::Target is comparison target for checking bounds for T. Its value must be comparable to itself and T. Putting non-comparable value(e.g f32::NAN) might give error construction.

§Example

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)
    }
}

let value = GType::<u8, Percent>::try_new(42);
assert!(value.is_ok());

Required Associated Types§

Source

type Target: PartialOrd<Self::Target> + PartialOrd<T> + ?Sized + 'static

Target type for bounds comparison.

§Examples
  • u32 -> u32
  • String -> str
  • Vec<T> -> [T] or [T; N]
Source

type Error

Validation error type.

Provided Methods§

Source

fn min() -> Option<&'static Self::Target>

Minimum value in range, inclusive.

Source

fn max() -> Option<&'static Self::Target>

Maximum value in range, inclusive.

Source

fn validate(_: &T) -> Result<(), Self::Error>

Validation logics.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: PartialOrd + 'static> Validator<T> for ()

Implementors§