Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the default implementation of a trait method instead of the type's custom implementation?

Tags:

rust

traits

Some trait methods have default implementations which can be overwritten by an implementer. How can I use the default implementation for a struct that overwrites the default?

For example:

trait SomeTrait {
    fn get_num(&self) -> i32;
    fn add_to_num(&self) -> i32 {
        self.get_num() + 1
    }
}

struct SomeStruct;
impl SomeTrait for SomeStruct {
    fn get_num(&self) -> i32 {
        3
    }
    fn add_to_num(&self) -> i32 {
        self.get_num() + 2
    }
}

fn main() {
    let the_struct = SomeStruct;
    println!("{}", the_struct.add_to_num()); // how can I get this to print 4 instead of 5?
}
like image 981
awelkie Avatar asked Oct 14 '14 15:10

awelkie


1 Answers

One solution I've come up with is to define a dummy struct that contains the struct I want to change. I can then cherry-pick which methods I want to overwrite and which ones I want to keep as the default.

To extend the original example:

trait SomeTrait {
    fn get_num(&self) -> i32;
    fn add_to_num(&self) -> i32 {
        self.get_num() + 1
    }
}

struct SomeStruct;

impl SomeTrait for SomeStruct {
    fn get_num(&self) -> i32 {
        3
    }
    fn add_to_num(&self) -> i32 {
        self.get_num() + 2
    }
}

fn main() {
    struct SomeOtherStruct {
        base: SomeStruct,
    }

    impl SomeTrait for SomeOtherStruct {
        fn get_num(&self) -> i32 {
            self.base.get_num()
        }
        //This dummy struct keeps the default behavior of add_to_num()
    }

    let the_struct = SomeStruct;
    println!("{}", the_struct.add_to_num());

    //now we can call the default method using the original struct's data.
    println!("{}", SomeOtherStruct { base: the_struct }.add_to_num());
}
like image 126
awelkie Avatar answered Oct 23 '22 07:10

awelkie