Feat: push new component API and context API

This commit is contained in:
Jonathan Kelley committed 2021-02-11 23:03:01 -05:00
1 parent 8329268d39
commit 125f5426a4
14 files changed
+432 -264

No files matched your search

+28 -29
View File
@@ -1,9 +1,11 @@
use crate::context::hooks::Hook;
use crate::inner::*;
use crate::nodes::VNode;
use crate::prelude::*;
use bumpalo::Bump;
use generational_arena::Index;
use std::{
any::TypeId, cell::RefCell, future::Future, marker::PhantomData, sync::atomic::AtomicUsize,
any::TypeId, borrow::Borrow, cell::RefCell, future::Future, marker::PhantomData,
sync::atomic::AtomicUsize,
};
/// Every component in Dioxus is represented by a `Scope`.
@@ -48,13 +50,15 @@ impl Scope {
pub fn create_context<'runner, T: Properties>(
&'runner mut self,
components: &'runner generational_arena::Arena<Scope>,
) -> Context<T> {
props: &'runner T,
) -> Context {
// ) -> Context<T> {
Context {
_p: PhantomData {},
arena: &self.hook_arena,
hooks: &self.hooks,
idx: 0.into(),
props: T::new(),
// props,
components,
}
}
@@ -62,32 +66,27 @@ impl Scope {
/// Create a new context and run the component with references from the Virtual Dom
/// This function downcasts the function pointer based on the stored props_type
fn run<T: 'static>(&self, f: FC<T>) {}
fn call<'a, T: Properties + 'static>(&'a mut self, val: T) {
if self.props_type == TypeId::of::<T>() {
/*
SAFETY ALERT
This particular usage of transmute is outlined in its docs https://doc.rust-lang.org/std/mem/fn.transmute.html
We hide the generic bound on the function item by casting it to raw pointer. When the function is actually called,
we transmute the function back using the props as reference.
This is safe because we check that the generic type matches before casting.
*/
let caller = unsafe { std::mem::transmute::<*const i32, FC<T>>(self.caller) };
// let ctx = self.create_context::<T>();
// // TODO: do something with these nodes
// let nodes = caller(ctx);
} else {
panic!("Do not try to use `call` on Scopes with the wrong props type")
}
}
}
pub struct Hook(pub Box<dyn std::any::Any>);
mod bad_unsafety {
// todo
// fn call<'a, T: Properties + 'static>(&'a mut self, val: T) {
// if self.props_type == TypeId::of::<T>() {
// /*
// SAFETY ALERT
impl Hook {
pub fn new(state: Box<dyn std::any::Any>) -> Self {
Self(state)
}
// This particular usage of transmute is outlined in its docs https://doc.rust-lang.org/std/mem/fn.transmute.html
// We hide the generic bound on the function item by casting it to raw pointer. When the function is actually called,
// we transmute the function back using the props as reference.
// This is safe because we check that the generic type matches before casting.
// */
// let caller = unsafe { std::mem::transmute::<*const i32, FC<T>>(self.caller) };
// // let ctx = self.create_context::<T>();
// // // TODO: do something with these nodes
// // let nodes = caller(ctx);
// } else {
// panic!("Do not try to use `call` on Scopes with the wrong props type")
// }
// }
}