fixed Box<dyn Fn> option fields (mostly in default-struct-builder)

This commit is contained in:
Maccesch 2023-07-12 20:46:25 +01:00
parent ef0989cbdd
commit 864a5d3be9
12 changed files with 225 additions and 177 deletions

View file

@ -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 }

View file

@ -454,7 +454,6 @@ pub struct UseStorageOptions<T> {
/// 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<dyn CloneableFnWithArg<UseStorageError>>,
/// Debounce or throttle the writing to storage whenever the value changes.

View file

@ -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<dyn CloneableFnWithArg<UseColorModeOnChangeArgs>>,
/// When provided, `useStorage` will be skipped.

View file

@ -92,7 +92,6 @@ pub struct UseIntervalOptions {
immediate: bool,
/// Callback on every interval.
#[builder(into)]
callback: Box<dyn CloneableFnWithArg<u64>>,
}

View file

@ -456,12 +456,10 @@ pub struct UseScrollOptions {
offset: ScrollOffset,
/// Callback when scrolling is happening.
#[builder(into)]
on_scroll: Box<dyn CloneableFnWithArg<web_sys::Event>>,
on_scroll: Box<dyn CloneableFnWithArg<web_sys::Event> + 'static>,
/// Callback when scrolling stops (after `idle` + `throttle` milliseconds have passed).
#[builder(into)]
on_stop: Box<dyn CloneableFnWithArg<web_sys::Event>>,
on_stop: Box<dyn CloneableFnWithArg<web_sys::Event> + 'static>,
/// Options passed to the `addEventListener("scroll", ...)` call
event_listener_options: web_sys::AddEventListenerOptions,

View file

@ -1,169 +0,0 @@
use std::fmt::Debug;
pub trait CloneableFnWithReturn<R>: FnOnce() -> R {
fn clone_box(&self) -> Box<dyn CloneableFnWithReturn<R>>;
}
impl<F, R> CloneableFnWithReturn<R> for F
where
F: FnOnce() -> R + Clone + 'static,
R: 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFnWithReturn<R>> {
Box::new(self.clone())
}
}
impl<R> Clone for Box<dyn CloneableFnWithReturn<R>> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl<R: Default + 'static> Default for Box<dyn CloneableFnWithReturn<R>> {
fn default() -> Self {
Box::new(|| Default::default())
}
}
pub trait CloneableFnWithArgAndReturn<Arg, R>: FnOnce(Arg) -> R {
fn clone_box(&self) -> Box<dyn CloneableFnWithArgAndReturn<Arg, R>>;
}
impl<F, R, Arg> CloneableFnWithArgAndReturn<Arg, R> for F
where
F: FnMut(Arg) -> R + Clone + 'static,
R: 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFnWithArgAndReturn<Arg, R>> {
Box::new(self.clone())
}
}
impl<R, Arg> Clone for Box<dyn CloneableFnWithArgAndReturn<Arg, R>> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl<R: Default + 'static, Arg> Default for Box<dyn CloneableFnWithArgAndReturn<Arg, R>> {
fn default() -> Self {
Box::new(|_| Default::default())
}
}
pub trait CloneableFn: FnOnce() {
fn clone_box(&self) -> Box<dyn CloneableFn>;
}
impl<F> CloneableFn for F
where
F: FnOnce() + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFn> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn CloneableFn> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl Default for Box<dyn CloneableFn> {
fn default() -> Self {
Box::new(|| {})
}
}
pub trait CloneableFnWithArg<Arg>: FnOnce(Arg) {
fn clone_box(&self) -> Box<dyn CloneableFnWithArg<Arg>>;
}
impl<F, Arg> CloneableFnWithArg<Arg> for F
where
F: FnMut(Arg) + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFnWithArg<Arg>> {
Box::new(self.clone())
}
}
impl<Arg> Clone for Box<dyn CloneableFnWithArg<Arg>> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl<Arg> Default for Box<dyn CloneableFnWithArg<Arg>> {
fn default() -> Self {
Box::new(|_| {})
}
}
pub trait CloneableFnMutWithArg<Arg>: FnMut(Arg) {
fn clone_box(&self) -> Box<dyn CloneableFnMutWithArg<Arg>>;
}
impl<F, Arg> CloneableFnMutWithArg<Arg> for F
where
F: FnMut(Arg) + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFnMutWithArg<Arg>> {
Box::new(self.clone())
}
}
impl<Arg> Clone for Box<dyn CloneableFnMutWithArg<Arg>> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl<Arg> Default for Box<dyn CloneableFnMutWithArg<Arg>> {
fn default() -> Self {
Box::new(|_| {})
}
}
impl<Arg> Debug for Box<dyn CloneableFnMutWithArg<Arg>> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Box<dyn CloneableFnMutWithArg<{}>>",
std::any::type_name::<Arg>()
)
}
}
pub trait CloneableFnMut: FnMut() {
fn clone_box(&self) -> Box<dyn CloneableFnMut>;
}
impl<F> CloneableFnMut for F
where
F: FnMut() + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFnMut> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn CloneableFnMut> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl Default for Box<dyn CloneableFnMut> {
fn default() -> Self {
Box::new(|| {})
}
}
impl Debug for Box<dyn CloneableFnMut> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Box<dyn CloneableFnMut>",)
}
}

View file

@ -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<dyn CloneableFn>;
}
impl<F> CloneableFn for F
where
F: FnOnce() + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFn> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn CloneableFn> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl Default for Box<dyn CloneableFn> {
fn default() -> Self {
Box::new(|| {})
}
}
impl Debug for Box<dyn CloneableFn> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Box<dyn CloneableFn>",)
}
}

View file

@ -0,0 +1,32 @@
use std::fmt::Debug;
pub trait CloneableFnMut: FnMut() {
fn clone_box(&self) -> Box<dyn CloneableFnMut>;
}
impl<F> CloneableFnMut for F
where
F: FnMut() + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFnMut> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn CloneableFnMut> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl Default for Box<dyn CloneableFnMut> {
fn default() -> Self {
Box::new(|| {})
}
}
impl Debug for Box<dyn CloneableFnMut> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Box<dyn CloneableFnMut>",)
}
}

View file

@ -0,0 +1,36 @@
use std::fmt::Debug;
pub trait CloneableFnMutWithArg<Arg>: FnMut(Arg) {
fn clone_box(&self) -> Box<dyn CloneableFnMutWithArg<Arg>>;
}
impl<F, Arg> CloneableFnMutWithArg<Arg> for F
where
F: FnMut(Arg) + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFnMutWithArg<Arg>> {
Box::new(self.clone())
}
}
impl<Arg> Clone for Box<dyn CloneableFnMutWithArg<Arg>> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl<Arg> Default for Box<dyn CloneableFnMutWithArg<Arg>> {
fn default() -> Self {
Box::new(|_| {})
}
}
impl<Arg> Debug for Box<dyn CloneableFnMutWithArg<Arg>> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Box<dyn CloneableFnMutWithArg<{}>>",
std::any::type_name::<Arg>()
)
}
}

View file

@ -0,0 +1,36 @@
use std::fmt::Debug;
pub trait CloneableFnWithArg<Arg>: FnOnce(Arg) {
fn clone_box(&self) -> Box<dyn CloneableFnWithArg<Arg>>;
}
impl<F, Arg> CloneableFnWithArg<Arg> for F
where
F: FnOnce(Arg) + Clone + 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFnWithArg<Arg>> {
Box::new(self.clone())
}
}
impl<Arg> Clone for Box<dyn CloneableFnWithArg<Arg>> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl<Arg> Default for Box<dyn CloneableFnWithArg<Arg>> {
fn default() -> Self {
Box::new(|_| {})
}
}
impl<Arg> Debug for Box<dyn CloneableFnWithArg<Arg>> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Box<dyn CloneableFnWithArg<{}>>",
std::any::type_name::<Arg>()
)
}
}

View file

@ -0,0 +1,38 @@
use std::fmt::Debug;
pub trait CloneableFnWithArgAndReturn<Arg, R>: FnOnce(Arg) -> R {
fn clone_box(&self) -> Box<dyn CloneableFnWithArgAndReturn<Arg, R>>;
}
impl<F, R, Arg> CloneableFnWithArgAndReturn<Arg, R> for F
where
F: FnMut(Arg) -> R + Clone + 'static,
R: 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFnWithArgAndReturn<Arg, R>> {
Box::new(self.clone())
}
}
impl<R, Arg> Clone for Box<dyn CloneableFnWithArgAndReturn<Arg, R>> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl<R: Default + 'static, Arg> Default for Box<dyn CloneableFnWithArgAndReturn<Arg, R>> {
fn default() -> Self {
Box::new(|_| Default::default())
}
}
impl<R, Arg> Debug for Box<dyn CloneableFnWithArgAndReturn<Arg, R>> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Box<dyn CloneableFnWithArgAndReturn<{}, {}>>",
std::any::type_name::<Arg>(),
std::any::type_name::<R>()
)
}
}

View file

@ -0,0 +1,37 @@
use std::fmt::Debug;
pub trait CloneableFnWithReturn<R>: FnOnce() -> R {
fn clone_box(&self) -> Box<dyn CloneableFnWithReturn<R>>;
}
impl<F, R> CloneableFnWithReturn<R> for F
where
F: FnOnce() -> R + Clone + 'static,
R: 'static,
{
fn clone_box(&self) -> Box<dyn CloneableFnWithReturn<R>> {
Box::new(self.clone())
}
}
impl<R> Clone for Box<dyn CloneableFnWithReturn<R>> {
fn clone(&self) -> Self {
(**self).clone_box()
}
}
impl<R: Default + 'static> Default for Box<dyn CloneableFnWithReturn<R>> {
fn default() -> Self {
Box::new(|| Default::default())
}
}
impl<R> Debug for Box<dyn CloneableFnWithReturn<R>> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Box<dyn CloneableFnWithReturn<{}>>",
std::any::type_name::<R>()
)
}
}