Crates.io | type-utilities-rs |
lib.rs | type-utilities-rs |
version | 0.1.0 |
source | src |
created_at | 2024-08-10 04:34:09.22654 |
updated_at | 2024-08-10 04:34:09.22654 |
description | Type utilities in Rust. |
homepage | |
repository | |
max_upload_size | |
id | 1331979 |
size | 18,560 |
Type utilities in Rust.
Create new Struct that omit the specified fields
use type_utilities_rs::omit;
// Create a new struct `NewS` with omitted field `b`
#[omit(NewS, [b])]
struct S {
a: i32,
b: &str,
}
// `NewS` will only have field `a`
let _ = NewS { a: 1 };
When the fields dosn't specified, it will be same as the original struct
use type_utilities_rs::omit;
#[omit(NewS)]
struct S<'a> {
a: i32,
b: &'a str,
}
// `NewS` is same as the original struct `S`
let _ = NewS { a: 1, b: "hello" };
Create new struct that pick the specified fields
use type_utilities_rs::pick;
// Create a new struct `NewS` with picked field `b`
#[pick(NewS, [b])]
struct S<'a> {
a: i32,
b: &'a str,
c: f64,
}
// `NewS` will only have field `b`
let _ = NewS { b: "hello" };
When the fields dosn't specified, it will be empty struct
use type_utilities_rs::pick;
#[pick(NewS)]
struct S {
a: i32,
b: f64,
}
// `NewS` will be empty struct
let _ = NewS {};
Change all fields to Option
type
use type_utilities_rs::partial;
// Create a new struct `NewS` with all fields optional
#[partial(NewS)]
struct S<'a> {
a: i32,
b: &'a str,
c: f64,
}
// `NewS` will have all fields optional
let _ = NewS { a: Some(1), b: Some("hello"), c: Some(1.5) };
When the field is already is Option
, it's no effect
use type_utilities_rs::partial;
#[partial(NewS)]
struct S {
a: i32,
b: Option<f64>,
}
let _ = NewS { a: Some(1), b: Some(1.1) };
Unwrap all fields from Option
type
use type_utilities_rs::required;
// Create a new struct `NewS` with all fields optional
#[required(NewS)]
struct S<'a> {
a: Option<i32>,
b: Option<&'a str>,
c: f64,
}
// `NewS` will have all fields.
// When the field is not [`Option`], it's no effect
let _ = NewS { a: 1, b: "hello", c: 1.5 };