https://github.com/ctron/isx
Traits for checking certain conditions of values
https://github.com/ctron/isx
Last synced: about 1 year ago
JSON representation
Traits for checking certain conditions of values
- Host: GitHub
- URL: https://github.com/ctron/isx
- Owner: ctron
- License: mit
- Created: 2024-07-02T12:08:25.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-02T12:19:43.000Z (almost 2 years ago)
- Last Synced: 2025-03-19T14:49:53.115Z (about 1 year ago)
- Language: Rust
- Size: 2.93 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# is-x
Traits for checking certain conditions of values: is empty? is default?
Also see:
## Examples
For the `IsDefault` trait:
```rust
use isx::prelude::*;
fn test () {
assert!(false.is_default());
assert!(true.is_not_default());
}
```
For the `IsEmpty` trait:
```rust
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:
```rust
#[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,
#[serde(default, skip_serializing_if = "IsEmpty::is_empty")]
map: HashMap,
#[serde(default, skip_serializing_if = "IsEmpty::is_empty")]
optional: Option,
#[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:
```rust
#[derive(Default, serde::Serialize, serde::Deserialize)]
struct MyData {
#[serde(default, skip_serializing_empty)]
list: Vec,
#[serde(default, skip_serializing_empty)]
map: HashMap,
#[serde(default, skip_serializing_default)]
flag: bool,
}
```
## ToDo
* Implement a derive for `IsDefault`
* Implement a derive for `IsEmpty`