This is unreleased documentation for Yew Next version.
For up-to-date documentation, see the latest version on docs.rs.

yew_agent/oneshot/
registrar.rs

1use std::fmt;
2
3use serde::de::Deserialize;
4use serde::ser::Serialize;
5
6use super::traits::Oneshot;
7use super::worker::OneshotWorker;
8use crate::codec::{Bincode, Codec};
9use crate::traits::Registrable;
10use crate::worker::WorkerRegistrar;
11
12/// A registrar for oneshot workers.
13pub struct OneshotRegistrar<T, CODEC = Bincode>
14where
15    T: Oneshot + 'static,
16    CODEC: Codec + 'static,
17{
18    inner: WorkerRegistrar<OneshotWorker<T>, CODEC>,
19}
20
21impl<T, CODEC> Default for OneshotRegistrar<T, CODEC>
22where
23    T: Oneshot + 'static,
24    CODEC: Codec + 'static,
25{
26    fn default() -> Self {
27        Self::new()
28    }
29}
30
31impl<N, CODEC> OneshotRegistrar<N, CODEC>
32where
33    N: Oneshot + 'static,
34    CODEC: Codec + 'static,
35{
36    /// Creates a new Oneshot Registrar.
37    pub fn new() -> Self {
38        Self {
39            inner: OneshotWorker::<N>::registrar().encoding::<CODEC>(),
40        }
41    }
42
43    /// Sets the encoding.
44    pub fn encoding<C>(&self) -> OneshotRegistrar<N, C>
45    where
46        C: Codec + 'static,
47    {
48        OneshotRegistrar {
49            inner: self.inner.encoding::<C>(),
50        }
51    }
52
53    /// Registers the worker.
54    pub fn register(&self)
55    where
56        N::Input: Serialize + for<'de> Deserialize<'de>,
57        N::Output: Serialize + for<'de> Deserialize<'de>,
58    {
59        self.inner.register()
60    }
61}
62
63impl<T, CODEC> fmt::Debug for OneshotRegistrar<T, CODEC>
64where
65    T: Oneshot + 'static,
66    CODEC: Codec + 'static,
67{
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        f.debug_struct("OneshotRegistrar<_>").finish()
70    }
71}