From 864a5d3be983e480fcd123598381f384cf73f7b8 Mon Sep 17 00:00:00 2001 From: Maccesch Date: Wed, 12 Jul 2023 20:46:25 +0100 Subject: [PATCH] fixed Box option fields (mostly in default-struct-builder) --- Cargo.toml | 2 +- src/storage/use_storage.rs | 1 - src/use_color_mode.rs | 1 - src/use_interval.rs | 1 - src/use_scroll.rs | 6 +- src/utils/clonable_fn.rs | 169 ------------------- src/utils/clonable_fn/mod.rs | 43 +++++ src/utils/clonable_fn/mut_.rs | 32 ++++ src/utils/clonable_fn/mut_with_arg.rs | 36 ++++ src/utils/clonable_fn/with_arg.rs | 36 ++++ src/utils/clonable_fn/with_arg_and_return.rs | 38 +++++ src/utils/clonable_fn/with_return.rs | 37 ++++ 12 files changed, 225 insertions(+), 177 deletions(-) delete mode 100644 src/utils/clonable_fn.rs create mode 100644 src/utils/clonable_fn/mod.rs create mode 100644 src/utils/clonable_fn/mut_.rs create mode 100644 src/utils/clonable_fn/mut_with_arg.rs create mode 100644 src/utils/clonable_fn/with_arg.rs create mode 100644 src/utils/clonable_fn/with_arg_and_return.rs create mode 100644 src/utils/clonable_fn/with_return.rs diff --git a/Cargo.toml b/Cargo.toml index 1e77af5..80cc532 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ homepage = "https://leptos-use.rs" leptos = "0.4" wasm-bindgen = "0.2" js-sys = "0.3" -default-struct-builder = "0.3" +default-struct-builder = "0.4" num = { version = "0.4", optional = true } serde = { version = "1", optional = true } serde_json = { version = "1", optional = true } diff --git a/src/storage/use_storage.rs b/src/storage/use_storage.rs index 3806bb2..511275d 100644 --- a/src/storage/use_storage.rs +++ b/src/storage/use_storage.rs @@ -454,7 +454,6 @@ pub struct UseStorageOptions { /// Defaults to simply returning the stored value. pub(crate) merge_defaults: fn(&str, &T) -> String, /// Optional callback whenever an error occurs. The callback takes an argument of type [`UseStorageError`]. - #[builder(into)] pub(crate) on_error: Box>, /// Debounce or throttle the writing to storage whenever the value changes. diff --git a/src/use_color_mode.rs b/src/use_color_mode.rs index e94a9c0..bc595a0 100644 --- a/src/use_color_mode.rs +++ b/src/use_color_mode.rs @@ -379,7 +379,6 @@ where /// Custom handler that is called on updates. /// If specified this will override the default behavior. /// To get the default behaviour back you can call the provided `default_handler` function. - #[builder(into)] on_changed: Box>, /// When provided, `useStorage` will be skipped. diff --git a/src/use_interval.rs b/src/use_interval.rs index 1c5962e..dbcba47 100644 --- a/src/use_interval.rs +++ b/src/use_interval.rs @@ -92,7 +92,6 @@ pub struct UseIntervalOptions { immediate: bool, /// Callback on every interval. - #[builder(into)] callback: Box>, } diff --git a/src/use_scroll.rs b/src/use_scroll.rs index e22bff1..87b9346 100644 --- a/src/use_scroll.rs +++ b/src/use_scroll.rs @@ -456,12 +456,10 @@ pub struct UseScrollOptions { offset: ScrollOffset, /// Callback when scrolling is happening. - #[builder(into)] - on_scroll: Box>, + on_scroll: Box + 'static>, /// Callback when scrolling stops (after `idle` + `throttle` milliseconds have passed). - #[builder(into)] - on_stop: Box>, + on_stop: Box + 'static>, /// Options passed to the `addEventListener("scroll", ...)` call event_listener_options: web_sys::AddEventListenerOptions, diff --git a/src/utils/clonable_fn.rs b/src/utils/clonable_fn.rs deleted file mode 100644 index 6b9ba8c..0000000 --- a/src/utils/clonable_fn.rs +++ /dev/null @@ -1,169 +0,0 @@ -use std::fmt::Debug; - -pub trait CloneableFnWithReturn: FnOnce() -> R { - fn clone_box(&self) -> Box>; -} - -impl CloneableFnWithReturn for F -where - F: FnOnce() -> R + Clone + 'static, - R: 'static, -{ - fn clone_box(&self) -> Box> { - Box::new(self.clone()) - } -} - -impl Clone for Box> { - fn clone(&self) -> Self { - (**self).clone_box() - } -} - -impl Default for Box> { - fn default() -> Self { - Box::new(|| Default::default()) - } -} - -pub trait CloneableFnWithArgAndReturn: FnOnce(Arg) -> R { - fn clone_box(&self) -> Box>; -} - -impl CloneableFnWithArgAndReturn for F -where - F: FnMut(Arg) -> R + Clone + 'static, - R: 'static, -{ - fn clone_box(&self) -> Box> { - Box::new(self.clone()) - } -} - -impl Clone for Box> { - fn clone(&self) -> Self { - (**self).clone_box() - } -} - -impl Default for Box> { - fn default() -> Self { - Box::new(|_| Default::default()) - } -} - -pub trait CloneableFn: FnOnce() { - fn clone_box(&self) -> Box; -} - -impl CloneableFn for F -where - F: FnOnce() + Clone + 'static, -{ - fn clone_box(&self) -> Box { - Box::new(self.clone()) - } -} - -impl Clone for Box { - fn clone(&self) -> Self { - (**self).clone_box() - } -} - -impl Default for Box { - fn default() -> Self { - Box::new(|| {}) - } -} - -pub trait CloneableFnWithArg: FnOnce(Arg) { - fn clone_box(&self) -> Box>; -} - -impl CloneableFnWithArg for F -where - F: FnMut(Arg) + Clone + 'static, -{ - fn clone_box(&self) -> Box> { - Box::new(self.clone()) - } -} - -impl Clone for Box> { - fn clone(&self) -> Self { - (**self).clone_box() - } -} - -impl Default for Box> { - fn default() -> Self { - Box::new(|_| {}) - } -} - -pub trait CloneableFnMutWithArg: FnMut(Arg) { - fn clone_box(&self) -> Box>; -} - -impl CloneableFnMutWithArg for F -where - F: FnMut(Arg) + Clone + 'static, -{ - fn clone_box(&self) -> Box> { - Box::new(self.clone()) - } -} - -impl Clone for Box> { - fn clone(&self) -> Self { - (**self).clone_box() - } -} - -impl Default for Box> { - fn default() -> Self { - Box::new(|_| {}) - } -} - -impl Debug for Box> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "Box>", - std::any::type_name::() - ) - } -} - -pub trait CloneableFnMut: FnMut() { - fn clone_box(&self) -> Box; -} - -impl CloneableFnMut for F -where - F: FnMut() + Clone + 'static, -{ - fn clone_box(&self) -> Box { - Box::new(self.clone()) - } -} - -impl Clone for Box { - fn clone(&self) -> Self { - (**self).clone_box() - } -} - -impl Default for Box { - fn default() -> Self { - Box::new(|| {}) - } -} - -impl Debug for Box { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "Box",) - } -} diff --git a/src/utils/clonable_fn/mod.rs b/src/utils/clonable_fn/mod.rs new file mode 100644 index 0000000..7e05e40 --- /dev/null +++ b/src/utils/clonable_fn/mod.rs @@ -0,0 +1,43 @@ +mod mut_; +mod mut_with_arg; +mod with_arg; +mod with_arg_and_return; +mod with_return; + +pub use mut_::*; +pub use mut_with_arg::*; +use std::fmt::Debug; +pub use with_arg::*; +pub use with_arg_and_return::*; +pub use with_return::*; + +pub trait CloneableFn: FnOnce() { + fn clone_box(&self) -> Box; +} + +impl CloneableFn for F +where + F: FnOnce() + Clone + 'static, +{ + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } +} + +impl Clone for Box { + fn clone(&self) -> Self { + (**self).clone_box() + } +} + +impl Default for Box { + fn default() -> Self { + Box::new(|| {}) + } +} + +impl Debug for Box { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Box",) + } +} diff --git a/src/utils/clonable_fn/mut_.rs b/src/utils/clonable_fn/mut_.rs new file mode 100644 index 0000000..f2a16b2 --- /dev/null +++ b/src/utils/clonable_fn/mut_.rs @@ -0,0 +1,32 @@ +use std::fmt::Debug; + +pub trait CloneableFnMut: FnMut() { + fn clone_box(&self) -> Box; +} + +impl CloneableFnMut for F +where + F: FnMut() + Clone + 'static, +{ + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } +} + +impl Clone for Box { + fn clone(&self) -> Self { + (**self).clone_box() + } +} + +impl Default for Box { + fn default() -> Self { + Box::new(|| {}) + } +} + +impl Debug for Box { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Box",) + } +} diff --git a/src/utils/clonable_fn/mut_with_arg.rs b/src/utils/clonable_fn/mut_with_arg.rs new file mode 100644 index 0000000..3d8fb63 --- /dev/null +++ b/src/utils/clonable_fn/mut_with_arg.rs @@ -0,0 +1,36 @@ +use std::fmt::Debug; + +pub trait CloneableFnMutWithArg: FnMut(Arg) { + fn clone_box(&self) -> Box>; +} + +impl CloneableFnMutWithArg for F +where + F: FnMut(Arg) + Clone + 'static, +{ + fn clone_box(&self) -> Box> { + Box::new(self.clone()) + } +} + +impl Clone for Box> { + fn clone(&self) -> Self { + (**self).clone_box() + } +} + +impl Default for Box> { + fn default() -> Self { + Box::new(|_| {}) + } +} + +impl Debug for Box> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "Box>", + std::any::type_name::() + ) + } +} diff --git a/src/utils/clonable_fn/with_arg.rs b/src/utils/clonable_fn/with_arg.rs new file mode 100644 index 0000000..fedcf57 --- /dev/null +++ b/src/utils/clonable_fn/with_arg.rs @@ -0,0 +1,36 @@ +use std::fmt::Debug; + +pub trait CloneableFnWithArg: FnOnce(Arg) { + fn clone_box(&self) -> Box>; +} + +impl CloneableFnWithArg for F +where + F: FnOnce(Arg) + Clone + 'static, +{ + fn clone_box(&self) -> Box> { + Box::new(self.clone()) + } +} + +impl Clone for Box> { + fn clone(&self) -> Self { + (**self).clone_box() + } +} + +impl Default for Box> { + fn default() -> Self { + Box::new(|_| {}) + } +} + +impl Debug for Box> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "Box>", + std::any::type_name::() + ) + } +} diff --git a/src/utils/clonable_fn/with_arg_and_return.rs b/src/utils/clonable_fn/with_arg_and_return.rs new file mode 100644 index 0000000..b9a4122 --- /dev/null +++ b/src/utils/clonable_fn/with_arg_and_return.rs @@ -0,0 +1,38 @@ +use std::fmt::Debug; + +pub trait CloneableFnWithArgAndReturn: FnOnce(Arg) -> R { + fn clone_box(&self) -> Box>; +} + +impl CloneableFnWithArgAndReturn for F +where + F: FnMut(Arg) -> R + Clone + 'static, + R: 'static, +{ + fn clone_box(&self) -> Box> { + Box::new(self.clone()) + } +} + +impl Clone for Box> { + fn clone(&self) -> Self { + (**self).clone_box() + } +} + +impl Default for Box> { + fn default() -> Self { + Box::new(|_| Default::default()) + } +} + +impl Debug for Box> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "Box>", + std::any::type_name::(), + std::any::type_name::() + ) + } +} diff --git a/src/utils/clonable_fn/with_return.rs b/src/utils/clonable_fn/with_return.rs new file mode 100644 index 0000000..4deb656 --- /dev/null +++ b/src/utils/clonable_fn/with_return.rs @@ -0,0 +1,37 @@ +use std::fmt::Debug; + +pub trait CloneableFnWithReturn: FnOnce() -> R { + fn clone_box(&self) -> Box>; +} + +impl CloneableFnWithReturn for F +where + F: FnOnce() -> R + Clone + 'static, + R: 'static, +{ + fn clone_box(&self) -> Box> { + Box::new(self.clone()) + } +} + +impl Clone for Box> { + fn clone(&self) -> Self { + (**self).clone_box() + } +} + +impl Default for Box> { + fn default() -> Self { + Box::new(|| Default::default()) + } +} + +impl Debug for Box> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "Box>", + std::any::type_name::() + ) + } +}