typechain 0.1.0

Create chains of trait objects
Documentation
  • Coverage
  • 100%
    1 out of 1 items documented1 out of 3 items with examples
  • Size
  • Source code size: 21.55 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.12 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Homepage
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • carlosskii

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];
}