Expand description
A fast trait for checking whether a value is the default, with convenient derive support for custom types.
The default value is defined as the value returned by the Default
trait. Therefore, any implementation of IsDefault must ensure that
&self == &Self::default() holds true.
Unlike the naive comparison &self == &Self::default(), it directly
evaluates the type’s internal fields without the overhead of
constructing a new instance.
§Features
| Feature | Default | Description |
|---|---|---|
derive | yes | Derive trait for a type |
std | yes | Implements for std-types |
Nightly-only:
| Nightly Feature | Default | Description |
|---|---|---|
nightly | no | Enable all below nightly features |
ascii_char | no | Core ascii_char |
bstr | no | Std bstr |
f16 | no | Core f16 |
f128 | no | Core f128 |
§Derive
The IsDefault trait is already implemented for most core and std
types that implement Default. For custom types, you can derive
IsDefault using derive.
§Structs
A struct can derive IsDefault if all of its fields implement
IsDefault:
#[derive(IsDefault)]
struct Unit;
assert!(Unit.is_default());
#[derive(IsDefault)]
struct Wrapper(u8);
assert!(Wrapper(0).is_default());
assert!(!Wrapper(1).is_default());
#[derive(IsDefault)]
struct Point { x: i16, y: f32 }
assert!(Point{ x: 0, y: 0.0 }.is_default());
assert!(!Point{ x: 1, y: 0.0 }.is_default());
assert!(!Point{ x: 0, y: 1.1 }.is_default());§Enums
When deriving IsDefault for an enum, you must specify which unit
variant should be considered the default. This is done by applying
the #[is_default] or #[default] attribute to the variant:
#[derive(IsDefault)]
enum A {
#[is_default]
X,
Y,
}
assert!(A::X.is_default());
assert!(!A::Y.is_default());#[default] attribute possible to derive both Default and
IsDefault:
#[derive(Default, IsDefault)]
enum B {
X,
#[default]
Y,
}
assert!(!B::X.is_default());
assert!(B::Y.is_default());
assert!(matches!(B::default(), B::Y));You can also derive IsDefault for enums that implement both Default
and PartialEq. This approach is more general but may be less
efficient, since a new value must be allocated for comparison:
#[derive(PartialEq, IsDefault)]
enum C {
X(u8),
Y,
}
impl Default for C {
fn default() -> C {
C::X(0)
}
}
assert!(C::X(0).is_default());
assert!(!C::X(1).is_default());§no_std
For no_std builds, add is_default to your Cargo.toml with default
features disabled:
# Cargo.toml
[dependencies]
is_default = { version = "1", default-features = false, features = ["derive"] }Traits§
- IsDefault
- Checks whether a value is equal to its type’s default.
Derive Macros§
- IsDefault
- Derive macro generating an impl of the trait
IsDefault.