Extending somebody's type

There are a few approaches:

  • if you want to implement a third-party trait B for Dialog, you can create a newtype

    struct MyDialog(Dialog);
    

    and then impl B for MyDialog

  • if you want to package additional data with Dialog, you'd create a new struct like this:

    struct DialogPlus {
        dialog: Dialog,
        extras: MyData,
    }
    
  • if you just want to write your own functions that take a Dialog as receiver, so that you can call them with method syntax, you can write an extension trait:

    trait DialogExt {
        fn my_extra_method(&self) -> Foo;
    }
    
    impl DialogExt for Dialog {
        fn my_extra_method(&self) -> Foo {
            /* implementation goes here */
        }
    }
    
7 Likes