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.
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
[]
= { = "1", = ["derive"] }
Structs
A struct can derive IsDefault
if all its fields implement IsDefault
.
use IsDefault;
;
assert!;
;
assert!;
assert!;
assert!;
assert!;
assert!;
Enums
An enum can derive IsDefault
using either the #[is_default]
OR the
#[default]
attribute. This makes it possible to derive both Default
and IsDefault
using the same attribute.
use IsDefault;
assert!;
assert!;
assert!;
assert!;
assert!;
An enum can also derive IsDefault
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!;