pub struct Sender<T>(/* private fields */);
Expand description
A Relm4 sender sends messages to a component or worker.
Implementations§
Source§impl<T> Sender<T>
impl<T> Sender<T>
Sourcepub fn emit(&self, message: T)
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.
Sourcepub fn send(&self, message: T) -> Result<(), T>
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
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§
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<C> AsyncPosition<()> for C
impl<C> AsyncPosition<()> for C
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more