I have a third party class that I normally in C would extend to get my own specialized type. How to do it in Rust? The reason I'm trying to do it that I of course could use composition instead but by doing so I cannot easily call the methods from the 3rd party type. How is it done in Rust? [Edit]
use some_3rd_crate;
struct MyStruct{third_type: some_3rd_crate::Type,}
How to get all the public methods of some_3rd_crate::Type into scope of my type so I can use them like:
let mt = MyStruct{third_type: some_3rd_crate::Type()};
mt.call_3rd_party_function();
CodePudding user response:
You could implement AsRef
or AsMut
for your type:
impl AsRef<some_3rd_crate::Type> for MyStruct
{
fn as_ref(&self) -> &some_3rd_crate::Type {
&self.third_type
}
}
Then could be used as:
let mt = MyStruct{third_type: some_3rd_crate::Type()};
mt.as_ref().call_3rd_party_function();