Решение на упр.09 задача 1 от Стилиян Иванов

Обратно към всички решения

Към профила на Стилиян Иванов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 5 успешни тест(а)
  • 0 неуспешни тест(а)

Код

use std::sync::{Arc, Mutex};
use std::collections::HashMap;
pub type Callback<S> = Box<dyn FnMut(&mut S) + Send + 'static>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Id {
event_hash: u64,
callback_id: usize,
}
fn event_hash(event_name: &str) -> u64 {
event_name.as_bytes().iter().fold(0u64, |acc, &b| acc.wrapping_add(b as u64) * 31)
}
struct EventGroup<S> {
callbacks: HashMap<usize, Callback<S>>,
}
struct Inner<S> {
next_callback_id: Mutex<usize>,
event_groups: Mutex<HashMap<String, Arc<Mutex<EventGroup<S>>>>>,
}
pub struct EventDispatcher<S> {
inner: Arc<Inner<S>>,
}
impl<S> EventDispatcher<S>
where
S: Send + 'static,
{
pub fn new() -> Self {
EventDispatcher {
inner: Arc::new(Inner {
next_callback_id: Mutex::new(0),
event_groups: Mutex::new(HashMap::new()),
}),
}
}
pub fn on(
&self,
event: &str,
callback: Callback<S>,
) -> Id {
let event_name = event.to_string();
let callback_id = {
let mut id_counter = self.inner.next_callback_id.lock().unwrap();
let id = *id_counter;
*id_counter += 1;
id
};
let id = Id {
event_hash: event_hash(&event_name),
callback_id,
};
let event_mutex_arc = {
let mut groups = self.inner.event_groups.lock().unwrap();
groups
.entry(event_name.clone())
.or_insert_with(|| Arc::new(Mutex::new(EventGroup { callbacks: HashMap::new() })))
.clone()
};
let mut group = event_mutex_arc.lock().unwrap();
group.callbacks.insert(callback_id, callback);
id
}
pub fn off(
&self,
id: Id,
) -> Option<Callback<S>> {
let event_mutex_arc_option = {
let groups = self.inner.event_groups.lock().unwrap();
groups.iter().find_map(|(event_name, event_mutex_arc)| {
if event_hash(event_name) == id.event_hash {
Some(event_mutex_arc.clone())
} else {
None
}
})
};
if let Some(event_mutex_arc) = event_mutex_arc_option {
let mut group = event_mutex_arc.lock().unwrap();
group.callbacks.remove(&id.callback_id)
} else {
None
}
}
pub fn emit(&self, event: &str, state: &mut S) {
let event_name = event.to_string();
let event_mutex_arc_option = {
let groups = self.inner.event_groups.lock().unwrap();
groups.get(&event_name).cloned()
};
if let Some(event_mutex_arc) = event_mutex_arc_option {
let mut group = event_mutex_arc.lock().unwrap();
let mut callbacks_to_run: Vec<&mut Callback<S>> = group.callbacks.values_mut().collect();
for callback in callbacks_to_run.iter_mut() {
callback(state);
}
}
}
}
impl<S> Clone for EventDispatcher<S> {
fn clone(&self) -> Self {
EventDispatcher { inner: Arc::clone(&self.inner) }
}
}

Лог от изпълнението

Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling unicode-ident v1.0.22
   Compiling quote v1.0.42
   Compiling futures-sink v0.3.31
   Compiling futures-core v0.3.31
   Compiling futures-channel v0.3.31
   Compiling slab v0.4.11
   Compiling syn v2.0.111
   Compiling memchr v2.7.6
   Compiling futures-io v0.3.31
   Compiling pin-utils v0.1.0
   Compiling futures-task v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling solution v0.1.0 (/tmp/d20251211-1757769-ppyjow/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.98s
     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

История (1 версия и 0 коментара)

Стилиян качи първо решение на 06.12.2025 13:50 (преди около 2 месеца)