Skip to content

Commit

Permalink
fix: manual Clone and Debug impl for Callbacks (#1703)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpotthast authored Sep 13, 2023
1 parent e384d53 commit dc1ba24
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions leptos_dom/src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
use crate::{AnyElement, ElementDescriptor, HtmlElement, IntoView, View};
use leptos_reactive::StoredValue;
use std::{rc::Rc, sync::Arc};
use std::{fmt, rc::Rc, sync::Arc};

/// A wrapper trait for calling callbacks.
pub trait Callable<In, Out = ()> {
Expand Down Expand Up @@ -100,9 +100,20 @@ pub trait Callable<In, Out = ()> {
/// # Cloning
/// See [StoredCallback]
#[derive(Clone)]
pub struct Callback<In, Out = ()>(Rc<dyn Fn(In) -> Out>);

impl<In> fmt::Debug for Callback<In> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
fmt.write_str("Callback")
}
}

impl<In> Clone for Callback<In> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<In, Out> Callback<In, Out> {
/// creates a new callback from the function or closure
pub fn new<F>(f: F) -> Callback<In, Out>
Expand Down Expand Up @@ -167,9 +178,20 @@ where
}

/// a callback type that is `Send` and `Sync` if the input type is
#[derive(Clone)]
pub struct SyncCallback<In, Out = ()>(Arc<dyn Fn(In) -> Out>);

impl<In> fmt::Debug for SyncCallback<In> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
fmt.write_str("SyncCallback")
}
}

impl<In> Clone for SyncCallback<In> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<In: 'static, Out: 'static> SyncCallback<In, Out> {
/// creates a new callback from the function or closure
pub fn new<F>(fun: F) -> Self
Expand Down Expand Up @@ -216,9 +238,20 @@ impl<In: 'static, Out: 'static> SyncCallback<In, Out> {
/// {render_number}
/// </div>
/// }
#[derive(Clone)]
pub struct HtmlCallback<In = ()>(Rc<dyn Fn(In) -> HtmlElement<AnyElement>>);

impl<In> fmt::Debug for HtmlCallback<In> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
fmt.write_str("HtmlCallback")
}
}

impl<In> Clone for HtmlCallback<In> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<In> HtmlCallback<In> {
/// creates a new callback from the function or closure
pub fn new<F, H>(f: F) -> Self
Expand Down Expand Up @@ -286,12 +319,23 @@ impl IntoView for HtmlCallback<()> {
/// {render_number}
/// </div>
/// }
#[derive(Clone)]
pub struct ViewCallback<In>(Rc<dyn Fn(In) -> View>);

impl<In> fmt::Debug for ViewCallback<In> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
fmt.write_str("ViewCallback")
}
}

impl<In> Clone for ViewCallback<In> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<In> ViewCallback<In> {
/// creates a new callback from the function or closure
fn new<F, V>(f: F) -> Self
pub fn new<F, V>(f: F) -> Self
where
F: Fn(In) -> V + 'static,
V: IntoView + 'static,
Expand Down

0 comments on commit dc1ba24

Please sign in to comment.