roop 0.1.0

Provides attributes to simulate inheritance in Rust
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented0 out of 2 items with examples
  • Size
  • Source code size: 9.22 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 299.53 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 5s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • MikulasBar/roop
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • MikulasBar

Roop

This library provides 2 attributes: class and extends. Combination of these attributes can 'simulate' inheritance in Rust. Note that this library and it's functions are highly experimental.

As class can be marked only struct with named fields. The child class don't have to be marked as class. You can't implement Deref and DerefMut traits on child class, because extends actively uses them.

Extending class outside of local module is currently not working.

use roop::*;

#[class] // Marks struct as class
struct Parent {
    a: i32,
    b: i32,
}

impl Parent {
    pub fn print_all(&self) {
        println!("a: {}, b: {}", self.a, self.b);
    }

    pub fn sum_a_b(&self) -> i32 {
        self.a + self.b
    }
}

#[extends(Parent)] // Extends Parent class, inherit all fields from Parent
struct Child {
    c: i32,
}

impl Child {
    pub fn print_all(&self) { // This will override the print_all from Parent class
        println!("a: {}, b: {}, c: {}", self.a, self.b, self.c);
    }
}

fn main() {
    let parent = Parent {
        a: 1,
        b: 2,
    };

    let child = Child {
        a: 1,
        b: 2,
        c: 3,
    };

    parent.print_all(); // Output: "a: 1, b: 2" 
    child.print_all(); // Output: "a: 1, b: 2, c: 3"

    let p_sum = parent.sum_a_b();
    let c_sum = child.sum_a_b();

    assert_eq!(p_sum, c_sum); // Will pass!
}