Expose composed methods?


#1

I read online some talk about being about to expose methods of a composed object. In the following simplified example, is there a way to call foo.SetFocus() without having to type foo.control.window.SetFocus() so that we can save some typing and avoid exposing implementation details?

struct Window {
    hwnd : u32,
}
impl Window {
    pub fn SetFocus ( self ) {
        unsafe blah blah blah
    }
}
struct Control {
    window : Window,
}
struct ListBox {
    control : Control,
}

let foo = ListBox::new();
foo.control.window.SetFocus();

I know this is an XY problem, but I’m asking this question specifically because I’m still learning rust and I want/need to understand it’s finer points.


#2

If SetFocus were to take &self, then you can impl Deref for ListBox (to expose the Control) and for Control to expose the Window. Then you can call listBox.SetFocus() and it will drill down through the deref chain to your Window.


#3

Thank you, this has led me to several features that I missed from my first read through the book.