Skip to main content

Validate

Trait Validate 

Source
pub trait Validate {
    // Provided method
    fn validate(&self) -> Result<(), &'static str> { ... }
}
Expand description

A configuration that can reject itself.

The same idea as the validate argument in the std crate: every field can be individually valid and the whole still wrong — a window that ends before it starts, a buffer larger than the RAM on the part.

Implemented for every T by default, accepting everything, so implementing it is opt-in:

struct Settings {
    low_ms: u32,
    high_ms: u32,
}

impl Validate for Settings {
    fn validate(&self) -> Result<(), &'static str> {
        if self.low_ms >= self.high_ms {
            return Err("low_ms must be below high_ms");
        }

        Ok(())
    }
}

The error is a &'static str rather than a String: there is no allocator, and a fixed message is what a device can log anyway.

Provided Methods§

Source

fn validate(&self) -> Result<(), &'static str>

Rejects a configuration that is not usable.

§Errors

A short, static description of what is wrong with it.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§