pub trait PartialDefault: Sized {
    // Required method
    fn partial_default() -> Self;
}
Expand description

A trait for giving a type a non-useful default value.

The standard Default trait documents its purpose as providing a “useful” default value. However, some types (such as a Credential) don’t have meaningful defaults, and yet there are still uses for a known-initialized value:

PartialDefault satisfies this niche. A type that implements PartialDefault can provide a value that is safe to drop or assign over, but promises nothing else about that value. Using it in any other way may panic or produce unexpected results, though it should not be possible to violate memory safety. That is, partial_default should always be a “safe” function in the Rust sense.

The name “PartialDefault” is by analogy to PartialEq/Eq and PartialOrd/Ord in the standard library: just as PartialEq provides weaker guarantees than Eq and PartialOrd provides weaker guarantees than Ord, PartialDefault provides weaker guarantees than Default. And just as every Eq-implementing type provides PartialEq, every Default-implementing type provides PartialDefault.

Derivable

Like Default, PartialDefault supports #[derive] if all fields implement PartialDefault. The value used for a field can be overridden using the #[partial_default(value = "alternative()")] syntax, where alternative() is a Rust expression that evaluates to the correct type.

By default, all generic parameters must implement PartialDefault to support deriving PartialDefault. You can override this by adding #[partial_default(bound = "T: MyTrait")] to the type, which replaces any bounds inferred by PartialDefault. Use an empty string to impose no restrictions at all.

Required Methods§

source

fn partial_default() -> Self

Returns a value that can be safely dropped or assigned over.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T: Default> PartialDefault for T

If a type does implement Default, its PartialDefault implementation will match.