Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callbacks: Manual Clone and Debug impl; Visibility fix #1703

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading