4 stable releases
1.2.0 | Mar 25, 2024 |
---|---|
1.1.1 | Mar 23, 2024 |
1.0.0 | Mar 23, 2024 |
#750 in Rust patterns
12KB
214 lines
Wrappr
wrappr
is a libary for some useful wrapping traits
the 4 trait groups are as follows:
- a
wrapper
is a cheap wrapper that constructs a new type, generic over the input type - a
composer
is a potentially costly operation that takes a generic input type, and composes it within itself - a
injectable
is the semantic opposite of a wrapper, it is automatically implemented and is just the object and generic swapped - a
embedable
is the semantic opposite of a composer, it is automatically implemented and is just the object and generic swapped
Examples
Wrapper
let x: u8 = 1;
let y: u8 = 2;
let z: Vec<u8> = vec![3, 4];
let yz: Vec<u8> = y.wrap(z);
assert_eq!(yz, vec![2, 3, 4]);
let xyz: Vec<u8> = x.wrap(yz);
assert_eq!(xyz, vec![1, 2, 3, 4]);
Composer
let mut x: Vec<u8> = vec![1, 2];
let y: Vec<u8> = vec![3, 4];
let z: u8 = 5;
x.compose(y);
assert_eq!(x, vec![1, 2, 3, 4]);
x.compose(z);
assert_eq!(x, vec![1, 2, 3, 4, 5]);
Injectable
let x: u8 = 1;
let y: u8 = 2;
let z: Vec<u8> = vec![3, 4];
let yz: Vec<u8> = z.inject(y);
assert_eq!(yz, vec![2, 3, 4]);
let xyz: Vec<u8> = yz.inject(x);
assert_eq!(xyz, vec![1, 2, 3, 4]);
Embedable
let mut x: Vec<u8> = vec![1, 2];
let y: Vec<u8> = vec![3, 4];
let z: u8 = 5;
y.embed(&mut x);
assert_eq!(x, vec![1, 2, 3, 4]);
z.embed(&mut x);
assert_eq!(x, vec![1, 2, 3, 4, 5]);