[−][src]Crate butcher
Butcher
An easy way to interact with Cow
ed structs and enums.
This crate proposes some simple solutions to the most common patterns I met
while working with Cow
s in Rust. It currently fixes two patterns:
destructuring and iteration related to objects wrapped in Cow
.
Destructuring
The Butcher
trait can be used when it is necessary to destruture something
wrapped in a Cow
. Below is a simple example:
use std::borrow::Cow; use butcher::Butcher; #[derive(Butcher, Clone)] struct MyNumberList { val: u32, next: Option<Box<MyNumberList>>, } fn destructure_list_elem(i: Cow<MyNumberList>) -> (Cow<u32>, Cow<Option<Box<MyNumberList>>>) { let ButcheredMyNumberList { val, next } = Butcher::butcher(i); (val, next) }
The Butcher
procedural macro can be derived for structs
and for enums.
Iteration
Here is a demonstration of how to iterate over an object wrapped in a Cow
:
use std::borrow::Cow; use butcher::iterator::{CowIter, IntoCowIterator}; fn print_numbers(elems: Cow<[u32]>) { let mut iter = elems.into_cow_iter(); for element in iter { // The type of element is Cow<u32> println!("{:?}", element); } }
See the documentation of CowIter
for more information.
Modules
deriving_butcher_enum | Quick introduction to butchering on enums |
deriving_butcher_struct | Quick introduction to butchering on structs |
iterator | An iterator over data wrapped in |
Macros
butcher_struct | Allow simple destructuring. |
Traits
Butcher | |
ButcherField |
Derive Macros
Butcher | Derives the |