Skip to content

Commit 90cb074

Browse files
committed
Begin removing Clone bounds on Msg if possible.
1 parent 59968ed commit 90cb074

File tree

9 files changed

+103
-111
lines changed

9 files changed

+103
-111
lines changed

src/dom_types.rs

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,100 +67,100 @@ pub trait UpdateEl<T> {
6767
fn update(self, el: &mut T);
6868
}
6969

70-
impl<Ms: Clone> UpdateEl<El<Ms>> for Attrs {
70+
impl<Ms> UpdateEl<El<Ms>> for Attrs {
7171
fn update(self, el: &mut El<Ms>) {
7272
el.attrs.merge(self);
7373
}
7474
}
7575

76-
impl<Ms: Clone> UpdateEl<El<Ms>> for &Attrs {
76+
impl<Ms> UpdateEl<El<Ms>> for &Attrs {
7777
fn update(self, el: &mut El<Ms>) {
7878
el.attrs.merge(self.clone());
7979
}
8080
}
8181

82-
impl<Ms: Clone> UpdateEl<El<Ms>> for Style {
82+
impl<Ms> UpdateEl<El<Ms>> for Style {
8383
fn update(self, el: &mut El<Ms>) {
8484
el.style.merge(self);
8585
}
8686
}
8787

88-
impl<Ms: Clone> UpdateEl<El<Ms>> for &Style {
88+
impl<Ms> UpdateEl<El<Ms>> for &Style {
8989
fn update(self, el: &mut El<Ms>) {
9090
el.style.merge(self.clone());
9191
}
9292
}
9393

94-
impl<Ms: Clone> UpdateEl<El<Ms>> for Listener<Ms> {
94+
impl<Ms> UpdateEl<El<Ms>> for Listener<Ms> {
9595
fn update(self, el: &mut El<Ms>) {
9696
el.listeners.push(self)
9797
}
9898
}
9999

100-
impl<Ms: Clone> UpdateEl<El<Ms>> for Vec<Listener<Ms>> {
100+
impl<Ms> UpdateEl<El<Ms>> for Vec<Listener<Ms>> {
101101
fn update(mut self, el: &mut El<Ms>) {
102102
el.listeners.append(&mut self);
103103
}
104104
}
105105

106-
impl<Ms: Clone> UpdateEl<El<Ms>> for DidMount<Ms> {
106+
impl<Ms> UpdateEl<El<Ms>> for DidMount<Ms> {
107107
fn update(self, el: &mut El<Ms>) {
108108
el.hooks.did_mount = Some(self)
109109
}
110110
}
111111

112-
impl<Ms: Clone> UpdateEl<El<Ms>> for DidUpdate<Ms> {
112+
impl<Ms> UpdateEl<El<Ms>> for DidUpdate<Ms> {
113113
fn update(self, el: &mut El<Ms>) {
114114
el.hooks.did_update = Some(self)
115115
}
116116
}
117117

118-
impl<Ms: Clone> UpdateEl<El<Ms>> for WillUnmount<Ms> {
118+
impl<Ms> UpdateEl<El<Ms>> for WillUnmount<Ms> {
119119
fn update(self, el: &mut El<Ms>) {
120120
el.hooks.will_unmount = Some(self)
121121
}
122122
}
123123

124-
impl<Ms: Clone> UpdateEl<El<Ms>> for &str {
124+
impl<Ms> UpdateEl<El<Ms>> for &str {
125125
// This, or some other mechanism seems to work for String too... note sure why.
126126
fn update(self, el: &mut El<Ms>) {
127127
el.children.push(Node::Text(Text::new(self.to_string())))
128128
}
129129
}
130130

131-
impl<Ms: Clone> UpdateEl<El<Ms>> for El<Ms> {
131+
impl<Ms> UpdateEl<El<Ms>> for El<Ms> {
132132
fn update(self, el: &mut El<Ms>) {
133133
el.children.push(Node::Element(self))
134134
}
135135
}
136136

137-
impl<Ms: Clone> UpdateEl<El<Ms>> for Vec<El<Ms>> {
137+
impl<Ms> UpdateEl<El<Ms>> for Vec<El<Ms>> {
138138
fn update(self, el: &mut El<Ms>) {
139139
el.children
140140
.append(&mut self.into_iter().map(Node::Element).collect());
141141
}
142142
}
143143

144-
impl<Ms: Clone> UpdateEl<El<Ms>> for Node<Ms> {
144+
impl<Ms> UpdateEl<El<Ms>> for Node<Ms> {
145145
fn update(self, el: &mut El<Ms>) {
146146
el.children.push(self)
147147
}
148148
}
149149

150-
impl<Ms: Clone> UpdateEl<El<Ms>> for Vec<Node<Ms>> {
150+
impl<Ms> UpdateEl<El<Ms>> for Vec<Node<Ms>> {
151151
fn update(mut self, el: &mut El<Ms>) {
152152
el.children.append(&mut self);
153153
}
154154
}
155155

156156
/// This is intended only to be used for the custom! element macro.
157-
impl<Ms: Clone> UpdateEl<El<Ms>> for Tag {
157+
impl<Ms> UpdateEl<El<Ms>> for Tag {
158158
fn update(self, el: &mut El<Ms>) {
159159
el.tag = self;
160160
}
161161
}
162162

163-
impl<Ms: Clone, I, U, F> UpdateEl<El<Ms>> for std::iter::Map<I, F>
163+
impl<Ms, I, U, F> UpdateEl<El<Ms>> for std::iter::Map<I, F>
164164
where
165165
I: Iterator,
166166
U: UpdateEl<El<Ms>>,
@@ -669,29 +669,29 @@ make_tags! {
669669
Placeholder => "placeholder"
670670
}
671671

672-
pub trait View<Ms: 'static + Clone> {
672+
pub trait View<Ms: 'static> {
673673
fn els(self) -> Vec<Node<Ms>>;
674674
}
675675

676-
impl<Ms: Clone> View<Ms> for El<Ms> {
676+
impl<Ms> View<Ms> for El<Ms> {
677677
fn els(self) -> Vec<Node<Ms>> {
678678
vec![Node::Element(self)]
679679
}
680680
}
681681

682-
impl<Ms: Clone> View<Ms> for Vec<El<Ms>> {
682+
impl<Ms> View<Ms> for Vec<El<Ms>> {
683683
fn els(self) -> Vec<Node<Ms>> {
684684
self.into_iter().map(Node::Element).collect()
685685
}
686686
}
687687

688-
impl<Ms: 'static + Clone> View<Ms> for Node<Ms> {
688+
impl<Ms: 'static> View<Ms> for Node<Ms> {
689689
fn els(self) -> Vec<Node<Ms>> {
690690
vec![self]
691691
}
692692
}
693693

694-
impl<Ms: 'static + Clone> View<Ms> for Vec<Node<Ms>> {
694+
impl<Ms: 'static> View<Ms> for Vec<Node<Ms>> {
695695
fn els(self) -> Vec<Node<Ms>> {
696696
self
697697
}
@@ -727,16 +727,25 @@ impl Text {
727727

728728
/// An component in our virtual DOM. Related to, but different from
729729
/// [DOM Nodes](https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType)
730-
#[derive(Clone, Debug, PartialEq)]
731-
pub enum Node<Ms: 'static + Clone> {
730+
#[derive(Debug, PartialEq)]
731+
pub enum Node<Ms: 'static> {
732732
Element(El<Ms>),
733733
// Svg(El<Ms>), // May be best to handle using namespace field on El
734734
Text(Text),
735735
Empty,
736736
}
737+
impl<Ms: 'static> Clone for Node<Ms> {
738+
fn clone(&self) -> Self {
739+
match self {
740+
Self::Element(e) => Self::Element(e.clone()),
741+
Self::Text(t) => Self::Text(t.clone()),
742+
Self::Empty => Self::Empty,
743+
}
744+
}
745+
}
737746

738747
// Mappers for `El` manipulations.
739-
impl<Ms: Clone> Node<Ms> {
748+
impl<Ms> Node<Ms> {
740749
/// See `El::from_markdown`
741750
pub fn from_markdown(markdown: &str) -> Vec<Node<Ms>> {
742751
El::from_markdown(markdown)
@@ -817,7 +826,7 @@ impl<Ms: Clone> Node<Ms> {
817826
}
818827
}
819828
// Workspace manipulations.
820-
impl<Ms: Clone> Node<Ms> {
829+
impl<Ms> Node<Ms> {
821830
pub(crate) fn strip_ws_nodes(&mut self) -> &mut Self {
822831
match self {
823832
Node::Element(e) => e.strip_ws_nodes(),
@@ -828,7 +837,7 @@ impl<Ms: Clone> Node<Ms> {
828837
}
829838
}
830839
// Convenience methods for `Node` a la `Result` and `Option`.
831-
impl<Ms: Clone> Node<Ms> {
840+
impl<Ms> Node<Ms> {
832841
pub fn new_text(text: impl Into<Cow<'static, str>>) -> Self {
833842
Node::Text(Text::new(text))
834843
}
@@ -883,7 +892,7 @@ impl<Ms: Clone> Node<Ms> {
883892
}
884893
}
885894

886-
impl<Ms: 'static + Clone, OtherMs: 'static + Clone> MessageMapper<Ms, OtherMs> for Node<Ms> {
895+
impl<Ms: 'static, OtherMs: 'static> MessageMapper<Ms, OtherMs> for Node<Ms> {
887896
type SelfWithOtherMs = Node<OtherMs>;
888897
/// See note on impl for El
889898
fn map_message(self, f: impl FnOnce(Ms) -> OtherMs + 'static + Clone) -> Node<OtherMs> {
@@ -895,7 +904,7 @@ impl<Ms: 'static + Clone, OtherMs: 'static + Clone> MessageMapper<Ms, OtherMs> f
895904
}
896905
}
897906

898-
impl<Ms: 'static + Clone, OtherMs: 'static + Clone> MessageMapper<Ms, OtherMs> for Vec<Node<Ms>> {
907+
impl<Ms: 'static, OtherMs: 'static> MessageMapper<Ms, OtherMs> for Vec<Node<Ms>> {
899908
type SelfWithOtherMs = Vec<Node<OtherMs>>;
900909
fn map_message(self, f: impl FnOnce(Ms) -> OtherMs + 'static + Clone) -> Vec<Node<OtherMs>> {
901910
self.into_iter()
@@ -906,7 +915,7 @@ impl<Ms: 'static + Clone, OtherMs: 'static + Clone> MessageMapper<Ms, OtherMs> f
906915

907916
/// An component in our virtual DOM.
908917
#[derive(Debug)] // todo: Custom debug implementation where children are on new lines and indented.
909-
pub struct El<Ms: 'static + Clone> {
918+
pub struct El<Ms: 'static> {
910919
// Ms is a message type, as in part of TEA.
911920
// We call this 'El' instead of 'Element' for brevity, and to prevent
912921
// confusion with web_sys::Element.
@@ -920,7 +929,7 @@ pub struct El<Ms: 'static + Clone> {
920929
pub namespace: Option<Namespace>,
921930
pub hooks: LifecycleHooks<Ms>,
922931
}
923-
impl<Ms: Clone> El<Ms> {
932+
impl<Ms> El<Ms> {
924933
pub(crate) fn strip_ws_nodes(&mut self) {
925934
self.node_ws.take();
926935
for child in &mut self.children {
@@ -979,7 +988,7 @@ impl<Ms: 'static, OtherMs: 'static> MessageMapper<Ms, OtherMs> for LifecycleHook
979988
}
980989
}
981990

982-
impl<Ms: 'static + Clone, OtherMs: 'static + Clone> MessageMapper<Ms, OtherMs> for El<Ms> {
991+
impl<Ms: 'static, OtherMs: 'static> MessageMapper<Ms, OtherMs> for El<Ms> {
983992
type SelfWithOtherMs = El<OtherMs>;
984993
/// Maps an element's message to have another message.
985994
///
@@ -1011,7 +1020,7 @@ impl<Ms: 'static + Clone, OtherMs: 'static + Clone> MessageMapper<Ms, OtherMs> f
10111020
}
10121021
}
10131022

1014-
impl<Ms: 'static + Clone, OtherMs: 'static + Clone> MessageMapper<Ms, OtherMs> for Vec<El<Ms>> {
1023+
impl<Ms: 'static, OtherMs: 'static> MessageMapper<Ms, OtherMs> for Vec<El<Ms>> {
10151024
type SelfWithOtherMs = Vec<El<OtherMs>>;
10161025
fn map_message(self, f: impl FnOnce(Ms) -> OtherMs + 'static + Clone) -> Vec<El<OtherMs>> {
10171026
self.into_iter()
@@ -1020,7 +1029,7 @@ impl<Ms: 'static + Clone, OtherMs: 'static + Clone> MessageMapper<Ms, OtherMs> f
10201029
}
10211030
}
10221031

1023-
impl<Ms: Clone> El<Ms> {
1032+
impl<Ms> El<Ms> {
10241033
/// Create an empty element, specifying only the tag
10251034
pub fn empty(tag: Tag) -> Self {
10261035
Self {
@@ -1153,7 +1162,7 @@ impl<Ms: Clone> El<Ms> {
11531162

11541163
/// Allow the user to clone their Els. Note that there's no easy way to clone the
11551164
/// closures within listeners or lifestyle hooks, so we omit them.
1156-
impl<Ms: Clone> Clone for El<Ms> {
1165+
impl<Ms> Clone for El<Ms> {
11571166
fn clone(&self) -> Self {
11581167
Self {
11591168
tag: self.tag.clone(),
@@ -1168,7 +1177,7 @@ impl<Ms: Clone> Clone for El<Ms> {
11681177
}
11691178
}
11701179

1171-
impl<Ms: Clone> PartialEq for El<Ms> {
1180+
impl<Ms> PartialEq for El<Ms> {
11721181
fn eq(&self, other: &Self) -> bool {
11731182
// todo Again, note that the listeners check only checks triggers.
11741183
// Don't check children.

src/events.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ pub fn input_ev<Ms, T: ToString + Copy>(
319319

320320
/// Create an event that passes a `web_sys::KeyboardEvent`, allowing easy access
321321
/// to items like `key_code`() and key().
322-
pub fn keyboard_ev<Ms: Clone, T: ToString + Copy>(
322+
pub fn keyboard_ev<Ms, T: ToString + Copy>(
323323
trigger: T,
324324
handler: impl FnOnce(web_sys::KeyboardEvent) -> Ms + 'static + Clone,
325325
) -> Listener<Ms> {
@@ -335,7 +335,7 @@ pub fn keyboard_ev<Ms: Clone, T: ToString + Copy>(
335335
}
336336

337337
/// See `keyboard_ev`
338-
pub fn mouse_ev<Ms: Clone, T: ToString + Copy>(
338+
pub fn mouse_ev<Ms, T: ToString + Copy>(
339339
trigger: T,
340340
handler: impl FnOnce(web_sys::MouseEvent) -> Ms + 'static + Clone,
341341
) -> Listener<Ms> {
@@ -381,28 +381,28 @@ pub fn raw_ev<Ms, T: ToString + Copy>(
381381
)
382382
}
383383

384+
// TODO: Remove seemingly arbitrary `Clone` bound here.
384385
/// Create an event that passes no data, other than it occurred. Foregoes using a closure,
385386
/// in favor of pointing to a message directly.
386387
pub fn simple_ev<Ms: Clone, T>(trigger: T, message: Ms) -> Listener<Ms>
387388
where
388-
Ms: Clone + 'static,
389+
Ms: 'static,
389390
T: ToString + Copy,
390391
{
391-
let msg_closure = message.clone();
392-
let handler = || msg_closure;
393-
let closure = move |_| handler.clone()();
392+
let stored_msg = message.clone();
393+
let closure = move |_| message.clone();
394394
Listener::new(
395395
&trigger.to_string(),
396396
Some(Box::new(closure)),
397397
Some(Category::Simple),
398-
Some(message),
398+
Some(stored_msg),
399399
)
400400
}
401401

402402
/// Create an event that passes a `web_sys::CustomEvent`, allowing easy access
403403
/// to detail() and then trigger update
404404
#[deprecated]
405-
pub fn trigger_update_ev<Ms: Clone>(
405+
pub fn trigger_update_ev<Ms>(
406406
handler: impl FnOnce(web_sys::CustomEvent) -> Ms + 'static + Clone,
407407
) -> Listener<Ms> {
408408
let closure = move |event: web_sys::Event| {
@@ -431,7 +431,7 @@ pub(crate) fn fmt_hook_fn<T>(h: &Option<T>) -> &'static str {
431431

432432
/// Trigger update function from outside of App
433433
#[deprecated]
434-
pub fn trigger_update_handler<Ms: Clone + DeserializeOwned>() -> Listener<Ms> {
434+
pub fn trigger_update_handler<Ms: DeserializeOwned>() -> Listener<Ms> {
435435
trigger_update_ev(|ev| {
436436
ev.detail()
437437
.into_serde()

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ mod websys_bridge;
3333

3434
/// Create an element flagged in a way that it will not be rendered. Useful
3535
/// in ternary operations.
36-
pub fn empty<Ms: Clone>() -> dom_types::Node<Ms> {
36+
pub const fn empty<Ms>() -> dom_types::Node<Ms> {
3737
dom_types::Node::Empty
3838
}
3939

@@ -133,7 +133,6 @@ pub mod tests {
133133
}
134134
}
135135

136-
#[derive(Clone)]
137136
enum Msg {
138137
Increment,
139138
}

0 commit comments

Comments
 (0)