Trait CustomKnownOption

Source
pub trait CustomKnownOption:
    'static
    + Send
    + Sized {
    type OptDeserializer: DeserializeParam<Option<Self>>;

    const OPT_DE: Self::OptDeserializer;
}
Expand description

Customizes the well-known deserializer for Option<Self>, similarly to WellKnown.

This trait usually implemented automatically via the WellKnownOption blanket impl. A manual implementation is warranted for corner cases:

  • Allow only Option<T> as a param type, but not T on its own.
  • Convert additional values to None when deserializing Option<T>.

Tip: It usually makes sense to have Optional wrapper for the used deserializer to handle missing / null values.

§Examples

§Allow type only in Option<_> wrapper

use smart_config::{de::{CustomKnownOption, Optional, Serde}, DescribeConfig};

#[derive(Serialize, Deserialize)]
struct OnlyInOption(u64);

impl CustomKnownOption for OnlyInOption {
    type OptDeserializer = Optional<Serde![int]>;
    const OPT_DE: Self::OptDeserializer = Optional(Serde![int]);
}

#[derive(DescribeConfig)]
struct TestConfig {
    /// Valid parameter.
    param: Option<OnlyInOption>,
}

…while this fails:

#[derive(DescribeConfig)]
struct BogusConfig {
    /// Bogus parameter: `OnlyInOption` doesn't implement `WellKnown`
    bogus: OnlyInOption,
}

Required Associated Constants§

Source

const OPT_DE: Self::OptDeserializer

Deserializer instance.

Required Associated Types§

Source

type OptDeserializer: DeserializeParam<Option<Self>>

Type of the deserializer used for Option<Self>.

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.

Implementations on Foreign Types§

Source§

impl CustomKnownOption for Duration

Implementors§