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

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

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

Резултати

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

Код

use std::{
collections::HashMap,
sync::{Arc, Mutex, atomic::AtomicU64},
};
/////////////////////////////////////////
#[derive(Hash, Clone, Copy)]
pub struct Id {
id: u64,
}
impl PartialEq for Id {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for Id {}
impl Id {
pub fn new() -> Self {
static MY_ID_COUNTER: AtomicU64 = AtomicU64::new(0);
let curr_id = MY_ID_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
return Id { id: curr_id };
}
}
/////////////////////////////////////////
struct EventCallbackDetails<S> {
id: Id,
callback: Box<dyn FnMut(&mut S) + Send /* + ... */>,
}
impl<S> EventCallbackDetails<S> {
pub fn new(id: Id, callback: Box<dyn FnMut(&mut S) + Send /* + ... */>) -> Self {
EventCallbackDetails { id, callback }
}
pub fn call(&mut self, state: &mut S) {
(self.callback)(state)
}
}
/////////////////////////////////////////
pub struct EventDispatcher<S> {
inner: Arc<Mutex<HashMap<String, Arc<Mutex<Vec<EventCallbackDetails<S>>>>>>>,
}
impl<S> EventDispatcher<S> {
pub fn new() -> Self {
EventDispatcher {
inner: Arc::new(Mutex::new(HashMap::new())),
}
}
pub fn on(&self, event: &str, callback: Box<dyn FnMut(&mut S) + Send /* + ... */>) -> Id {
let new_id = Id::new();
let subs_arc = {
let mut inner = self.inner.lock().unwrap();
match inner.get(event) {
Some(arc) => arc.clone(),
None => {
inner.insert(
event.to_string(),
Arc::new(Mutex::new(vec![EventCallbackDetails::new(
new_id, callback,
)])),
);
return new_id;
}
}
};
let mut a = subs_arc.lock().unwrap();
a.push(EventCallbackDetails {
id: new_id,
callback: callback,
});
new_id
}
pub fn off(&self, id: Id) -> Option<Box<dyn FnMut(&mut S) + Send /* + ... */>> {
let target_arc = {
let inner = self.inner.lock().unwrap();
inner
.values()
.find(|event_arc| {
let subs = event_arc.lock().unwrap();
subs.iter().any(|el| el.id == id)
})
.cloned()?
};
let mut subs = target_arc.lock().unwrap();
let index = subs.iter().position(|el| el.id == id)?;
let removed = subs.swap_remove(index);
Some(removed.callback)
}
pub fn emit(&self, event: &str, state: &mut S) {
let subs_arc = {
let inner = self.inner.lock().unwrap();
match inner.get(event) {
Some(arc) => arc.clone(),
None => return,
}
};
let mut subs = subs_arc.lock().unwrap();
for sub in subs.iter_mut() {
sub.call(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-task v0.3.31
   Compiling pin-utils v0.1.0
   Compiling syn v2.0.111
   Compiling futures-io v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling slab v0.4.11
   Compiling memchr v2.7.6
   Compiling solution v0.1.0 (/tmp/d20251211-1757769-11s0kq8/solution)
warning: function `main` is never used
   --> src/lib.rs:129:4
    |
129 | 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:129:4
    |
129 | 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 8.93s
     Running tests/solution_test.rs (target/debug/deps/solution_test-ee0783488e12dce9)

running 5 tests
test solution_test::test_basic_run2 ... ok
test solution_test::test_basic_run1 ... ok
test solution_test::test_multithreaded_run1 ... ok
test solution_test::test_multithreaded_run2 ... ok
test solution_test::test_reentrant_run ... FAILED

failures:

---- solution_test::test_reentrant_run stdout ----
thread 'solution_test::test_reentrant_run' panicked at tests/solution_test.rs:128:60:
test timeout
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    solution_test::test_reentrant_run

test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.00s

error: test failed, to rerun pass `--test solution_test`

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

Станислав качи първо решение на 10.12.2025 23:23 (преди около 2 месеца)