Решение на упр.09 задача 1 от Пламен Колев

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

Към профила на Пламен Колев

Резултати

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

Код

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
pub struct Id {
id: usize,
}
struct Inner<S> {
callback: HashMap<String, HashMap<usize, Box<dyn FnMut(&mut S) + Send>>>,
next_id: usize,
}
pub struct EventDispatcher<S> {
inner: Arc<Mutex<Inner<S>>>,
}
impl<S> EventDispatcher<S> {
pub fn new() -> Self {
EventDispatcher {
inner: Arc::new(Mutex::new(Inner {
callback: HashMap::new(),
next_id: 0,
})),
}
}
pub fn on(
&self,
event: &str,
callback: Box<dyn FnMut(&mut S) + Send>,
) -> Id {
let mut inner = self.inner.lock().unwrap();
let id = Id{id:inner.next_id};
inner.next_id += 1;
inner.callback.entry(event.to_string()).or_insert_with(HashMap::new).insert(id.id, callback);
id
}
pub fn off(
&self,
id: Id,
) -> Option<Box<dyn FnMut(&mut S) + Send>> {
let mut inner = self.inner.lock().unwrap();
for (_e, map) in inner.callback.iter_mut() {
if let Some(callback) = map.remove(&id.id) {
return Some(callback);
}
}
None
}
pub fn emit(&self, event: &str, state: &mut S) {
let mut callbacks = Vec::new();
{
let mut inner = self.inner.lock().unwrap();
if let Some(map) = inner.callback.get_mut(event) {
for callback in map.values_mut(){
callbacks.push(callback as *mut Box<dyn FnMut(&mut S) + Send>);
}
}
}
for callback in callbacks {
unsafe{
(*callback)(state);
}
}
}
}
impl<S> Clone for EventDispatcher<S> {
fn clone(&self) -> Self {
EventDispatcher{inner:Arc::clone(&self.inner)}
}
}
fn main() {
}

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

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 futures-io v0.3.31
   Compiling futures-task v0.3.31
   Compiling syn v2.0.111
   Compiling pin-utils v0.1.0
   Compiling memchr v2.7.6
   Compiling slab v0.4.11
   Compiling pin-project-lite v0.2.16
   Compiling solution v0.1.0 (/tmp/d20251211-1757769-s7r6fu/solution)
warning: function `main` is never used
  --> src/lib.rs:76:4
   |
76 | fn main() {
   |    ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `solution` (lib) generated 1 warning
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
warning: function `main` is never used
  --> tests/../src/lib.rs:76:4
   |
76 | fn main() {
   |    ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `solution` (test "solution_test") generated 1 warning
    Finished `test` profile [unoptimized + debuginfo] target(s) in 9.15s
     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_run1 ... ok
test solution_test::test_multithreaded_run2 ... 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 коментара)

Пламен качи първо решение на 09.12.2025 22:06 (преди около 2 месеца)