feat: massage lifetimes
This commit is contained in:
1 parent
16dbf4a6f8
commit
9726a065b0
92 files changed
+250
-241
No files matched your search
@@ -7,6 +7,40 @@
|
||||
|
||||
use crate::innerlude::{Context, DomTree, LazyNodes, FC};
|
||||
|
||||
/// A component is a wrapper around a Context and some Props that share a lifetime
|
||||
///
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// With memoized state:
|
||||
/// ```rust
|
||||
/// struct State {}
|
||||
///
|
||||
/// fn Example((cx, props): Component<State>) -> DomTree {
|
||||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// With borrowed state:
|
||||
/// ```rust
|
||||
/// struct State<'a> {
|
||||
/// name: &'a str
|
||||
/// }
|
||||
///
|
||||
/// fn Example<'a>((cx, props): Component<'a, State>) -> DomTree<'a> {
|
||||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// With owned state as a closure:
|
||||
/// ```rust
|
||||
/// static Example: FC<()> = |(cx, props)| {
|
||||
/// // ...
|
||||
/// };
|
||||
/// ```
|
||||
///
|
||||
pub type Component<'a, T> = (Context<'a>, &'a T);
|
||||
|
||||
/// Create inline fragments using Component syntax.
|
||||
///
|
||||
/// Fragments capture a series of children without rendering extra nodes.
|
||||
@@ -29,7 +63,7 @@ use crate::innerlude::{Context, DomTree, LazyNodes, FC};
|
||||
/// You want to use this free-function when your fragment needs a key and simply returning multiple nodes from rsx! won't cut it.
|
||||
///
|
||||
#[allow(non_upper_case_globals, non_snake_case)]
|
||||
pub fn Fragment<'a>(cx: Context<'a>, _: &'a ()) -> DomTree<'a> {
|
||||
pub fn Fragment((cx, _): Component<()>) -> DomTree {
|
||||
cx.render(LazyNodes::new(|f| f.fragment_from_iter(cx.children())))
|
||||
}
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ impl<'src> Context<'src> {
|
||||
/// ## Example
|
||||
///
|
||||
/// ```rust
|
||||
/// const App: FC<()> = |cx, props|{
|
||||
/// const App: FC<()> = |(cx, props)|{
|
||||
/// cx.render(rsx!{
|
||||
/// CustomCard {
|
||||
/// h1 {}
|
||||
@@ -78,7 +78,7 @@ impl<'src> Context<'src> {
|
||||
/// })
|
||||
/// }
|
||||
///
|
||||
/// const CustomCard: FC<()> = |cx, props|{
|
||||
/// const CustomCard: FC<()> = |(cx, props)|{
|
||||
/// cx.render(rsx!{
|
||||
/// div {
|
||||
/// h1 {"Title card"}
|
||||
@@ -181,12 +181,12 @@ impl<'src> Context<'src> {
|
||||
/// ```
|
||||
/// struct SharedState(&'static str);
|
||||
///
|
||||
/// static App: FC<()> = |cx, props|{
|
||||
/// static App: FC<()> = |(cx, props)|{
|
||||
/// cx.provide_state(SharedState("world"));
|
||||
/// rsx!(cx, Child {})
|
||||
/// }
|
||||
///
|
||||
/// static Child: FC<()> = |cx, props|{
|
||||
/// static Child: FC<()> = |(cx, props)|{
|
||||
/// let state = cx.consume_state::<SharedState>();
|
||||
/// rsx!(cx, div { "hello {state.0}" })
|
||||
/// }
|
||||
@@ -221,7 +221,7 @@ impl<'src> Context<'src> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// static App: FC<()> = |cx, props| {
|
||||
/// static App: FC<()> = |(cx, props)| {
|
||||
/// todo!();
|
||||
/// rsx!(cx, div { "Subtree {id}"})
|
||||
/// };
|
||||
@@ -238,7 +238,7 @@ impl<'src> Context<'src> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// static App: FC<()> = |cx, props| {
|
||||
/// static App: FC<()> = |(cx, props)| {
|
||||
/// let id = cx.get_current_subtree();
|
||||
/// rsx!(cx, div { "Subtree {id}"})
|
||||
/// };
|
||||
|
||||
@@ -18,7 +18,7 @@ use std::{any::Any, cell::RefCell, future::Future, ops::Deref, rc::Rc};
|
||||
/// Returns the handle to the task and the value (if it is ready, else None).
|
||||
///
|
||||
/// ```
|
||||
/// static Example: FC<()> = |cx, props| {
|
||||
/// static Example: FC<()> = |(cx, props)| {
|
||||
/// let (task, value) = use_task(|| async {
|
||||
/// timer::sleep(Duration::from_secs(1)).await;
|
||||
/// "Hello World"
|
||||
|
||||
@@ -57,7 +57,7 @@ pub(crate) mod innerlude {
|
||||
pub use crate::virtual_dom::*;
|
||||
|
||||
pub type DomTree<'a> = Option<VNode<'a>>;
|
||||
pub type FC<P> = for<'a> fn(Context<'a>, &'a P) -> DomTree<'a>;
|
||||
pub type FC<P> = for<'a> fn(Component<'a, P>) -> DomTree<'a>;
|
||||
}
|
||||
|
||||
pub use crate::innerlude::{
|
||||
@@ -67,7 +67,7 @@ pub use crate::innerlude::{
|
||||
};
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::component::{fc_to_builder, Fragment, Properties};
|
||||
pub use crate::component::{fc_to_builder, Component, Fragment, Properties};
|
||||
pub use crate::context::Context;
|
||||
pub use crate::hooks::*;
|
||||
pub use crate::innerlude::{DioxusElement, DomTree, LazyNodes, Mutations, NodeFactory, FC};
|
||||
|
||||
@@ -526,7 +526,7 @@ impl<'a> NodeFactory<'a> {
|
||||
let caller: &'a mut dyn for<'b> Fn(&'b Scope) -> DomTree<'b> =
|
||||
bump.alloc(move |scope: &Scope| -> DomTree {
|
||||
let props: &'_ P = unsafe { &*(raw_props as *const P) };
|
||||
let res = component(Context { scope }, props);
|
||||
let res = component((Context { scope }, props));
|
||||
unsafe { std::mem::transmute(res) }
|
||||
});
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ impl Scope {
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// let mut dom = VirtualDom::new(|cx, props|cx.render(rsx!{ div {} }));
|
||||
/// let mut dom = VirtualDom::new(|(cx, props)|cx.render(rsx!{ div {} }));
|
||||
/// dom.rebuild();
|
||||
///
|
||||
/// let base = dom.base_scope();
|
||||
@@ -82,7 +82,7 @@ impl Scope {
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// let mut dom = VirtualDom::new(|cx, props|cx.render(rsx!{ div {} }));
|
||||
/// let mut dom = VirtualDom::new(|(cx, props)|cx.render(rsx!{ div {} }));
|
||||
/// dom.rebuild();
|
||||
///
|
||||
/// let base = dom.base_scope();
|
||||
@@ -110,7 +110,7 @@ impl Scope {
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// let mut dom = VirtualDom::new(|cx, props|cx.render(rsx!{ div {} }));
|
||||
/// let mut dom = VirtualDom::new(|(cx, props)|cx.render(rsx!{ div {} }));
|
||||
/// dom.rebuild();
|
||||
///
|
||||
/// let base = dom.base_scope();
|
||||
@@ -130,7 +130,7 @@ impl Scope {
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// let mut dom = VirtualDom::new(|cx, props|cx.render(rsx!{ div {} }));
|
||||
/// let mut dom = VirtualDom::new(|(cx, props)|cx.render(rsx!{ div {} }));
|
||||
/// dom.rebuild();
|
||||
///
|
||||
/// let base = dom.base_scope();
|
||||
@@ -148,7 +148,7 @@ impl Scope {
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// let mut dom = VirtualDom::new(|cx, props|cx.render(rsx!{ div {} }));
|
||||
/// let mut dom = VirtualDom::new(|(cx, props)|cx.render(rsx!{ div {} }));
|
||||
/// dom.rebuild();
|
||||
/// let base = dom.base_scope();
|
||||
///
|
||||
|
||||
@@ -33,7 +33,7 @@ use std::{any::Any, rc::Rc};
|
||||
///
|
||||
/// Example
|
||||
/// ```rust
|
||||
/// static App: FC<()> = |cx, props|{
|
||||
/// static App: FC<()> = |(cx, props)|{
|
||||
/// cx.render(rsx!{
|
||||
/// div {
|
||||
/// "Hello World"
|
||||
@@ -145,7 +145,7 @@ impl VirtualDom {
|
||||
// Safety: this callback is only valid for the lifetime of the root props
|
||||
let root_caller: Rc<dyn Fn(&Scope) -> DomTree> = Rc::new(move |scope: &Scope| unsafe {
|
||||
let props = _p.downcast_ref::<P>().unwrap();
|
||||
std::mem::transmute(root(Context { scope }, props))
|
||||
std::mem::transmute(root((Context { scope }, props)))
|
||||
});
|
||||
|
||||
let scheduler = Scheduler::new(sender, receiver);
|
||||
@@ -196,7 +196,7 @@ impl VirtualDom {
|
||||
/// struct AppProps {
|
||||
/// route: &'static str
|
||||
/// }
|
||||
/// static App: FC<AppProps> = |cx, props|cx.render(rsx!{ "route is {cx.route}" });
|
||||
/// static App: FC<AppProps> = |(cx, props)|cx.render(rsx!{ "route is {cx.route}" });
|
||||
///
|
||||
/// let mut dom = VirtualDom::new_with_props(App, AppProps { route: "start" });
|
||||
///
|
||||
@@ -222,7 +222,7 @@ impl VirtualDom {
|
||||
let root_caller: Box<dyn Fn(&Scope) -> DomTree> =
|
||||
Box::new(move |scope: &Scope| unsafe {
|
||||
let props: &'_ P = &*(props_ptr as *const P);
|
||||
std::mem::transmute(root(Context { scope }, props))
|
||||
std::mem::transmute(root((Context { scope }, props)))
|
||||
});
|
||||
|
||||
root_scope.update_scope_dependencies(&root_caller, ScopeChildren(&[]));
|
||||
@@ -246,7 +246,7 @@ impl VirtualDom {
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// static App: FC<()> = |cx, props|cx.render(rsx!{ "hello world" });
|
||||
/// static App: FC<()> = |(cx, props)|cx.render(rsx!{ "hello world" });
|
||||
/// let mut dom = VirtualDom::new();
|
||||
/// let edits = dom.rebuild();
|
||||
///
|
||||
@@ -271,7 +271,7 @@ impl VirtualDom {
|
||||
/// value: Shared<&'static str>,
|
||||
/// }
|
||||
///
|
||||
/// static App: FC<AppProps> = |cx, props|{
|
||||
/// static App: FC<AppProps> = |(cx, props)|{
|
||||
/// let val = cx.value.borrow();
|
||||
/// cx.render(rsx! { div { "{val}" } })
|
||||
/// };
|
||||
@@ -333,7 +333,7 @@ impl VirtualDom {
|
||||
/// # Example
|
||||
///
|
||||
/// ```no_run
|
||||
/// static App: FC<()> = |cx, props|rsx!(cx, div {"hello"} );
|
||||
/// static App: FC<()> = |(cx, props)|rsx!(cx, div {"hello"} );
|
||||
/// let mut dom = VirtualDom::new(App);
|
||||
/// loop {
|
||||
/// let deadline = TimeoutFuture::from_ms(16);
|
||||
|
||||
Reference in new issue
Block a user