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

前端 未结 1 749
时光取名叫无心
时光取名叫无心 2021-01-17 20:19

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

相关标签:
1条回答
  • 2021-01-17 20:54

    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());
    }
    
    0 讨论(0)
提交回复
热议问题