completeq_rs/oneshot/
sender.rs

1use std::sync::{Arc, Mutex};
2
3use crate::{
4    result::{EmitInnerResult, EmitResult},
5    user_event::UserEvent,
6};
7
8use super::inner::CompleteQImpl;
9
10/// Event sender endpoint, creating by [`super::EventReceiver`] instance.
11pub struct EventSender<E: UserEvent> {
12    event_id: E::ID,
13    inner: Arc<Mutex<CompleteQImpl<E>>>,
14}
15
16impl<E: UserEvent> EventSender<E> {
17    pub(crate) fn new(event_id: E::ID, inner: Arc<Mutex<CompleteQImpl<E>>>) -> Self {
18        Self { event_id, inner }
19    }
20
21    pub fn send(&self, event_arg: E::Argument) -> EmitResult {
22        let result = self
23            .inner
24            .lock()
25            .unwrap()
26            .complete_one(self.event_id.clone(), event_arg);
27
28        match result {
29            EmitInnerResult::Completed => EmitResult::Completed,
30            EmitInnerResult::Closed => EmitResult::Closed,
31            EmitInnerResult::Pending(_) => EmitResult::Closed,
32        }
33    }
34}