ValidateFromParts

Trait ValidateFromParts 

Source
pub trait ValidateFromParts<Parts>: Validate {
    // Required methods
    fn validate_parts(parts: &Parts) -> Result<(), Self::Error>;
    fn from_parts(parts: Parts) -> Self;
}
Expand description

Represents a type that can be constructed and validated from Parts

That can be particularly useful when vaidating Parts is cheaper than validating Self.

§Example

Suppose you have a struct KeyShare that consists of DirtyCoreKeyShare and some AuxData. In order to validate KeyShare, both core key share and aux data need to be validated separately and then they need to be checked for consistency. Now, if you already have Valid<DirtyKeyShare> and Valid<AuxData>, then you can skip their validation and only check that they’re consistent.

use key_share::{Valid, Validate, ValidateFromParts};
use generic_ec::Curve;

pub struct KeyShare<E: Curve> {
    core: key_share::DirtyCoreKeyShare<E>,
    aux: AuxData,
}

// Validation for the whole key share can be expensive
impl<E: Curve> Validate for KeyShare<E> {
    type Error = InvalidKeyShare;
    fn is_valid(&self) -> Result<(), Self::Error> {
        self.core.is_valid()?;
        self.aux.is_valid()?;
        check_consistency(&self.core, &self.aux)
    }
}
fn check_consistency<E: Curve>(
    core: &key_share::DirtyCoreKeyShare<E>,
    aux: &AuxData,
) -> Result<(), InvalidKeyShare> {
    // check that `core` and `aux` seem to match each other
}

// Sometimes, we already validated that `core` and `aux` are valid, so we can perform cheaper validation:
impl<E: Curve> ValidateFromParts<(Valid<key_share::DirtyCoreKeyShare<E>>, Valid<AuxData>)>
    for KeyShare<E>
{
    fn validate_parts(parts: &(Valid<key_share::DirtyCoreKeyShare<E>>, Valid<AuxData>)) -> Result<(), Self::Error> {
        check_consistency(&parts.0, &parts.1)
    }
    fn from_parts(parts: (Valid<key_share::DirtyCoreKeyShare<E>>, Valid<AuxData>)) -> Self {
        Self { core: parts.0.into_inner(), aux: parts.1.into_inner() }
    }
}

Required Methods§

Source

fn validate_parts(parts: &Parts) -> Result<(), Self::Error>

Validates parts

Note: implementation must guarantee that if T::validate_parts(parts).is_ok() then T::from_parts(parts).is_valid().is_ok()

Source

fn from_parts(parts: Parts) -> Self

Constructs Self from parts

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§