FactorySender

Struct FactorySender 

Source
pub struct FactorySender<C: FactoryComponent> { /* private fields */ }
Expand description

Contains senders to send and receive messages from a Component.

Implementations§

Source§

impl<C: FactoryComponent> FactorySender<C>

Source

pub fn input_sender(&self) -> &Sender<C::Input>

Retrieve the sender for input messages.

Useful to forward inputs from another component. If you just need to send input messages, input() is more concise.

Examples found in repository?
relm4/examples/tab_game.rs (line 141)
140    fn init_model(value: Self::Init, _index: &DynamicIndex, sender: FactorySender<Self>) -> Self {
141        GAME_STATE.subscribe(sender.input_sender(), |_| CounterMsg::Update);
142        Self { id: value }
143    }
Source

pub fn output_sender(&self) -> &Sender<C::Output>

Retrieve the sender for output messages.

Useful to forward outputs from another component. If you just need to send output messages, output() is more concise.

Examples found in repository?
relm4/examples/state_management.rs (line 165)
161    fn init_model(_name: Self::Init, index: &DynamicIndex, sender: FactorySender<Self>) -> Self {
162        let task_index = index.clone();
163
164        let tags = FactoryVecDeque::builder().launch_default().forward(
165            sender.output_sender(),
166            move |output| match output {
167                TagOutput::Delete(tag_index) => {
168                    TaskOutput::DeleteTag(task_index.clone(), tag_index)
169                }
170            },
171        );
172
173        Self {
174            name: "".into(),
175            tags,
176        }
177    }
Source

pub fn command_sender(&self) -> &Sender<C::CommandOutput>

Retrieve the sender for command output messages.

Useful to forward outputs from another component. If you just need to send output messages, command() is more concise.

Source

pub fn input(&self, message: C::Input)

Emit an input to the component.

Examples found in repository?
relm4/examples/grid_factory.rs (line 84)
66    fn init_widgets(
67        &mut self,
68        index: &DynamicIndex,
69        root: Self::Root,
70        _returned_widget: &gtk::Widget,
71        sender: FactorySender<Self>,
72    ) -> Self::Widgets {
73        relm4::view! {
74            #[local_ref]
75            root -> gtk::Box {
76                #[name(label)]
77                gtk::Label {
78                    set_label: &self.value.to_string(),
79                    set_width_chars: 3,
80                },
81
82                gtk::Button {
83                    set_label: "+",
84                    connect_clicked => CounterMsg::Increment,
85                },
86
87                gtk::Button {
88                    set_label: "-",
89                    connect_clicked => CounterMsg::Decrement,
90                },
91
92                gtk::Button {
93                    set_label: "Up",
94                    connect_clicked[sender, index] => move |_| {
95                        sender.output(CounterOutput::MoveUp(index.clone())).unwrap();
96                    }
97                },
98
99                gtk::Button {
100                    set_label: "Down",
101                    connect_clicked[sender, index] => move |_| {
102                        sender.output(CounterOutput::MoveDown(index.clone())).unwrap();
103                    }
104                },
105
106                gtk::Button {
107                    set_label: "To Start",
108                    connect_clicked[sender, index] => move |_| {
109                        sender.output(CounterOutput::SendFront(index.clone())).unwrap();
110                    }
111                }
112            }
113        }
114
115        CounterWidgets { label }
116    }
Source

pub fn output(&self, message: C::Output) -> Result<(), C::Output>

Emit an output to the component.

Returns Err if all receivers were dropped, for example by detach.

Examples found in repository?
relm4/examples/grid_factory.rs (line 95)
66    fn init_widgets(
67        &mut self,
68        index: &DynamicIndex,
69        root: Self::Root,
70        _returned_widget: &gtk::Widget,
71        sender: FactorySender<Self>,
72    ) -> Self::Widgets {
73        relm4::view! {
74            #[local_ref]
75            root -> gtk::Box {
76                #[name(label)]
77                gtk::Label {
78                    set_label: &self.value.to_string(),
79                    set_width_chars: 3,
80                },
81
82                gtk::Button {
83                    set_label: "+",
84                    connect_clicked => CounterMsg::Increment,
85                },
86
87                gtk::Button {
88                    set_label: "-",
89                    connect_clicked => CounterMsg::Decrement,
90                },
91
92                gtk::Button {
93                    set_label: "Up",
94                    connect_clicked[sender, index] => move |_| {
95                        sender.output(CounterOutput::MoveUp(index.clone())).unwrap();
96                    }
97                },
98
99                gtk::Button {
100                    set_label: "Down",
101                    connect_clicked[sender, index] => move |_| {
102                        sender.output(CounterOutput::MoveDown(index.clone())).unwrap();
103                    }
104                },
105
106                gtk::Button {
107                    set_label: "To Start",
108                    connect_clicked[sender, index] => move |_| {
109                        sender.output(CounterOutput::SendFront(index.clone())).unwrap();
110                    }
111                }
112            }
113        }
114
115        CounterWidgets { label }
116    }
Source

pub fn command<Cmd, Fut>(&self, cmd: Cmd)
where Cmd: FnOnce(Sender<C::CommandOutput>, ShutdownReceiver) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send,

Spawns an asynchronous command. You can bind the the command to the lifetime of the component by using a ShutdownReceiver.

Source

pub fn spawn_command<Cmd>(&self, cmd: Cmd)
where Cmd: FnOnce(Sender<C::CommandOutput>) + Send + 'static,

Spawns a synchronous command.

This is particularly useful for CPU-intensive background jobs that need to run on a thread-pool in the background.

If you expect the component to be dropped while the command is running take care while sending messages!

Source

pub fn oneshot_command<Fut>(&self, future: Fut)
where Fut: Future<Output = C::CommandOutput> + Send + 'static,

Spawns a future that will be dropped as soon as the factory component is shut down.

Essentially, this is a simpler version of Self::command().

Source

pub fn spawn_oneshot_command<Cmd>(&self, cmd: Cmd)
where Cmd: FnOnce() -> C::CommandOutput + Send + 'static,

Spawns a synchronous command that will be dropped as soon as the factory component is shut down.

Essentially, this is a simpler version of Self::spawn_command().

Trait Implementations§

Source§

impl<C: FactoryComponent> Clone for FactorySender<C>

Source§

fn clone(&self) -> Self

Returns a duplicate 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<C: Debug + FactoryComponent> Debug for FactorySender<C>
where C::Input: Debug, C::Output: Debug, C::CommandOutput: Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<C> Freeze for FactorySender<C>

§

impl<C> RefUnwindSafe for FactorySender<C>

§

impl<C> Send for FactorySender<C>

§

impl<C> Sync for FactorySender<C>

§

impl<C> Unpin for FactorySender<C>

§

impl<C> UnwindSafe for FactorySender<C>

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