Rust library to support inheritance **Moved to gitlab** https://gitlab.com/NamingThingsIsHard/libraries/inheriteRS
This repository has been archived on 2024-11-27. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
Find a file
2024-04-01 15:03:08 +00:00
src feat: Implement method override in child structs 2024-04-01 17:02:25 +02:00
tests test: red: support overriding inherited non-trait methods 2024-04-01 17:02:25 +02:00
.gitignore feat: first function version 2024-03-09 15:00:01 +01:00
Cargo.lock chore: replace unwrap and expect with Result 2024-03-31 12:34:24 +02:00
Cargo.toml chore: replace unwrap and expect with Result 2024-03-31 12:34:24 +02:00
LICENSE feat: first function version 2024-03-09 15:00:01 +01:00
README.md chore: Add non-trait implementation inheritance to README.md 2024-04-01 17:02:25 +02:00

A proc-macro to enable inheritance in rust without runtime performance implications.

Examples

Inheriting attributes

use inheriters::specialisations;

specialisations!(
    struct Parent {
        attr1: u8,
    }
    #[inherit(Child)]
    struct Child {
        attr2: u8,
    }
);

will result in

struct Parent {
    attr1: u8,
}
struct Child {
    attr1: u8,
    attr2: u8,
}

Inheriting and overriding functions

By default, non-trait struct implementations (aka impl StructName not impl Trait for StructName) will be inherited by descendants. Those functions can also be overridden.

use inheriters::specialisations;

specialisations!(
    struct Parent {
        attr1: u8,
    }
    impl Parent {
        fn from_parent(self) -> u8 { 8 }
        fn overridden(self) -> u8 { self.attr1 }
    }
    
    #[inherit(Child)]
    struct Child {
        attr2: u8,
    }
    impl Child {
        fn overridden(self) -> u8 { self.attr2 }
    }
);

results in

struct Parent {
    attr1: u8,
}
impl Parent {
    fn from_parent(self) -> u8 { 8 }
    fn overridden(self) -> u8 { self.attr1 }
}


struct Child {
    attr1: u8, // new
    attr2: u8,
}
impl Child {
    fn from_parent(self) -> u8 { 8 } // new
    fn overridden(self) -> u8 { self.attr2 }
}