Git Product home page Git Product logo

butcher's People

Contributors

scrabsha avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

icodein

butcher's Issues

Add a `rebutcher` butchering method

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.

Handle cases where a field has a type containing `Self`

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.

Add a way to "flatten" cows

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.

Implement `Butcher` for std enums, structs and tuples

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:

  • 26/07/2020: formatting, add reference to tuples

Allow catch-all clause in matches

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:

  • 30/07/2020: provide better solution, more likely to actually work

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.