diff --git a/native/src/element.rs b/native/src/element.rs index fab73f77de..ae47e89329 100644 --- a/native/src/element.rs +++ b/native/src/element.rs @@ -171,7 +171,7 @@ where /// ``` pub fn map(self, f: F) -> Element<'a, B, Renderer> where - Message: 'static + Clone, + Message: 'static, Renderer: 'a, B: 'static, F: 'static + Fn(Message) -> B, @@ -269,7 +269,6 @@ impl<'a, A, B, Renderer> Map<'a, A, B, Renderer> { impl<'a, A, B, Renderer> Widget for Map<'a, A, B, Renderer> where - A: Clone, Renderer: crate::Renderer, { fn width(&self) -> Length { @@ -309,8 +308,7 @@ where ); original_messages - .iter() - .cloned() + .drain(..) .for_each(|message| messages.push((self.mapper)(message))); } diff --git a/src/application.rs b/src/application.rs index 7dd767740d..5ea64ba81b 100644 --- a/src/application.rs +++ b/src/application.rs @@ -83,7 +83,7 @@ pub trait Application: Sized { /// The type of __messages__ your [`Application`] will produce. /// /// [`Application`]: trait.Application.html - type Message: std::fmt::Debug + Send + Clone; + type Message: std::fmt::Debug + Send; /// Initializes the [`Application`]. /// diff --git a/src/sandbox.rs b/src/sandbox.rs index 75020b16b9..dda4c3f510 100644 --- a/src/sandbox.rs +++ b/src/sandbox.rs @@ -81,7 +81,7 @@ pub trait Sandbox { /// The type of __messages__ your [`Sandbox`] will produce. /// /// [`Sandbox`]: trait.Sandbox.html - type Message: std::fmt::Debug + Send + Clone; + type Message: std::fmt::Debug + Send; /// Initializes the [`Sandbox`]. /// diff --git a/web/src/bus.rs b/web/src/bus.rs index 1b650b285d..b3984aff7a 100644 --- a/web/src/bus.rs +++ b/web/src/bus.rs @@ -8,14 +8,21 @@ use std::rc::Rc; /// /// [`Application`]: trait.Application.html #[allow(missing_debug_implementations)] -#[derive(Clone)] pub struct Bus { publish: Rc>, } +impl Clone for Bus { + fn clone(&self) -> Self { + Self { + publish: Rc::clone(&self.publish), + } + } +} + impl Bus where - Message: 'static + Clone, + Message: 'static, { pub(crate) fn new() -> Self { Self { diff --git a/web/src/element.rs b/web/src/element.rs index 85fa7c3472..0315d7d6c9 100644 --- a/web/src/element.rs +++ b/web/src/element.rs @@ -38,8 +38,8 @@ impl<'a, Message> Element<'a, Message> { /// [`Element`]: struct.Element.html pub fn map(self, f: F) -> Element<'a, B> where - Message: 'static + Clone, - B: 'static + Clone, + Message: 'static, + B: 'static, F: 'static + Fn(Message) -> B, { Element { @@ -82,8 +82,8 @@ impl<'a, A, B> Map<'a, A, B> { impl<'a, A, B> Widget for Map<'a, A, B> where - A: 'static + Clone, - B: 'static + Clone, + A: 'static, + B: 'static, { fn node<'b>( &self, diff --git a/web/src/lib.rs b/web/src/lib.rs index d4c422d20b..7ea22e857e 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -91,7 +91,7 @@ pub trait Application { /// The type of __messages__ your [`Application`] will produce. /// /// [`Application`]: trait.Application.html - type Message: Clone; + type Message; /// Initializes the [`Application`]. /// @@ -148,16 +148,26 @@ pub trait Application { } } -#[derive(Clone)] + struct Instance { title: String, ui: Rc>>>, vdom: Rc>>, } +impl Clone for Instance { + fn clone(&self) -> Self { + Self { + title: self.title.clone(), + ui: Rc::clone(&self.ui), + vdom: Rc::clone(&self.vdom), + } + } +} + impl Instance where - Message: 'static + Clone, + Message: 'static { fn new(ui: impl Application + 'static) -> Self { Self { @@ -221,7 +231,7 @@ where impl dodrio::Render for Instance where - Message: 'static + Clone, + Message: 'static, { fn render<'a, 'bump>( &'a self, diff --git a/web/src/widget/checkbox.rs b/web/src/widget/checkbox.rs index b81a0d524f..34d13a1b86 100644 --- a/web/src/widget/checkbox.rs +++ b/web/src/widget/checkbox.rs @@ -1,6 +1,7 @@ use crate::{style, Bus, Color, Element, Widget}; use dodrio::bumpalo; +use std::rc::Rc; /// A box that can be checked. /// @@ -22,7 +23,7 @@ use dodrio::bumpalo; #[allow(missing_debug_implementations)] pub struct Checkbox { is_checked: bool, - on_toggle: Box Message>, + on_toggle: Rc Message>, label: String, label_color: Option, } @@ -44,7 +45,7 @@ impl Checkbox { { Checkbox { is_checked, - on_toggle: Box::new(f), + on_toggle: Rc::new(f), label: String::from(label), label_color: None, } @@ -61,7 +62,7 @@ impl Checkbox { impl Widget for Checkbox where - Message: 'static + Clone, + Message: 'static, { fn node<'b>( &self, @@ -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) @@ -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(); }) @@ -96,7 +99,7 @@ where impl<'a, Message> From> for Element<'a, Message> where - Message: 'static + Clone, + Message: 'static, { fn from(checkbox: Checkbox) -> Element<'a, Message> { Element::new(checkbox) diff --git a/web/src/widget/scrollable.rs b/web/src/widget/scrollable.rs index 710bb70ac6..f146e00736 100644 --- a/web/src/widget/scrollable.rs +++ b/web/src/widget/scrollable.rs @@ -134,7 +134,7 @@ where impl<'a, Message> From> for Element<'a, Message> where - Message: 'static + Clone, + Message: 'static, { fn from(scrollable: Scrollable<'a, Message>) -> Element<'a, Message> { Element::new(scrollable) diff --git a/web/src/widget/slider.rs b/web/src/widget/slider.rs index 5b203e0791..fc9557812b 100644 --- a/web/src/widget/slider.rs +++ b/web/src/widget/slider.rs @@ -82,7 +82,7 @@ impl<'a, Message> Slider<'a, Message> { impl<'a, Message> Widget for Slider<'a, Message> where - Message: 'static + Clone, + Message: 'static, { fn node<'b>( &self, @@ -130,7 +130,7 @@ where impl<'a, Message> From> for Element<'a, Message> where - Message: 'static + Clone, + Message: 'static, { fn from(slider: Slider<'a, Message>) -> Element<'a, Message> { Element::new(slider) diff --git a/web/src/widget/text_input.rs b/web/src/widget/text_input.rs index eedc25bc4f..a478874af6 100644 --- a/web/src/widget/text_input.rs +++ b/web/src/widget/text_input.rs @@ -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