这个想法很简单:有些应用程序有一些 Windows。Windows 对其应用程序有某种类型的引用。他们需要使用彼此的方法。我已经编写了一些粗略的代码,但是当我解决一个问题时,我得到了一个新问题。
struct Win {
x: i32,
y: i32,
last_event: u32, // just for testing
app: App,
}
impl Win {
pub fn new(app: &mut App, x: i32, y: i32) -> Win {
let mut win = Win{app: *app, x: x, y: y, last_event: 0};
app.add_window(&mut win);
win
}
fn add_window_to_app(&mut self, app: &mut App) {
app.add_window(self);
}
pub fn on_event(&mut self, event: u32, param1: u32, param2: u32) {
self.last_event = event;
}
}
struct App {
pid: u32,
windows: Vec<Win>,
}
impl App {
pub fn new(pid: u32) -> App {
let app = App{pid: pid, windows: Vec::<Win>::new()};
app
}
pub fn add_window(&mut self, win: &mut Win) {
self.windows.push(*win);
}
pub fn on_event(&mut self, win: &mut Win, event: u32, param1: u32, param2: u32) {
win.on_event(event, param1, param2);
}
}
fn main() {
let mut app = App::new(1);
}