Trait smart_config::de::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 Types§

source

type OptDeserializer: DeserializeParam<Option<Self>>

Type of the deserializer used for Option<Self>.

Required Associated Constants§

source

const OPT_DE: Self::OptDeserializer

Deserializer instance.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl CustomKnownOption for Duration

Implementors§