2 releases
0.1.1 | Jul 2, 2024 |
---|---|
0.1.0 | Jul 2, 2024 |
#786 in Data structures
7KB
108 lines
is-x
Traits for checking certain conditions of values: is empty? is default?
Also see: https://internals.rust-lang.org/t/traits-for-is-empty-and-or-is-default/21114
Examples
For the IsDefault
trait:
use isx::prelude::*;
fn test () {
assert!(false.is_default());
assert!(true.is_not_default());
}
For the IsEmpty
trait:
use isx::prelude::*;
fn test () {
assert!(vec![].is_empty());
assert!(None::<()>.is_empty());
}
Why?
Because in same cases, it would be great to have a common pattern:
#[derive(Default, IsDefault, IsEmpty, serde::Serialize, serde::Deserialize)]
struct MySubData {
// […]
}
#[derive(Default, serde::Serialize, serde::Deserialize)]
struct MyData {
#[serde(default, skip_serializing_if = "IsEmpty::is_empty")]
list: Vec<String>,
#[serde(default, skip_serializing_if = "IsEmpty::is_empty")]
map: HashMap<String, String>,
#[serde(default, skip_serializing_if = "IsEmpty::is_empty")]
optional: Option<String>,
#[serde(default, skip_serializing_if = "IsDefault::is_default")]
flag: bool,
#[serde(default, skip_serializing_if = "IsEmpty::is_empty")]
sub_data: MySubData,
}
Having that in std
or core
, might actually convince people to go for this:
#[derive(Default, serde::Serialize, serde::Deserialize)]
struct MyData {
#[serde(default, skip_serializing_empty)]
list: Vec<String>,
#[serde(default, skip_serializing_empty)]
map: HashMap<String, String>,
#[serde(default, skip_serializing_default)]
flag: bool,
}
ToDo
- Implement a derive for
IsDefault
- Implement a derive for
IsEmpty