Struct relm4::Sender

source ·
pub struct Sender<T>(_);
Expand description

A Relm4 sender sends messages to a component or worker.

Implementations§

Sends a message through the channel.

This method ignores errors. Only a log message will appear when sending fails.

Sends a message through the channel.

If all receivers where dropped, Err is returned with the content of the message.

Examples found in repository?
src/component/async/controller.rs (line 15)
14
15
16
    fn emit(&self, event: C::Input) {
        self.sender().send(event).unwrap();
    }
More examples
Hide additional examples
src/component/sync/controller.rs (line 16)
15
16
17
    fn emit(&self, event: C::Input) {
        self.sender().send(event).unwrap();
    }
src/component/worker.rs (line 258)
257
258
259
    pub fn emit(&self, event: W::Input) {
        self.sender.send(event).unwrap();
    }
src/component/sync/message_broker.rs (line 56)
55
56
57
    pub fn send(&self, input: C::Input) {
        self.inner.sender.send(input).unwrap();
    }
src/component/sync/state_watcher.rs (line 27)
26
27
28
29
    pub fn get_mut(&self) -> RefMut<'_, ComponentParts<C>> {
        self.notifier.send(()).unwrap();
        self.state.borrow_mut()
    }
src/channel/mod.rs (line 37)
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    pub fn emit(&self, message: T) {
        if self.send(message).is_err() {
            tracing::warn!("Receiver was dropped");
        }
    }

    /// Sends a message through the channel.
    ///
    /// If all receivers where dropped, [`Err`] is returned
    /// with the content of the message.
    pub fn send(&self, message: T) -> Result<(), T> {
        self.0.send(message).map_err(|e| e.into_inner())
    }
}

impl<T> Clone for Sender<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<T> fmt::Debug for Sender<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Sender").field(&self.0).finish()
    }
}

/// A Relm4 receiver receives messages from a component or worker.
pub struct Receiver<T>(pub(crate) flume::Receiver<T>);

impl<T> Receiver<T> {
    /// Receives a message from a component or worker.
    ///
    /// Returns [`None`] if all senders have been disconnected.
    pub async fn recv(&self) -> Option<T> {
        self.0.recv_async().await.ok()
    }

    /// Receives a message synchronously from a component or worker.
    ///
    /// Returns [`None`] if all senders have been disconnected.
    #[must_use]
    pub fn recv_sync(&self) -> Option<T> {
        self.0.recv().ok()
    }

    #[must_use]
    pub(crate) fn into_stream(self) -> RecvStream<'static, T> {
        self.0.into_stream()
    }

    /// Forwards an event from one channel to another.
    pub async fn forward<Transformer, Output>(
        self,
        sender: impl Into<Sender<Output>>,
        transformer: Transformer,
    ) where
        Transformer: (Fn(T) -> Output) + 'static,
        Output: 'static,
    {
        let sender = sender.into();
        while let Some(event) = self.recv().await {
            if sender.send(transformer(event)).is_err() {
                return;
            }
        }
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Returns the position. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Returns the position. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more