scrabsha / butcher Goto Github PK
View Code? Open in Web Editor NEWSeparate different pieces of meat from Cow easily
License: Apache License 2.0
Separate different pieces of meat from Cow easily
License: Apache License 2.0
Let's consider the following code:
use butcher::Butcher;
#[derive(Butcher, Clone)]
struct Foo {
a: Bar,
}
#[derive(Butcher, Clone)]
struct Bar {
b: u32,
}
It is currently impossible to bind values of Bar::b
when pattern-matching on a Cow<Foo>
.
Adding a rebutcher
butchering method to a field will, instead of creating a Cow
, create the butchered version of its associated type.
The following code fails to compile:
use butcher::Butcher;
#[derive(Butcher, Clone)]
struct Foo {
bar: Box<Self>,
}
However, replacing Self
with Foo
make it working:
use butcher::butcher;
#[derive(Butcher, Clone)]
struct Foo {
bar: Box<Foo>,
}
This is because Self
means "the current struct or enum", so we have to dynamically replace it.
To fix this, it is necessary to write a function which replaces every occurrence to Self
in a Type
with the struct/enum name with its generics. The function may have a shape similar to find_generics_in_type
, expect that it takes a mutable reference instead of taking ownership, and returns nothing.
It is also necessary to write a function which generates "the struct/enum name with its generics". This part is way trickier, since we have to generate a Path
from a DeriveInput
.
Warning: this would also need a lot of disambiguation in the documentation because flatten is used for two different contexts.
Working with Cow<String>
is less optimal than working with Cow<str>
. The same goes for every type which implements ToOwned
.
It can be helpful to have a trait which ease such conversion.
It is currently impossible to use the Butcher
trait for enums and structs. This makes pattern matching harder.
Find the most common enums in the rust standard library, and implement Butcher
on them.
Butcher
also needs to be implemented on tuples. This can be easily done with a declarative macro.
Edits:
The following source code does not compile:
use butcher::Butcher;
use std::borrow::Cow;
#[derive(Butcher, Clone)]
struct Foo(u32, u32);
fn match_foo(i: Cow<Foo>) {
match Foo::butcher(i) {
ButcheredFoo(0, 0) => println!("Two zeros"),
_ => println!("{}, {}", i.0, i.1),
}
The error comes from the fact that Foo::butcher(i)
takes ownership of i
. As such, we can't use it anymore.
The best solution I found is:
// WARNING: this is unsound, and is impossible to reproduce
use butcher::{Butcher, Unbutcher};
use std::borrow::Cow;
#[derive(Butcher, Clone)]
struct Foo(u32, u32);
fn match_foo(i: Cow<Foo>) {
match Foo::butcher(i) {
ButcheredFoo(0, 0) => println!("Two zeros"),
// other_butchered has type ButcheredFoo
other_butchered => {
// i has type Foo
let i = other_butchered.unbutcher();
println!("{}, {}", i.0, i.1),
}
}
Where the Unbutcher
trait has an unbutcher
method, which is implemented for every butchered type.
This solution is still open for debate.
The best solution I found is to recreate the initial structure, as owned data. This implies that any borrowed data will be cloned.
Edit:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.