Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Clone bound on Application::Message #155

Merged
merged 5 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion web/src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ use std::rc::Rc;
///
/// [`Application`]: trait.Application.html
#[allow(missing_debug_implementations)]
#[derive(Clone)]
pub struct Bus<Message> {
publish: Rc<Box<dyn Fn(Message, &mut dyn dodrio::RootRender)>>,
}

impl<Message> Clone for Bus<Message> {
fn clone(&self) -> Self {
Self {
publish: Rc::clone(&self.publish),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you discovered the same thing I did, where #[derive(Clone)] seemsto generate a wonky implementation of clone here, which only kicks in if Message is Clone -- even though all the fields are trivially cloneable unconditionally. And the solution isto just manually implement an unconditional clone.

}
}
}

impl<Message> Bus<Message>
where
Message: 'static,
Expand Down
15 changes: 9 additions & 6 deletions web/src/widget/checkbox.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{style, Bus, Color, Element, Widget};

use dodrio::bumpalo;
use std::rc::Rc;

/// A box that can be checked.
///
Expand All @@ -22,7 +23,7 @@ use dodrio::bumpalo;
#[allow(missing_debug_implementations)]
pub struct Checkbox<Message> {
is_checked: bool,
on_toggle: Box<dyn Fn(bool) -> Message>,
on_toggle: Rc<dyn Fn(bool) -> Message>,
label: String,
label_color: Option<Color>,
}
Expand All @@ -44,7 +45,7 @@ impl<Message> Checkbox<Message> {
{
Checkbox {
is_checked,
on_toggle: Box::new(f),
on_toggle: Rc::new(f),
label: String::from(label),
label_color: None,
}
Expand All @@ -61,7 +62,7 @@ impl<Message> Checkbox<Message> {

impl<Message> Widget<Message> for Checkbox<Message>
where
Message: 'static + Clone,
Message: 'static,
{
fn node<'b>(
&self,
Expand All @@ -74,7 +75,8 @@ where
let checkbox_label = bumpalo::format!(in bump, "{}", self.label);

let event_bus = bus.clone();
let msg = (self.on_toggle)(!self.is_checked);
let on_toggle = self.on_toggle.clone();
let is_checked = self.is_checked;

// TODO: Complete styling
label(bump)
Expand All @@ -83,7 +85,8 @@ where
.attr("type", "checkbox")
.bool_attr("checked", self.is_checked)
.on("click", move |root, vdom, _event| {
event_bus.publish(msg.clone(), root);
let msg = on_toggle(!is_checked);
event_bus.publish(msg, root);

vdom.schedule_render();
})
Expand All @@ -96,7 +99,7 @@ where

impl<'a, Message> From<Checkbox<Message>> for Element<'a, Message>
where
Message: 'static + Clone,
Message: 'static,
{
fn from(checkbox: Checkbox<Message>) -> Element<'a, Message> {
Element::new(checkbox)
Expand Down
2 changes: 1 addition & 1 deletion web/src/widget/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ where

impl<'a, Message> From<Scrollable<'a, Message>> for Element<'a, Message>
where
Message: 'static + Clone,
Message: 'static,
{
fn from(scrollable: Scrollable<'a, Message>) -> Element<'a, Message> {
Element::new(scrollable)
Expand Down
4 changes: 2 additions & 2 deletions web/src/widget/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<'a, Message> Slider<'a, Message> {

impl<'a, Message> Widget<Message> for Slider<'a, Message>
where
Message: 'static + Clone,
Message: 'static,
{
fn node<'b>(
&self,
Expand Down Expand Up @@ -130,7 +130,7 @@ where

impl<'a, Message> From<Slider<'a, Message>> for Element<'a, Message>
where
Message: 'static + Clone,
Message: 'static,
{
fn from(slider: Slider<'a, Message>) -> Element<'a, Message> {
Element::new(slider)
Expand Down
2 changes: 1 addition & 1 deletion web/src/widget/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<'a, Message> TextInput<'a, Message> {
self.is_secure = true;
self
}

/// Sets the width of the [`TextInput`].
///
/// [`TextInput`]: struct.TextInput.html
Expand Down