Thanks @osrust and @kornel
So, can I tell it is all about calling a function Listener
from another function Emitter
, which can be made in the trait through calling Self::<Fn>
something like below playgrund which did the job.
#[derive(Debug)]
pub struct Counter {
count: i32,
}
trait ManipulateExt {
fn square(&self) -> Self;
fn increase(&self, amount: i32) -> Self;
fn decrease(&self, amount: i32) -> Self;
}
impl ManipulateExt for Counter {
fn square(&self) -> Self {
Self::on_squared();
Counter { count: &self.count * &self.count }
}
fn increase(&self, amount: i32) -> Self {
Self::on_increased(amount);
Counter { count: &self.count + amount }
}
fn decrease(&self, amount: i32) -> Self {
Self::on_decreased(self, amount);
Counter { count: &self.count - amount }
}
}
trait EventListener {
fn on_squared() -> () {println!("Counter squared")}
fn on_increased(amount: i32) -> () {println!("Counter increased by {}", amount)}
fn on_decreased(this: &Counter, amount: i32) -> () {println!("Counter reduced from {} to {}", &this.count, &this.count - amount)}
}
impl EventListener for Counter {}
fn main() {
let mut x = Counter { count: 6 };
x = x.increase(3);
println!("{:?}", x);
x.decrease(2);
println!("{:?}", x);
}
Or could be better to re-arrange it as below, so that the Listener
be called after executing the Emitter
playground
#[derive(Debug)]
pub struct Counter {
count: i32,
}
trait CounterExt {
fn square(&mut self);
fn increase(&mut self, amount: i32);
fn decrease(&mut self, amount: i32);
}
impl CounterExt for Counter {
fn square(&mut self) {
self.count = self.count * self.count;
Self::on_squared();
}
fn increase(&mut self, amount: i32) {
self.count = self.count + amount;
Self::on_increased(amount);
}
fn decrease(&mut self, amount: i32) {
let initial_value = self.count;
self.count = self.count - amount;
Self::on_decreased(&Counter {count: initial_value}, amount);
}
}
trait CounterListener {
fn on_squared() {
println!("Counter squared")
}
fn on_increased(amount: i32) {
println!("Counter increased by {}", amount)
}
fn on_decreased(this: &Counter, amount: i32) {
println!("Counter reduced from {} to {}", &this.count, &this.count - amount)
}
}
impl CounterListener for Counter {}
fn main() {
let mut x = Counter { count: 6 };
println!("Counter started at: {:#?}", x);
x.increase(3);
println!("{:?}", x);
x.decrease(2);
println!("{:?}", x);
}
But I've slight issue in the on_decrease
as I failed to use self
and forced to define another variable that is this: &Counter
:
fn on_decreased(this: &Counter, amount: i32) {
println!("Counter reduced from {} to {}", &this.count, &this.count - amount)
}
// below failed
// fn on_decreased(self, amount: i32) {
// println!("Counter reduced from {} to {}", &self.count, &self.count - amount)
// }
If I defined it as:
fn on_decreased(self, amount: i32) {}
I get error:
error[E0277]: the size for values of type `Self` cannot be known at compilation time
--> src/main.rs:35:22
|
35 | fn on_decreased(self, amount: i32) {
| ^^^^ doesn't have a size known at compile-time
|
error[E0609]: no field `count` on type `Self`
--> src/main.rs:36:57
|
36 | println!("Counter reduced from {} to {}", &self.count, &self.count - amount)
| ^^^^^
And if I tried to define it as:
fn on_decreased(&self, amount: i32) {}
I get the error:
error[E0609]: no field `count` on type `Self`
--> src/main.rs:36:57
|
36 | println!("Counter reduced from {} to {}", &self.count, &self.count - amount)
| ^^^^^