Hereditary

Procedural macros for emulating OOP Inheritance in Rust, by extending the trait functionality in structs based on their composition (as they contain instances of other types that implement the desired functionality).

Composition Over Inheritance

Hereditary generates the boilerplate of trait implementations for the composited struct, by Forwarding the required methods that wrap the functionality already implemented in the contained instances referenced by its struct fields.

Currently, Hereditary support 2 kinds of delegation: - Partial Delegation: By using the decorator attribute #[forward_trait(submember)] on trait implementations. - Full Delegation: By applying #[derive(Forwarding)] on the composited struct, it derives the trait implementation on the struct field (designated by attribute #[forward_derive(Trait)]).

Note

Learn more in the documentation page here https://docs.rs/hereditary

Features

Usage

Install in your repository:

cargo add hereditary

Insert hereditary macros in your code:

```rust mod animal { // Attribute macro for generating trait info #[hereditary::traitinfo] pub trait Cannis { fn bark(&self)-> String; fn sniff(&self)-> bool; } #[hereditary::traitinfo] pub trait Bird { fn sing(&self) -> String; fn fly(&mut self, elevation:f64) -> f64; } }

// Heritance for an hybrid animal

[derive(hereditary::Forwarding)]

struct KimeraSphinx {
#[forward_derive(animal::Cannis)] // full implementation of Cannis dogpart:Bulldog, birdpart:Seagull }

// Combining existing trait implementation from Seagull instance

[hereditary::forward_trait(birdpart)]

impl animal::Bird for KimeraSphinx { fn sing(&self) -> String { self.dogpart.bark() } }

// -- Subcomponent implementations -- // struct Bulldog { position:f64 } impl animal::Cannis for Bulldog { //... Cannis methods ... } struct Seagull { elevation:f64 } impl animal::Bird for Seagull { //... Bird methods ... }

fn main() { // Instance kimera let mut kimera = KimeraSphinx::new(); // A dogs that flies. asserteq!(kimera.fly(50f64), 50f64); asserteq!(kimera.bark(), kimera.sing()); // It barks instead of singing assert_eq!(kimera.sniff(), true); } ```

Limitations

License

Apache 2.0

Authors: Francisco Leon