2 releases

0.1.3 May 26, 2023
0.1.2 May 26, 2023
0.1.1 May 26, 2023
0.1.0 May 25, 2023

#1375 in Procedural macros

38 downloads per month

MIT license

5KB
54 lines

convert-chain

Crates.io docs.rs Crates.io Crates.io

A tiny crate for chain type converter. There is only one macro convert_chian in it

Assuming there are 3 sturct with following relationship.

struct A;
struct B;
struct C;

impl From<B> for A { ... }
impl From<C> for B { ... }

You can use the macro to directly convert an expression or create a closure for the convert.

  • Directly convert

    let c = C;
    let a = convert_chain!(c; B, A);
    
  • Create a closure

    let c = C;
    let f = convert_chain!(B, A);
    let a = f(c);
    

Both of the above methods are equivalent to

let c = C;
let a = A::from(B::from(c));

lib.rs:

convert-chain

Crates.io docs.rs Crates.io Crates.io

A tiny crate for chain type converter. There is only one macro convert_chian in it

Assuming there are 3 sturct with following relationship.

struct A;
struct B;
struct C;

impl From<B> for A { ... }
impl From<C> for B { ... }

You can use the macro to directly convert an expression or create a closure for the convert.

  • Directly convert

    let c = C;
    let a = convert_chain!(c; B, A);
    
  • Create a closure

    let c = C;
    let f = convert_chain!(B, A);
    let a = f(c);
    

Both of the above methods are equivalent to

let c = C;
let a = A::from(B::from(c));

Dependencies

~255–700KB
~17K SLoC