Skip to content

Commit

Permalink
Merge pull request #155 from ejmahler/remove-clone
Browse files Browse the repository at this point in the history
Remove Clone bound on Application::Message
  • Loading branch information
hecrj authored Jan 13, 2020
2 parents bad1bab + 0cbd666 commit 142dc1e
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 26 deletions.
6 changes: 2 additions & 4 deletions native/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ where
/// ```
pub fn map<F, B>(self, f: F) -> Element<'a, B, Renderer>
where
Message: 'static + Clone,
Message: 'static,
Renderer: 'a,
B: 'static,
F: 'static + Fn(Message) -> B,
Expand Down Expand Up @@ -269,7 +269,6 @@ impl<'a, A, B, Renderer> Map<'a, A, B, Renderer> {

impl<'a, A, B, Renderer> Widget<B, Renderer> for Map<'a, A, B, Renderer>
where
A: Clone,
Renderer: crate::Renderer,
{
fn width(&self) -> Length {
Expand Down Expand Up @@ -309,8 +308,7 @@ where
);

original_messages
.iter()
.cloned()
.drain(..)
.for_each(|message| messages.push((self.mapper)(message)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
///
Expand Down
2 changes: 1 addition & 1 deletion src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
///
Expand Down
11 changes: 9 additions & 2 deletions web/src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ 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),
}
}
}

impl<Message> Bus<Message>
where
Message: 'static + Clone,
Message: 'static,
{
pub(crate) fn new() -> Self {
Self {
Expand Down
8 changes: 4 additions & 4 deletions web/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl<'a, Message> Element<'a, Message> {
/// [`Element`]: struct.Element.html
pub fn map<F, B>(self, f: F) -> Element<'a, B>
where
Message: 'static + Clone,
B: 'static + Clone,
Message: 'static,
B: 'static,
F: 'static + Fn(Message) -> B,
{
Element {
Expand Down Expand Up @@ -82,8 +82,8 @@ impl<'a, A, B> Map<'a, A, B> {

impl<'a, A, B> Widget<B> for Map<'a, A, B>
where
A: 'static + Clone,
B: 'static + Clone,
A: 'static,
B: 'static,
{
fn node<'b>(
&self,
Expand Down
18 changes: 14 additions & 4 deletions web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
///
Expand Down Expand Up @@ -148,16 +148,26 @@ pub trait Application {
}
}

#[derive(Clone)]

struct Instance<Message> {
title: String,
ui: Rc<RefCell<Box<dyn Application<Message = Message>>>>,
vdom: Rc<RefCell<Option<dodrio::VdomWeak>>>,
}

impl<Message> Clone for Instance<Message> {
fn clone(&self) -> Self {
Self {
title: self.title.clone(),
ui: Rc::clone(&self.ui),
vdom: Rc::clone(&self.vdom),
}
}
}

impl<Message> Instance<Message>
where
Message: 'static + Clone,
Message: 'static
{
fn new(ui: impl Application<Message = Message> + 'static) -> Self {
Self {
Expand Down Expand Up @@ -221,7 +231,7 @@ where

impl<Message> dodrio::Render for Instance<Message>
where
Message: 'static + Clone,
Message: 'static,
{
fn render<'a, 'bump>(
&'a self,
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

0 comments on commit 142dc1e

Please sign in to comment.