Feat: some docs

This commit is contained in:
Jonathan Kelley committed 2021-06-02 11:07:30 -04:00
1 parent 508c560320
commit 1919f88f03
10 files changed
+179 -182

No files matched your search

@@ -22,6 +22,7 @@ static Example: FC<()> = |ctx| {
//
rsx! { in ctx,
div {
h1 {"these are children nodes: "}
{nodes}
}
}
+5 -126
View File
@@ -14,7 +14,11 @@ static Header: FC<()> = |ctx| {
ctx.render(dioxus::prelude::LazyNodes::new(|nodectx| {
builder::ElementBuilder::new(nodectx, "div")
.child(VNode::Component(VComponent::new(Bottom, (), None)))
.child(VNode::Component(nodectx.bump().alloc(VComponent::new(
Bottom,
(),
None,
))))
.finish()
}))
};
@@ -36,128 +40,3 @@ fn Top(ctx: Context<()>) -> VNode {
</div>
})
}
struct Callback<T>(Box<T>);
// impl<O, T: Fn() -> O> From<T> for Callback<T> {
// fn from(_: T) -> Self {
// todo!()
// }
// }
impl<O, A> From<&dyn Fn(A) -> O> for Callback<&dyn Fn(A) -> O> {
fn from(_: &dyn Fn(A) -> O) -> Self {
todo!()
}
}
impl<O, A, B> From<&dyn Fn(A, B) -> O> for Callback<&dyn Fn(A, B) -> O> {
fn from(_: &dyn Fn(A, B) -> O) -> Self {
todo!()
}
}
// compile time reordering of arguments
// Allows for transparently calling
#[derive(Default)]
pub struct Args<A, B, C> {
pub a: CuOpt<A>,
pub b: CuOpt<B>,
pub c: CuOpt<C>,
}
pub enum CuOpt<T> {
Some(T),
None,
}
impl<T> Default for CuOpt<T> {
fn default() -> Self {
CuOpt::None
}
}
impl<T> CuOpt<T> {
fn unwrap(self) -> T {
match self {
CuOpt::Some(t) => t,
CuOpt::None => panic!(""),
}
}
}
trait IsMemo {
fn memo(&self, other: &Self) -> bool {
false
}
}
impl<T: PartialEq> IsMemo for CuOpt<T> {
fn memo(&self, other: &Self) -> bool {
self == other
}
}
impl<T: PartialEq> PartialEq for CuOpt<T> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(CuOpt::Some(a), CuOpt::Some(b)) => a == b,
(CuOpt::Some(_), CuOpt::None) => false,
(CuOpt::None, CuOpt::Some(_)) => false,
(CuOpt::None, CuOpt::None) => true,
}
}
}
impl<T> IsMemo for &CuOpt<T> {
fn memo(&self, other: &Self) -> bool {
false
}
}
// #[test]
#[test]
fn try_test() {
// test_poc()
}
// fn test_poc(ctx: Context) {
// let b = Bump::new();
// let h = Args {
// a: CuOpt::Some("ASD"),
// b: CuOpt::Some(123),
// c: CuOpt::Some(|| {}),
// // c: CuOpt::Some(b.alloc(|| {})),
// // c: CuOpt::Some(Box::new(|| {}) as Box<dyn Fn()>),
// };
// let h2 = Args {
// a: CuOpt::Some("ASD"),
// b: CuOpt::Some(123),
// c: CuOpt::Some(|| {}),
// // c: CuOpt::Some(b.alloc(|| {})),
// // c: CuOpt::Some(Box::new(|| {}) as Box<dyn Fn()>),
// // c: CuOpt::Some(Box::new(|| {}) as Box<dyn Fn()>),
// // c: CuOpt::Some(Box::new(|| {}) as Box<dyn Fn()>),
// };
// // dbg!((&h.a).memo((&&h2.a)));
// // dbg!((&h.b).memo((&&h2.b)));
// // dbg!((&h.c).memo((&&h2.c)));
// //
// // ctx: Context
// Top(ctx, &h.a.unwrap(), &h.b.unwrap(), &h.c.unwrap());
// }
// fn test_realzies() {
// let h = Args {
// a: CuOpt::Some("ASD"),
// b: CuOpt::Some(123),
// c: CuOpt::Some(|| {}),
// };
// let g = |ctx: Context| {
// //
// Top(ctx, &h.a.unwrap(), &h.b.unwrap(), &h.c.unwrap())
// };
// }
+5 -12
View File
@@ -486,14 +486,6 @@ where
}
}
// impl IntoIterator for VNode {
// type Item = VNode;
// type IntoIter = std::iter::Once<Self::Item>;
// fn into_iter(self) -> Self::IntoIter {
// std::iter::once(self)
// }
// }
impl<'a> IntoIterator for VNode<'a> {
type Item = VNode<'a>;
type IntoIter = std::iter::Once<Self::Item>;
@@ -665,8 +657,9 @@ pub fn virtual_child<'a, T: Properties>(
) -> VNode<'a> {
// currently concerned about if props have a custom drop implementation
// might override it with the props macro
VNode::Component(
ctx.bump()
.alloc(crate::nodes::VComponent::new(f, props, key)),
)
todo!()
// VNode::Component(
// ctx.bump()
// .alloc(crate::nodes::VComponent::new(f, props, key)),
// )
}
+27 -19
View File
@@ -235,7 +235,7 @@ pub struct VComponent<'src> {
pub children: &'src [VNode<'src>],
// a pointer into the bump arena (given by the 'src lifetime)
raw_props: Box<dyn Any>,
// raw_props: Box<dyn Any>,
// raw_props: *const (),
// a pointer to the raw fn typ
@@ -243,13 +243,24 @@ pub struct VComponent<'src> {
_p: PhantomData<&'src ()>,
}
unsafe fn transmogrify<'a>(p: impl Properties + 'a) -> impl Properties + 'static {
todo!()
}
impl<'a> VComponent<'a> {
// use the type parameter on props creation and move it into a portable context
// this lets us keep scope generic *and* downcast its props when we need to:
// - perform comparisons when diffing (memoization)
// TODO: lift the requirement that props need to be static
// we want them to borrow references... maybe force implementing a "to_static_unsafe" trait
pub fn new<P: Properties>(component: FC<P>, props: P, key: Option<&'a str>) -> Self {
pub fn new<P: Properties>(
component: FC<P>,
// props: bumpalo::boxed::Box<'a, P>,
props: P,
key: Option<&'a str>,
) -> Self {
// pub fn new<P: Properties + 'a>(component: FC<P>, props: P, key: Option<&'a str>) -> Self {
// let bad_props = unsafe { transmogrify(props) };
let caller_ref = component as *const ();
// let raw_props = props as *const P as *const ();
@@ -271,7 +282,19 @@ impl<'a> VComponent<'a> {
// }
// };
let caller: Rc<dyn Fn(&Scope) -> VNode + 'a> = Rc::new(move |scp| todo!());
// let prref: &'a P = props.as_ref();
let caller: Rc<dyn Fn(&Scope) -> VNode> = Rc::new(move |scope| {
//
// let props2 = bad_props;
// props.as_ref()
// let ctx = Context {
// props: prref,
// scope,
// };
todo!()
// component(ctx)
});
// let caller = Rc::new(create_closure(component, raw_props));
let key = match key {
@@ -283,7 +306,7 @@ impl<'a> VComponent<'a> {
key,
ass_scope: Rc::new(RefCell::new(None)),
user_fc: caller_ref,
raw_props: Box::new(props),
// raw_props: Box::new(props),
_p: PhantomData,
children: &[],
caller,
@@ -292,18 +315,3 @@ impl<'a> VComponent<'a> {
}
}
}
// fn create_closure<P: Properties>(
// component: FC<P>,
// raw_props: *const (),
// ) -> impl for<'r> Fn(&'r Scope) -> VNode<'r> {
// move |ctx| -> VNode {
// // cast back into the right lifetime
// let safe_props: &P = unsafe { &*(raw_props as *const P) };
// component(Context {
// props: safe_props,
// scope: ctx,
// })
// // component(ctx, safe_props)
// }
// }
+17 -13
View File
@@ -137,12 +137,11 @@ impl VirtualDom {
// Normally, a component would be passed as a child in the RSX macro which automatically produces OpaqueComponents
// Here, we need to make it manually, using an RC to force the Weak reference to stick around for the main scope.
let _root_caller: Rc<OpaqueComponent> = Rc::new(move |ctx| {
todo!()
// root(Context {
// props: &root_props,
// scope: ctx,
// })
let _root_caller: Rc<OpaqueComponent<'static>> = Rc::new(move |scope| {
// the lifetime of this closure is just as long as the lifetime on the scope reference
// this closure moves root props (which is static) into this closure
let props = unsafe { &*(&root_props as *const _) };
root(Context { props, scope })
});
// Create a weak reference to the OpaqueComponent for the root scope to use as its render function
@@ -618,18 +617,23 @@ impl Scope {
.ok_or(Error::FatalInternal("Failed to get caller"))?;
// Cast the caller ptr from static to one with our own reference
let new_head = unsafe {
std::mem::transmute::<&OpaqueComponent<'static>, &OpaqueComponent<'sel>>(
caller.as_ref(),
)
}(&self);
let c2: &OpaqueComponent<'static> = caller.as_ref();
let c3: &OpaqueComponent<'sel> = unsafe { std::mem::transmute(c2) };
todo!();
// self.frames.cur_frame_mut().head_node = new_head;
let unsafe_head = unsafe { self.own_vnodes(c3) };
self.frames.cur_frame_mut().head_node = unsafe_head;
Ok(())
}
// this is its own function so we can preciesly control how lifetimes flow
unsafe fn own_vnodes<'a>(&'a self, f: &OpaqueComponent<'a>) -> VNode<'static> {
let new_head: VNode<'a> = f(self);
let out: VNode<'static> = std::mem::transmute(new_head);
out
}
// A safe wrapper around calling listeners
// calling listeners will invalidate the list of listeners
// The listener list will be completely drained because the next frame will write over previous listeners