Skip to content

Commit

Permalink
feat(react): removed WrapFn*, added AnyFn*
Browse files Browse the repository at this point in the history
  • Loading branch information
EqualMa committed Mar 1, 2022
1 parent 64755e7 commit ba76464
Show file tree
Hide file tree
Showing 13 changed files with 616 additions and 300 deletions.
4 changes: 2 additions & 2 deletions crates/react-html/src/macros/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ macro_rules! __impl_prop_default {
{ event_handler }
) => {
$($attr)*
fn $k <F: ?Sized, A> (mut self, event_handler: Option<react::WrapFn<F, A>>) -> Self
where react::WrapFn<F, A>: react::event::IntoJsEventHandler<$v_ty>
fn $k <F: ?Sized + react::DynFn> (mut self, event_handler: Option<react::AnyFn<F>>) -> Self
where react::AnyFn<F>: react::event::IntoJsEventHandler<$v_ty>
{
if let Some(event_handler) = event_handler {
let ret = react::event::IntoJsEventHandler::into_js_event_handler(event_handler);
Expand Down
125 changes: 125 additions & 0 deletions crates/react/src/any_fn_family/any_fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use std::{any::Any, rc::Rc};
use wasm_bindgen::closure::{Closure, WasmClosure};

use crate::{DynFn, FnOfArgs, IntoPropValue};

pub struct AnyFn<F>(pub Rc<F>)
where
F: ?Sized + DynFn;

impl<F: ?Sized + DynFn> AnyFn<F> {
#[inline]
pub fn inner(&self) -> &Rc<F> {
&self.0
}

#[inline]
pub fn into_inner(self) -> Rc<F> {
self.0
}

#[inline]
pub fn new<
TFunc: 'static + ?Sized + FnOfArgs<F::ArgsTuple, DynFn = F>,
RF: crate::IntoRc<TFunc>,
>(
func: RF,
) -> Self {
let func: Rc<TFunc> = func.into_rc();
let func = func.into_rc_dyn_fn();
Self(func)
}
}

impl<F: ?Sized + DynFn> std::ops::Deref for AnyFn<F> {
type Target = Rc<F>;

#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<F: ?Sized + DynFn> AsRef<Rc<F>> for AnyFn<F> {
#[inline]
fn as_ref(&self) -> &Rc<F> {
&self.0
}
}

impl<F: ?Sized + DynFn> Clone for AnyFn<F> {
#[inline]
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<F: ?Sized + DynFn> std::fmt::Debug for AnyFn<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "AnyFn<{}>", std::any::type_name::<F>())
}
}

impl<F: ?Sized + DynFn> crate::SafeIntoJsRuntime for AnyFn<F>
where
F: 'static + WasmClosure,
{
fn safe_into_js_runtime(self) -> crate::PassedToJsRuntime {
let boxed = self.0.rc_into_box_dyn_fn();

let closure = Closure::wrap(boxed);
let js_value = closure.as_ref().clone();
crate::PassedToJsRuntime {
js_value,
to_persist: Some(Box::new(closure) as Box<dyn Any>),
}
}
}

impl<F: ?Sized + DynFn, TFunc: 'static + ?Sized + FnOfArgs<F::ArgsTuple, DynFn = F>>
IntoPropValue<AnyFn<F>> for Rc<TFunc>
{
fn into_prop_value(self) -> AnyFn<F> {
AnyFn(self.into_rc_dyn_fn())
}
}

impl<F: ?Sized + DynFn, TFunc: 'static + ?Sized + FnOfArgs<F::ArgsTuple, DynFn = F>>
IntoPropValue<Option<AnyFn<F>>> for Rc<TFunc>
{
fn into_prop_value(self) -> Option<AnyFn<F>> {
Some(self.into_prop_value())
}
}

impl<F: ?Sized + DynFn, TFunc: 'static + ?Sized + FnOfArgs<F::ArgsTuple, DynFn = F>>
IntoPropValue<Option<AnyFn<F>>> for Option<Rc<TFunc>>
{
fn into_prop_value(self) -> Option<AnyFn<F>> {
self.map(IntoPropValue::into_prop_value)
}
}

impl<F: ?Sized + DynFn, TFunc: 'static + ?Sized + FnOfArgs<F::ArgsTuple, DynFn = F>>
IntoPropValue<AnyFn<F>> for &Rc<TFunc>
{
fn into_prop_value(self) -> AnyFn<F> {
AnyFn(Rc::clone(self).into_rc_dyn_fn())
}
}

impl<F: ?Sized + DynFn, TFunc: 'static + ?Sized + FnOfArgs<F::ArgsTuple, DynFn = F>>
IntoPropValue<Option<AnyFn<F>>> for &Rc<TFunc>
{
fn into_prop_value(self) -> Option<AnyFn<F>> {
Some(self.into_prop_value())
}
}

impl<F: ?Sized + DynFn, TFunc: 'static + ?Sized + FnOfArgs<F::ArgsTuple, DynFn = F>>
IntoPropValue<Option<AnyFn<F>>> for Option<&Rc<TFunc>>
{
fn into_prop_value(self) -> Option<AnyFn<F>> {
self.map(IntoPropValue::into_prop_value)
}
}
68 changes: 68 additions & 0 deletions crates/react/src/any_fn_family/any_fn_mut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::any::Any;
use wasm_bindgen::closure::{Closure, WasmClosure};

use crate::{DynFnMut, FnMutOfArgs};

pub struct AnyFnMut<F>(pub Box<F>)
where
F: ?Sized + DynFnMut;

impl<F: ?Sized + DynFnMut> AnyFnMut<F> {
#[inline]
pub fn inner(&self) -> &Box<F> {
&self.0
}

#[inline]
pub fn into_inner(self) -> Box<F> {
self.0
}

#[inline]
pub fn new<
TFunc: 'static + ?Sized + FnMutOfArgs<F::ArgsTuple, DynFnMut = F>,
BF: crate::IntoBoxed<TFunc>,
>(
func: BF,
) -> Self {
let func: Box<TFunc> = func.into_boxed();
let func = func.into_box_dyn_fn_mut();
Self(func)
}
}

impl<F: ?Sized + DynFnMut> std::ops::Deref for AnyFnMut<F> {
type Target = Box<F>;

#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<F: ?Sized + DynFnMut> AsRef<Box<F>> for AnyFnMut<F> {
#[inline]
fn as_ref(&self) -> &Box<F> {
&self.0
}
}

impl<F: ?Sized + DynFnMut> std::fmt::Debug for AnyFnMut<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "AnyFnMut<{}>", std::any::type_name::<F>())
}
}

impl<F: ?Sized + DynFnMut> crate::SafeIntoJsRuntime for AnyFnMut<F>
where
F: 'static + WasmClosure,
{
fn safe_into_js_runtime(self) -> crate::PassedToJsRuntime {
let closure = Closure::wrap(self.0);
let js_value = closure.as_ref().clone();
crate::PassedToJsRuntime {
js_value,
to_persist: Some(Box::new(closure) as Box<dyn Any>),
}
}
}
69 changes: 69 additions & 0 deletions crates/react/src/any_fn_family/any_fn_once.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::any::Any;
use wasm_bindgen::closure::{Closure, WasmClosureFnOnce};

use crate::{DynFnOnce, FnOnceOfArgs};

pub struct AnyFnOnce<F>(pub Box<F>)
where
F: ?Sized + DynFnOnce;

impl<F: ?Sized + DynFnOnce> AnyFnOnce<F> {
#[inline]
pub fn inner(&self) -> &Box<F> {
&self.0
}

#[inline]
pub fn into_inner(self) -> Box<F> {
self.0
}

#[inline]
pub fn new<
TFunc: 'static + ?Sized + FnOnceOfArgs<F::ArgsTuple, DynFnOnce = F>,
BF: crate::IntoBoxed<TFunc>,
>(
func: BF,
) -> Self {
let func: Box<TFunc> = func.into_boxed();
let func = func.into_box_dyn_fn_once();
Self(func)
}
}

impl<F: ?Sized + DynFnOnce> std::ops::Deref for AnyFnOnce<F> {
type Target = Box<F>;

#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<F: ?Sized + DynFnOnce> AsRef<Box<F>> for AnyFnOnce<F> {
#[inline]
fn as_ref(&self) -> &Box<F> {
&self.0
}
}

impl<F: ?Sized + DynFnOnce> std::fmt::Debug for AnyFnOnce<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "AnyFnOnce<{}>", std::any::type_name::<F>())
}
}

impl<F: ?Sized + DynFnOnce> crate::SafeIntoJsRuntime for AnyFnOnce<F>
where
F: 'static,
Box<F>: WasmClosureFnOnce<F::ArgsTuple, F::Output>,
{
fn safe_into_js_runtime(self) -> crate::PassedToJsRuntime {
let closure = Closure::once(self.0);
let js_value = closure.as_ref().clone();
crate::PassedToJsRuntime {
js_value,
to_persist: Some(Box::new(closure) as Box<dyn Any>),
}
}
}
Loading

0 comments on commit ba76464

Please sign in to comment.