Решение на упр.09 задача 1 от Георги Стоянов
Резултати
- 5 точки от тестове
- 0 бонус точки
- 5 точки общо
- 5 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone)]
pub struct Id {
pub event: String,
pub id: u64,
}
type Callback<S> = Box<dyn FnMut(&mut S) + Send + 'static>;
struct Inner<S> {
next_id: u64,
events: HashMap<String, Arc<Mutex<HashMap<u64, Callback<S>>>>>,
}
pub struct EventDispatcher<S> {
inner: Arc<Mutex<Inner<S>>>,
}
impl<S> EventDispatcher<S>
where
S: Send + 'static,
{
pub fn new() -> Self {
EventDispatcher {
inner: Arc::new(Mutex::new(Inner {
next_id: 0,
events: HashMap::new(),
})),
}
}
pub fn on(&self, event: &str, callback: Callback<S>) -> Id {
let (new_id, event_arc) = {
let mut inner = self.inner.lock().unwrap();
let id = inner.next_id;
inner.next_id += 1;
let entry = inner
.events
.entry(event.to_string())
.or_insert_with(|| Arc::new(Mutex::new(HashMap::new())))
.clone();
(id, entry)
};
{
let mut callbacks = event_arc.lock().unwrap();
callbacks.insert(new_id, callback);
}
Id {
event: event.to_string(),
id: new_id,
}
}
pub fn off(&self, id: Id) -> Option<Callback<S>> {
let event_arc_opt = {
let inner = self.inner.lock().unwrap();
inner.events.get(&id.event).cloned()
};
if let Some(event_arc) = event_arc_opt {
let mut callbacks = event_arc.lock().unwrap();
callbacks.remove(&id.id)
} else {
None
}
}
pub fn emit(&self, event: &str, state: &mut S) {
let event_arc_opt = {
let inner = self.inner.lock().unwrap();
inner.events.get(event).cloned()
};
if let Some(event_arc) = event_arc_opt {
let mut callbacks = event_arc.lock().unwrap();
for cb in callbacks.values_mut() {
(cb)(state);
}
}
}
}
impl<S> Clone for EventDispatcher<S> {
fn clone(&self) -> Self {
EventDispatcher {
inner: Arc::clone(&self.inner),
}
}
}
/*
Използваме типове, които се държат като handle-и (като EventDispatcher и Id), когато искаме много части от програмата или много нишки да споделят един и същ ресурс, без да знаят как е имплементиран вътрешно. Не ги ползваме, когато данните са малки, не се споделят и могат спокойно да се предават направо като стойност или обикновена референция – така кодът остава по-прост и четим.
*/
Лог от изпълнението
Updating crates.io index
Locking 17 packages to latest compatible versions
Compiling proc-macro2 v1.0.103
Compiling quote v1.0.42
Compiling unicode-ident v1.0.22
Compiling futures-core v0.3.31
Compiling futures-sink v0.3.31
Compiling futures-channel v0.3.31
Compiling futures-task v0.3.31
Compiling slab v0.4.11
Compiling memchr v2.7.6
Compiling syn v2.0.111
Compiling futures-io v0.3.31
Compiling pin-project-lite v0.2.16
Compiling pin-utils v0.1.0
Compiling solution v0.1.0 (/tmp/d20251211-1757769-u92lnq/solution)
Compiling futures-macro v0.3.31
Compiling futures-util v0.3.31
Compiling futures-executor v0.3.31
Compiling futures v0.3.31
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.89s
Running tests/solution_test.rs (target/debug/deps/solution_test-ee0783488e12dce9)
running 5 tests
test solution_test::test_basic_run1 ... ok
test solution_test::test_basic_run2 ... ok
test solution_test::test_multithreaded_run2 ... ok
test solution_test::test_multithreaded_run1 ... ok
test solution_test::test_reentrant_run ... ok
test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
