Struct Sender

Source
pub struct Sender<T>(/* private fields */);
Expand description

A Relm4 sender sends messages to a component or worker.

Implementations§

Source§

impl<T> Sender<T>

Source

pub fn emit(&self, message: T)

Sends a message through the channel.

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

Source

pub fn send(&self, message: T) -> Result<(), T>

Sends a message through the channel.

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

Examples found in repository?
examples/drawing.rs (line 134)
114    fn init(
115        _: Self::Init,
116        root: Self::Root,
117        sender: ComponentSender<Self>,
118    ) -> ComponentParts<Self> {
119        let model = App {
120            width: 100.0,
121            height: 100.0,
122            points: Vec::new(),
123            handler: DrawHandler::new(),
124        };
125
126        let area = model.handler.drawing_area();
127        let widgets = view_output!();
128
129        sender.command(|out, shutdown| {
130            shutdown
131                .register(async move {
132                    loop {
133                        tokio::time::sleep(Duration::from_millis(20)).await;
134                        out.send(UpdatePointsMsg).unwrap();
135                    }
136                })
137                .drop_on_shutdown()
138        });
139
140        ComponentParts { model, widgets }
141    }
More examples
Hide additional examples
examples/progress.rs (line 131)
120    fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>, _root: &Self::Root) {
121        match message {
122            Input::Compute => {
123                self.computing = true;
124                sender.command(|out, shutdown| {
125                    shutdown
126                        // Performs this operation until a shutdown is triggered
127                        .register(async move {
128                            let mut progress = 0.0;
129
130                            while progress < 1.0 {
131                                out.send(CmdOut::Progress(progress)).unwrap();
132                                progress += 0.1;
133                                tokio::time::sleep(std::time::Duration::from_millis(333)).await;
134                            }
135
136                            out.send(CmdOut::Finished(Ok("42".into()))).unwrap();
137                        })
138                        // Perform task until a shutdown interrupts it
139                        .drop_on_shutdown()
140                        // Wrap into a `Pin<Box<Future>>` for return
141                        .boxed()
142                });
143            }
144        }
145    }
examples/tab_game.rs (line 262)
250    fn update(&mut self, msg: Self::Input, sender: ComponentSender<Self>, _root: &Self::Root) {
251        match msg {
252            AppMsg::StartGame(index) => {
253                self.start_index = Some(index);
254                sender.command(|sender, _| async move {
255                    for i in (1..4).rev() {
256                        *GAME_STATE.write() = GameState::Countdown(i);
257                        relm4::tokio::time::sleep(Duration::from_millis(1000)).await;
258                    }
259                    *GAME_STATE.write() = GameState::Running;
260                    for _ in 0..20 {
261                        relm4::tokio::time::sleep(Duration::from_millis(500)).await;
262                        sender.send(false).unwrap();
263                    }
264                    relm4::tokio::time::sleep(Duration::from_millis(1000)).await;
265                    sender.send(true).unwrap();
266                });
267            }
268            AppMsg::StopGame => {
269                *GAME_STATE.write() = GameState::Guessing;
270            }
271            AppMsg::SelectedGuess(index) => {
272                *GAME_STATE.write() = GameState::End(index == self.start_index.take().unwrap());
273            }
274        }
275    }
examples/settings_list.rs (line 37)
18fn main() {
19    gtk::Application::builder()
20        .application_id("org.relm4.SettingsListExample")
21        .launch(|_app, window| {
22            // Initialize a component's root widget
23            let mut component = App::builder()
24                // Attach the root widget to the given window.
25                .attach_to(&window)
26                // Start the component service with an initial parameter
27                .launch("Settings List Demo".into())
28                // Attach the returned receiver's messages to this closure.
29                .connect_receiver(move |sender, message| match message {
30                    Output::Clicked(id) => {
31                        eprintln!("ID {id} Clicked");
32
33                        match id {
34                            0 => xdg_open("https://github.com/Relm4/Relm4".into()),
35                            1 => xdg_open("https://docs.rs/relm4/".into()),
36                            2 => {
37                                sender.send(Input::Clear).unwrap();
38                            }
39                            _ => (),
40                        }
41                    }
42
43                    Output::Reload => {
44                        sender
45                            .send(Input::AddSetting {
46                                description: "Browse GitHub Repository".into(),
47                                button: "GitHub".into(),
48                                id: 0,
49                            })
50                            .unwrap();
51
52                        sender
53                            .send(Input::AddSetting {
54                                description: "Browse Documentation".into(),
55                                button: "Docs".into(),
56                                id: 1,
57                            })
58                            .unwrap();
59
60                        sender
61                            .send(Input::AddSetting {
62                                description: "Clear List".into(),
63                                button: "Clear".into(),
64                                id: 2,
65                            })
66                            .unwrap();
67                    }
68                });
69
70            // Keep runtime alive after the component is dropped
71            component.detach_runtime();
72
73            println!("parent is {:?}", component.widget().toplevel_window());
74        });
75}

Trait Implementations§

Source§

impl<T> Clone for Sender<T>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Sender<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> From<Sender<T>> for Sender<T>

Source§

fn from(sender: Sender<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for Sender<T>

§

impl<T> RefUnwindSafe for Sender<T>

§

impl<T> Send for Sender<T>
where T: Send,

§

impl<T> Sync for Sender<T>
where T: Send,

§

impl<T> Unpin for Sender<T>

§

impl<T> UnwindSafe for Sender<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<C> AsyncPosition<()> for C

Source§

fn position(_index: usize)

Returns the position. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<C, I> Position<(), I> for C

Source§

fn position(&self, _index: &I)

Returns the position. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more