5个版本
0.1.4 | 2021年1月4日 |
---|---|
0.1.3 | 2020年11月25日 |
0.1.2 | 2020年11月22日 |
0.1.1 | 2020年11月10日 |
0.1.0 | 2020年11月8日 |
#1106 在 数据结构
在 2 crate 中使用
36KB
549 行
pair_macro

创建由相同类型值组成的类型,例如Pair、Triplet等。
此crate在no-std
环境中运行。
示例
使用提供的类型 Pair<T>
。
use pair_macro::Pair;
let p = Pair::new(1.0, 2.0); // Pair<f64>
let q = p.map(|v| v * 2.0);
assert_eq!(Pair::new(2.0, 4.0), q);
// `Pair<T>` has `x` and `y` fields
assert_eq!(2.0, q.x);
assert_eq!(4.0, q.y);
创建一个新的pair类型。
use pair_macro::create_pair_prelude::*;
create_pair!(MyOwnPair; a, b, c, d);
let p = MyOwnPair::new(1, 2, 3, 4); // MyOwnPair<i32>
let q = MyOwnPair::new(5, 6, 7, 8);
let r = p + q;
// `MyOwnPair<T>` has `a`, `b`, `c` and `d` fields
assert_eq!(6, r.a);
assert_eq!(8, r.b);
assert_eq!(10, r.c);
assert_eq!(12, r.d);
使用提供的方法
use core::str::FromStr;
use pair_macro::Pair;
let p = Pair::new(["hello", "42"], ["world", "58"]); // Pair<[&str; 2]>
let value = p
.as_ref() // Pair<&[&str; 2]>
.map(|strs| strs[1]) // Pair<&str>
.map(i32::from_str) // Pair<Result<i32, Error>>
.into_result() // Result<Pair<i32>, Error>
.unwrap() // Pair<i32>
.into_iter() // Iterates each value in the pair (`42` and `58` in this situation)
.sum::<i32>(); // 42 + 58
assert_eq!(100, value);
当然,您使用create_pair!
宏定义的原始pair类型具有相同的方法。
功能
Pair类型通过启用serde
功能支持serde
:在您的Cargo.toml
中,
pair_macro = { version = "0.1.4", features = ["serde"] }
注意
由于Pair类型的文档是通过宏展开生成的,因此可能难以阅读。
许可证:MIT
依赖关系
~0–275KB