1 unstable release

0.1.0 Jun 3, 2023

#2609 in Rust patterns

Download history 12/week @ 2024-02-24 1/week @ 2024-03-02 14/week @ 2024-03-30 1/week @ 2024-04-06 42/week @ 2024-04-13

57 downloads per month

MIT/Apache

9KB

typechain

This crate provides procedural macros for generating chains of related traits in Rust.

Example

types.rs

use typechain::{chainlink, chain};

chainlink!(Currency => {
  const usd_value: f64;
});

chain!(Fiat => {
  @Currency
  const usd_value: f64;
});

impl Fiat {
  pub fn new(usd_value: f64) -> Self {
    Self { usd_value }
  }
}

chain!(Crypto => {
  @Currency
  const usd_value: f64;
});

impl Crypto {
  pub fn new(usd_value: f64) -> Self {
    Self { usd_value }
  }
}

main.rs

use typechain::use_chains;
mod types;

use types::{Fiat, Crypto};
use_chains![types::Currency];

fn main() {
  let usd = Fiat::new(1.0);
  let btc = Crypto::new(10000.0);

  let currencies: Vec<&Currency> = vec![&usd, &btc];
}

lib.rs:

typechain

This crate re-exports the typechain macros. In the future, it may also contain other utilities for working with typechain-generated code.

Usage

use typechain::{chainlink, chain};

chainlink!(Foo => {
    const foo: u32;
});

chain!(Bar => {
    @Foo
    const foo: u32;
});

chain!(Baz => {
    @Foo
    const foo: u32;
});

let bar = Bar { foo: 42 };
let baz = Baz { foo: 97 };

let foos: Vec<&Foo> = vec![&bar, &baz];

Dependencies

~0.4–0.8MB
~19K SLoC