Created
July 20, 2020 15:58
-
-
Save andyjsbell/e7c5900e2f578263364bd7037e9a5c2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Operations { | |
count: u32, | |
} | |
impl Operations { | |
pub fn new() -> Self { | |
Operations{ | |
count: 0, | |
} | |
} | |
pub fn update(&mut self) { | |
self.count = self.count + 1 | |
} | |
pub fn count(&self) -> u32 { | |
self.count | |
} | |
} | |
struct Emitter { | |
listener: Listener, | |
} | |
impl Emitter { | |
pub fn new(listener: Listener) -> Self { | |
Emitter { | |
listener | |
} | |
} | |
pub fn fire(&mut self) { | |
self.listener.event() | |
} | |
} | |
struct Controller { | |
emitter: Emitter, | |
} | |
impl Controller { | |
pub fn new() -> Self { | |
let listener = Listener::new(); | |
Controller { | |
emitter: Emitter::new(listener) | |
} | |
} | |
pub fn test(&mut self) { | |
self.emitter.fire() | |
} | |
} | |
struct Listener { | |
ops: Operations, | |
} | |
impl Listener { | |
pub fn new() -> Self { | |
Listener { | |
ops: Operations::new(), | |
} | |
} | |
pub fn event(&mut self) { | |
println!("event"); | |
self.ops.update(); | |
self.ops.update(); | |
self.ops.update(); | |
println!("count={}", self.ops.count()) | |
} | |
} | |
pub fn main() { | |
println!("main"); | |
let mut c = Controller::new(); | |
c.test() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment