9 releases (2 stable)

1.0.1 May 2, 2024
0.5.0 Jul 3, 2023
0.4.3 Mar 9, 2023
0.4.2 Feb 26, 2023
0.1.1 Feb 21, 2023

#568 in Rust patterns

Download history 1009/week @ 2024-06-30 1228/week @ 2024-07-07 1112/week @ 2024-07-14 2146/week @ 2024-07-21 1356/week @ 2024-07-28 1211/week @ 2024-08-04 1391/week @ 2024-08-11 792/week @ 2024-08-18 552/week @ 2024-08-25 441/week @ 2024-09-01 511/week @ 2024-09-08 422/week @ 2024-09-15 560/week @ 2024-09-22 606/week @ 2024-09-29 186/week @ 2024-10-06 442/week @ 2024-10-13

1,803 downloads per month

MIT license

22KB
333 lines

Crates.io

transitive

Transitive converions through derive macros for Rust.

Rationale:

Assume you have types A, B and C with the following, already implemented, conversions:

  • A -> B
  • B -> C

Sometimes it might be desirable to have an A -> C implementation which could easily be represented as A -> B -> C.

That is precisely what this crate does. Through the Transitive derive macro, it will implement From or TryFrom respectively for converting from/to the derived type and a target type, given a path of transitions to go through.

use transitive::Transitive;

#[derive(Transitive)]
#[transitive(into(B, C, D))] // impl From<A> for D by doing A -> B -> C -> D
struct A;

#[derive(Transitive)]
#[transitive(into(C, D))] // impl From<B> for D by doing B -> C -> D
struct B;
struct C;
struct D;

impl From<A> for B {
    fn from(val: A) -> Self {
        Self
    }
};

impl From<B> for C {
    fn from(val: B) -> Self {
        Self
    }
};

impl From<C> for D {
    fn from(val: C) -> Self {
        Self
    }
};

#[test]
fn into() {
    D::from(A);
    D::from(B);
}

More examples and explanations can be found in the documentation.

License

Licensed under MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT).

Contributing

Contributions to this repository, unless explicitly stated otherwise, will be considered licensed under MIT. Bugs/issues encountered can be opened here.

Dependencies

~270–720KB
~17K SLoC