A trait for checking if a value is default, with easy derive support for custom types.
Example, instead of is_none
for Option
and is_empty
for Vec
can be used is_default
for all.
assert!;
assert!;
use IsDefault;
assert!;
assert!;
The IsDefault
trait is implemented for most standard types that
has Default
impl. With the derive
feature, you can easily generate
implementations for your own types:
Derive
To use the derive macro, add the dependency with the derive
feature
in your Cargo.toml
:
# Cargo.toml
[]
= { = "0.1", = ["derive"] }
Structs
A struct can derive IsDefault
if all its fields implement IsDefault
.
use IsDefault;
;
assert!;
;
assert!;
assert!;
assert!;
assert!;
assert!;
Enums
When using #[derive(IsDefault)] on an enum, you need to choose which unit variant will be default. You do this by placing the #[is_default] OR #[default] attribute on the variant.
This makes it possible to derive both Default
and IsDefault
using
the same attribute.
use IsDefault;
assert!;
assert!;
assert!;
assert!;
assert!;
Also #[derive(IsDefault)] on an enum possible if it implements both
Default
and PartialEq
. However, this implementation may be
inefficient, since a new Self
object must be allocated for comparison.
use IsDefault;
assert!;
assert!;