im trying to make some thing like Observer Pattern, i encounterd a problem, i want to use
Box<dyn Observer>
to store different type in HashSet :
pub struct Stock {
observer_list: HashSet<Box<dyn Observer>>,
price: i32,
}
but when i try to insert something into observer_list, something went wrong:
error[E0599]: the method `insert` exists for struct `HashSet<Box<dyn Observer>>`, but its trait bounds were not satisfied
--> src\lib.rs:68:32
|
68 | self.observer_list.insert()
| ^^^^^^
|
::: C:\Users\ASUS\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib/rustlib/src/rust\library\alloc\src\boxed.rs:195:1
|
195 | / pub struct Box<
196 | | T: ?Sized,
197 | | #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
198 | | >(Unique<T>, A);
| |_- doesn't satisfy `Box<(dyn Observer + 'static)>: Eq`, `Box<(dyn Observer + 'static)>: Hash` or `Box<(dyn Observer + 'static)>: PartialEq`
|
= note: the following trait bounds were not satisfied:
`Box<(dyn Observer + 'static)>: Eq`
`Box<(dyn Observer + 'static)>: PartialEq`
which is required by `Box<(dyn Observer + 'static)>: Eq`
`Box<(dyn Observer + 'static)>: Hash`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0599`.
error: could not compile `rs_test_new` (lib) due to 2 previous errors
and i have tried hard to impl those trait but in vain. Here's the code:
pub mod observer_pattern {
use std::collections::HashSet;
trait Observer{
fn update(&self);
}
trait ObserverManager
{
fn attach(&mut self, things: Box<impl Observer>) -> bool;
fn detach(&mut self, things: Box<impl Observer>) -> bool;
fn notify(&self);
}
pub struct Monitor<'a>(&'a Stock);
impl<'a> Observer for Monitor<'a>
{
fn update(&self) {
self.print(self.0.get_price())
}
}
impl<'a> Monitor<'a>
{
pub fn new(thing: &mut Stock) -> Monitor {
Monitor(thing)
}
fn print(&self, i: i32) {
println!("Monitor: {}", i);
}
}
pub struct BillBoard<'a>(&'a Stock);
impl<'a> Observer for BillBoard<'a>
{
fn update(&self) {
self.display(self.0.get_price())
}
}
impl<'a> BillBoard<'a> {
pub fn new(thing: &Stock) -> BillBoard {
BillBoard(thing)
}
fn display(&self, i: i32) {
println!("BillBoard: {i}");
}
}
pub struct Stock {
observer_list: HashSet<Box<dyn Observer>>,
price: i32,
}
impl ObserverManager for Stock
{
fn attach(&mut self, things: Box<impl Observer>) -> bool{
self.observer_list.insert()
}
fn detach(&mut self, things: Box<impl Observer>) -> bool{
todo!()
}
fn notify(&self) {
todo!()
}
}
impl Stock {
pub fn new(price: i32) -> Stock {
Stock { price, observer_list: HashSet::new() }
}
pub fn get_price(&self) -> i32 {
self.price
}
pub fn set_price(&mut self, price: i32) {
self.price = price;
// self.notify();
}
}
}
and here's the main function:
use rs_test_new::observer_pattern::*;
fn main() {
let mut stock = Stock::new(55);
let mo = Monitor::new(&stock);
let bb = BillBoard::new(&stock);
stock.set_price(20);
}
i wish when use method set_price everyone in the observer_list will print a line to prove that it did receive the changes.
please help me , i will really appriciate it !!