feat: add .into_X_boxed() for classes, properties, and styles as for attributes

This commit is contained in:
jquesada2016 2023-09-03 19:18:49 -05:00 committed by GitHub
parent 006ca13797
commit 716b9fb50b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 1 deletions

View file

@ -105,6 +105,7 @@ impl std::fmt::Debug for Attribute {
pub trait IntoAttribute {
/// Converts the object into an [`Attribute`].
fn into_attribute(self) -> Attribute;
/// Helper function for dealing with `Box<dyn IntoAttribute>`.
fn into_attribute_boxed(self: Box<Self>) -> Attribute;
}

View file

@ -21,6 +21,9 @@ pub enum Class {
pub trait IntoClass {
/// Converts the object into a [`Class`].
fn into_class(self) -> Class;
/// Helper function for dealing with `Box<dyn IntoClass>`.
fn into_class_boxed(self: Box<Self>) -> Class;
}
impl IntoClass for bool {
@ -28,6 +31,10 @@ impl IntoClass for bool {
fn into_class(self) -> Class {
Class::Value(self)
}
fn into_class_boxed(self: Box<Self>) -> Class {
(*self).into_class()
}
}
impl<T> IntoClass for T
@ -39,6 +46,10 @@ where
let modified_fn = Box::new(self);
Class::Fn(modified_fn)
}
fn into_class_boxed(self: Box<Self>) -> Class {
(*self).into_class()
}
}
impl Class {
@ -128,6 +139,10 @@ macro_rules! class_signal_type {
let modified_fn = Box::new(move || self.get());
Class::Fn(modified_fn)
}
fn into_class_boxed(self: Box<Self>) -> Class {
(*self).into_class()
}
}
};
}
@ -141,6 +156,10 @@ macro_rules! class_signal_type_optional {
let modified_fn = Box::new(move || self.get().unwrap_or(false));
Class::Fn(modified_fn)
}
fn into_class_boxed(self: Box<Self>) -> Class {
(*self).into_class()
}
}
};
}

View file

@ -15,7 +15,7 @@ use wasm_bindgen::UnwrapThrowExt;
pub enum Property {
/// A static JavaScript value.
Value(JsValue),
/// A (presumably reactive) function, which will be run inside an effect to toggle the class.
/// A (presumably reactive) function, which will be run inside an effect to update the property.
Fn(Box<dyn Fn() -> JsValue>),
}
@ -25,6 +25,9 @@ pub enum Property {
pub trait IntoProperty {
/// Converts the object into a [`Property`].
fn into_property(self) -> Property;
/// Helper function for dealing with `Box<dyn IntoProperty>`.
fn into_property_boxed(self: Box<Self>) -> Property;
}
impl<T, U> IntoProperty for T
@ -36,6 +39,10 @@ where
let modified_fn = Box::new(move || self().into());
Property::Fn(modified_fn)
}
fn into_property_boxed(self: Box<Self>) -> Property {
(*self).into_property()
}
}
macro_rules! prop_type {
@ -45,6 +52,10 @@ macro_rules! prop_type {
fn into_property(self) -> Property {
Property::Value(self.into())
}
fn into_property_boxed(self: Box<Self>) -> Property {
(*self).into_property()
}
}
impl IntoProperty for Option<$prop_type> {
@ -52,6 +63,10 @@ macro_rules! prop_type {
fn into_property(self) -> Property {
Property::Value(self.into())
}
fn into_property_boxed(self: Box<Self>) -> Property {
(*self).into_property()
}
}
};
}
@ -67,6 +82,10 @@ macro_rules! prop_signal_type {
let modified_fn = Box::new(move || self.get().into());
Property::Fn(modified_fn)
}
fn into_property_boxed(self: Box<Self>) -> Property {
(*self).into_property()
}
}
};
}
@ -83,6 +102,10 @@ macro_rules! prop_signal_type_optional {
let modified_fn = Box::new(move || self.get().into());
Property::Fn(modified_fn)
}
fn into_property_boxed(self: Box<Self>) -> Property {
(*self).into_property()
}
}
};
}

View file

@ -41,6 +41,9 @@ impl std::fmt::Debug for Style {
pub trait IntoStyle {
/// Converts the object into a [`Style`].
fn into_style(self) -> Style;
/// Helper function for dealing with `Box<dyn IntoStyle>`.
fn into_style_boxed(self: Box<Self>) -> Style;
}
impl IntoStyle for &'static str {
@ -48,6 +51,10 @@ impl IntoStyle for &'static str {
fn into_style(self) -> Style {
Style::Value(Oco::Borrowed(self))
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
impl IntoStyle for String {
@ -55,6 +62,10 @@ impl IntoStyle for String {
fn into_style(self) -> Style {
Style::Value(Oco::Owned(self))
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
impl IntoStyle for Rc<str> {
@ -62,6 +73,10 @@ impl IntoStyle for Rc<str> {
fn into_style(self) -> Style {
Style::Value(Oco::Counted(self))
}
fn into_style_boxed(self: Box<Self>) -> Style {
(self).into_style()
}
}
impl IntoStyle for Cow<'static, str> {
@ -69,6 +84,10 @@ impl IntoStyle for Cow<'static, str> {
fn into_style(self) -> Style {
Style::Value(self.into())
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
impl IntoStyle for Oco<'static, str> {
@ -76,6 +95,10 @@ impl IntoStyle for Oco<'static, str> {
fn into_style(self) -> Style {
Style::Value(self)
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
impl IntoStyle for Option<&'static str> {
@ -83,6 +106,10 @@ impl IntoStyle for Option<&'static str> {
fn into_style(self) -> Style {
Style::Option(self.map(Oco::Borrowed))
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
impl IntoStyle for Option<String> {
@ -90,6 +117,10 @@ impl IntoStyle for Option<String> {
fn into_style(self) -> Style {
Style::Option(self.map(Oco::Owned))
}
fn into_style_boxed(self: Box<Self>) -> Style {
(self).into_style()
}
}
impl IntoStyle for Option<Rc<str>> {
@ -97,6 +128,10 @@ impl IntoStyle for Option<Rc<str>> {
fn into_style(self) -> Style {
Style::Option(self.map(Oco::Counted))
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
impl IntoStyle for Option<Cow<'static, str>> {
@ -104,6 +139,10 @@ impl IntoStyle for Option<Cow<'static, str>> {
fn into_style(self) -> Style {
Style::Option(self.map(Oco::from))
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
impl IntoStyle for Option<Oco<'static, str>> {
@ -111,6 +150,10 @@ impl IntoStyle for Option<Oco<'static, str>> {
fn into_style(self) -> Style {
Style::Option(self)
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
impl<T, U> IntoStyle for T
@ -123,6 +166,10 @@ where
let modified_fn = Rc::new(move || (self)().into_style());
Style::Fn(modified_fn)
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
impl Style {
@ -221,12 +268,20 @@ macro_rules! style_type {
fn into_style(self) -> Style {
Style::Value(self.to_string().into())
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
impl IntoStyle for Option<$style_type> {
fn into_style(self) -> Style {
Style::Option(self.map(|n| n.to_string().into()))
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
};
}
@ -242,6 +297,10 @@ macro_rules! style_signal_type {
let modified_fn = Rc::new(move || self.get().into_style());
Style::Fn(modified_fn)
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
};
}
@ -258,6 +317,10 @@ macro_rules! style_signal_type_optional {
let modified_fn = Rc::new(move || self.get().into_style());
Style::Fn(modified_fn)
}
fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
};
}