https://github.com/lu-zero/cow_struct
Derive per-field Cow Structures
https://github.com/lu-zero/cow_struct
Last synced: 3 months ago
JSON representation
Derive per-field Cow Structures
- Host: GitHub
- URL: https://github.com/lu-zero/cow_struct
- Owner: lu-zero
- License: mit
- Created: 2021-01-25T17:37:58.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-01-25T20:21:10.000Z (over 4 years ago)
- Last Synced: 2025-01-17T13:17:04.915Z (5 months ago)
- Language: Rust
- Size: 2.93 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# derive CowStruct
This crate consists in a procedural macro derive that provides a struct that is Cow and the impl to create one from the target struct.
## Usage
``` rust
use cow_struct::CowStruct;#[derive(Debug, Default, CowStruct)]
struct A {
a: LargeStruct,
b: OtherLargeStruct,
c: AnotherOne,
...
z: YetAnother,
}fn evaluation(cfg: &Config, state: &mut CowA) -> usize { ... }
fn foo() {
// original struct
let a = A::new();let mut val = 0;
// similar struct, with all the fields set as Cow::Borrowed from a;
let mut cow = a.to_cow();
let mut cow_out = cow.to_cow();
let mut v_max = usize::min();
for cfg in inputs() {
// it is going to change cow, a remains untouched
let val = evaluation(cfg, &mut cow);// let's pick the best candidate
for candidate in candidates_b(val) {
let mut cow2 = cow.to_cow();
let v = evaluation_b(candidate, &mut cow2);
if v > v_max {
v_max = v;
cow_out = cow2;
}
}
}
}
```