generated from tlibjs/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): removed WrapFn*, added AnyFn*
- Loading branch information
Showing
13 changed files
with
616 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>), | ||
} | ||
} | ||
} |
Oops, something went wrong.