From 089a073913b47cf6966f14eec3cf3d02c30a7c01 Mon Sep 17 00:00:00 2001 From: Pauan Date: Thu, 27 May 2021 23:54:52 +0200 Subject: [PATCH] Adding in attr and prop aliases --- src/dom.rs | 111 ++++++++++++++++++++++++++++++++++++-------------- src/traits.rs | 38 +++++++++++++++-- 2 files changed, 115 insertions(+), 34 deletions(-) diff --git a/src/dom.rs b/src/dom.rs index f32ba19..ecab7a2 100644 --- a/src/dom.rs +++ b/src/dom.rs @@ -200,10 +200,10 @@ fn make_text_signal(callbacks: &mut Callbacks, value: B) -> Text let element = element.clone(); callbacks.after_remove(for_each(value, move |value| { - let value = value.as_str(); - - // TODO maybe this should intern ? - bindings::set_text(&element, value); + value.with_str(|value| { + // TODO maybe this should intern ? + bindings::set_text(&element, value); + }); })); } @@ -520,6 +520,12 @@ impl DomBuilder where A: Into { } impl DomBuilder where A: AsRef { + #[inline] + pub fn prop(self, name: B, value: C) -> Self where B: MultiStr, C: Into { + self.property(name, value) + } + + /// The same as the [`prop`](#method.prop) method. #[inline] pub fn property(self, name: B, value: C) -> Self where B: MultiStr, C: Into { set_property(&self.element, &name, value); @@ -541,6 +547,15 @@ impl DomBuilder where A: AsRef { })); } + #[inline] + pub fn prop_signal(self, name: B, value: D) -> Self + where B: MultiStr + 'static, + C: Into, + D: Signal + 'static { + self.property_signal(name, value) + } + + /// The same as the [`prop_signal`](#method.prop_signal) method. #[inline] pub fn property_signal(mut self, name: B, value: D) -> Self where B: MultiStr + 'static, @@ -573,19 +588,6 @@ impl DomBuilder where A: AsRef { } impl DomBuilder where A: AsRef { - #[inline] - pub fn child>(mut self, mut child: B) -> Self { - operations::insert_children_one(self.element.as_ref(), &mut self.callbacks, child.borrow_mut()); - self - } - - // TODO figure out how to make this owned rather than &mut - #[inline] - pub fn children, C: IntoIterator>(mut self, children: C) -> Self { - operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, children); - self - } - #[inline] pub fn text(self, value: &str) -> Self { // TODO should this intern ? @@ -604,10 +606,8 @@ impl DomBuilder where A: AsRef { } #[inline] - pub fn children_signal_vec(mut self, children: B) -> Self - where B: SignalVec + 'static { - - operations::insert_children_signal_vec(self.element.as_ref().clone(), &mut self.callbacks, children); + pub fn child>(mut self, mut child: B) -> Self { + operations::insert_children_one(self.element.as_ref(), &mut self.callbacks, child.borrow_mut()); self } @@ -618,6 +618,21 @@ impl DomBuilder where A: AsRef { operations::insert_child_signal(self.element.as_ref().clone(), &mut self.callbacks, child); self } + + // TODO figure out how to make this owned rather than &mut + #[inline] + pub fn children, C: IntoIterator>(mut self, children: C) -> Self { + operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, children); + self + } + + #[inline] + pub fn children_signal_vec(mut self, children: B) -> Self + where B: SignalVec + 'static { + + operations::insert_children_signal_vec(self.element.as_ref().clone(), &mut self.callbacks, children); + self + } } impl DomBuilder where A: AsRef { @@ -628,6 +643,12 @@ impl DomBuilder where A: AsRef { DomBuilder::new(shadow) } + #[inline] + pub fn attr(self, name: B, value: &str) -> Self where B: MultiStr { + self.attribute(name, value) + } + + /// The same as the [`attr`](#method.attr) method. #[inline] pub fn attribute(self, name: B, value: &str) -> Self where B: MultiStr { let element = self.element.as_ref(); @@ -641,6 +662,12 @@ impl DomBuilder where A: AsRef { self } + #[inline] + pub fn attr_ns(self, namespace: &str, name: B, value: &str) -> Self where B: MultiStr { + self.attribute_namespace(namespace, name, value) + } + + /// The same as the [`attr_ns`](#method.attr_ns) method. #[inline] pub fn attribute_namespace(self, namespace: &str, name: B, value: &str) -> Self where B: MultiStr { let element = self.element.as_ref(); @@ -690,11 +717,11 @@ impl DomBuilder where A: AsRef { set_option(self.element.as_ref().clone(), &mut self.callbacks, value, move |element, value| { match value { Some(value) => { - let value = value.as_str(); - - name.each(|name| { - // TODO should this intern the value ? - bindings::set_attribute(element, intern(name), &value); + value.with_str(|value| { + name.each(|name| { + // TODO should this intern the value ? + bindings::set_attribute(element, intern(name), &value); + }); }); }, None => { @@ -706,6 +733,16 @@ impl DomBuilder where A: AsRef { }); } + #[inline] + pub fn attr_signal(self, name: B, value: E) -> Self + where B: MultiStr + 'static, + C: AsStr, + D: OptionStr, + E: Signal + 'static { + self.attribute_signal(name, value) + } + + /// The same as the [`attr_signal`](#method.attr_signal) method. #[inline] pub fn attribute_signal(mut self, name: B, value: E) -> Self where B: MultiStr + 'static, @@ -731,11 +768,11 @@ impl DomBuilder where A: AsRef { set_option(self.element.as_ref().clone(), &mut self.callbacks, value, move |element, value| { match value { Some(value) => { - let value = value.as_str(); - - name.each(|name| { - // TODO should this intern the value ? - bindings::set_attribute_ns(element, &namespace, intern(name), &value); + value.with_str(|value| { + name.each(|name| { + // TODO should this intern the value ? + bindings::set_attribute_ns(element, &namespace, intern(name), &value); + }); }); }, None => { @@ -747,6 +784,16 @@ impl DomBuilder where A: AsRef { }); } + #[inline] + pub fn attr_ns_signal(self, namespace: &str, name: B, value: E) -> Self + where B: MultiStr + 'static, + C: AsStr, + D: OptionStr, + E: Signal + 'static { + self.attribute_namespace_signal(namespace, name, value) + } + + /// The same as the [`attr_ns_signal`](#method.attr_ns_signal) method. #[inline] pub fn attribute_namespace_signal(mut self, namespace: &str, name: B, value: E) -> Self where B: MultiStr + 'static, @@ -824,6 +871,7 @@ impl DomBuilder where A: AsRef { }); } + // TODO rename to scroll_x_signal ? #[inline] pub fn scroll_left_signal(mut self, signal: B) -> Self where B: Signal> + 'static { // TODO bindings function for this ? @@ -831,6 +879,7 @@ impl DomBuilder where A: AsRef { self } + // TODO rename to scroll_y_signal ? #[inline] pub fn scroll_top_signal(mut self, signal: B) -> Self where B: Signal> + 'static { // TODO bindings function for this ? diff --git a/src/traits.rs b/src/traits.rs index f90f1a6..49b8d9c 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -27,14 +27,26 @@ impl Mixin for F where F: FnOnce(A) -> A { // TODO figure out a way to implement this for all of AsRef / Borrow / etc. // TODO implementations for &String and &mut String pub trait AsStr { + #[deprecated(since = "0.5.18", note = "Use with_str instead")] fn as_str(&self) -> &str; + + fn with_str(&self, f: F) -> A where F: FnOnce(&str) -> A { + #[allow(deprecated)] + f(self.as_str()) + } } impl<'a, A> AsStr for &'a A where A: AsStr { #[inline] fn as_str(&self) -> &str { + #[allow(deprecated)] AsStr::as_str(*self) } + + #[inline] + fn with_str(&self, f: F) -> B where F: FnOnce(&str) -> B { + AsStr::with_str(*self, f) + } } impl AsStr for String { @@ -42,6 +54,11 @@ impl AsStr for String { fn as_str(&self) -> &str { self } + + #[inline] + fn with_str(&self, f: F) -> A where F: FnOnce(&str) -> A { + f(&self) + } } impl AsStr for str { @@ -49,6 +66,11 @@ impl AsStr for str { fn as_str(&self) -> &str { self } + + #[inline] + fn with_str(&self, f: F) -> A where F: FnOnce(&str) -> A { + f(self) + } } impl<'a> AsStr for &'a str { @@ -56,6 +78,11 @@ impl<'a> AsStr for &'a str { fn as_str(&self) -> &str { self } + + #[inline] + fn with_str(&self, f: F) -> A where F: FnOnce(&str) -> A { + f(self) + } } impl AsStr for RefFn where C: Fn(&A) -> &str { @@ -63,6 +90,11 @@ impl AsStr for RefFn where C: Fn(&A) -> &str { fn as_str(&self) -> &str { self.call_ref() } + + #[inline] + fn with_str(&self, f: F) -> B where F: FnOnce(&str) -> B { + f(self.call_ref()) + } } @@ -80,8 +112,8 @@ pub trait MultiStr { impl MultiStr for A where A: AsStr { #[inline] - fn find_map(&self, mut f: F) -> Option where F: FnMut(&str) -> Option { - f(self.as_str()) + fn find_map(&self, f: F) -> Option where F: FnMut(&str) -> Option { + self.with_str(f) } } @@ -106,7 +138,7 @@ macro_rules! array_multi_str { impl MultiStr for [A; $size] where A: AsStr { #[inline] fn find_map(&self, mut f: F) -> Option where F: FnMut(&str) -> Option { - self.iter().find_map(|x| f(x.as_str())) + self.iter().find_map(|x| x.with_str(|x| f(x))) } } };