| Crates.io | isx |
| lib.rs | isx |
| version | 0.1.5 |
| created_at | 2024-07-02 12:09:25.852487+00 |
| updated_at | 2025-07-23 15:41:33.900091+00 |
| description | Traits for checking certain conditions of values |
| homepage | |
| repository | https://github.com/ctron/is-x |
| max_upload_size | |
| id | 1289540 |
| size | 19,903 |
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
For the IsDefault trait:
use isx::IsDefault;
fn test() {
assert!(false.is_default());
assert!(true.is_not_default());
}
For the IsEmpty trait:
use isx::IsEmpty;
fn test() {
assert!(vec![].is_empty());
assert!(None::<()>.is_empty());
}
Using a derive this can be implemented easily for custom types:
use isx::{IsDefault, IsEmpty};
#[derive(Default, IsDefault, IsEmpty)]
struct MyStruct {
s: String,
}
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,
}
#[no_std]This crate has a std and alloc feature, which are enabled by default. However, you can make it #[no_std] by
using default_features = true. It then is possible to enable support for alloc types (like String and Vec)
using the alloc feature.