Feat: push new component API and context API
This commit is contained in:
1 parent
8329268d39
commit
125f5426a4
14 files changed
+432
-264
No files matched your search
@@ -1,9 +1,101 @@
|
||||
#![allow(unused, non_upper_case_globals)]
|
||||
use bumpalo::Bump;
|
||||
use dioxus_core::nodebuilder::*;
|
||||
use dioxus_core::prelude::VNode;
|
||||
use dioxus_core::prelude::*;
|
||||
use once_cell::sync::{Lazy, OnceCell};
|
||||
// #![allow(unused, non_upper_case_globals)]
|
||||
// use bumpalo::Bump;
|
||||
// use dioxus_core::nodebuilder::*;
|
||||
// use dioxus_core::prelude::VNode;
|
||||
// use dioxus_core::prelude::*;
|
||||
// use once_cell::sync::{Lazy, OnceCell};
|
||||
use std::{collections::HashMap, future::Future, marker::PhantomData};
|
||||
|
||||
use std::ops::Deref;
|
||||
|
||||
/*
|
||||
A guard over underlying T that provides access in callbacks via "Copy"
|
||||
*/
|
||||
|
||||
// #[derive(Clone)]
|
||||
struct ContextGuard2<T> {
|
||||
_val: std::marker::PhantomData<T>,
|
||||
}
|
||||
impl<T> Clone for ContextGuard2<T> {
|
||||
// we aren't cloning the underlying data so clone isn't necessary
|
||||
fn clone(&self) -> Self {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
impl<T> Copy for ContextGuard2<T> {}
|
||||
|
||||
impl<T> ContextGuard2<T> {
|
||||
fn get<'a>(&'a self) -> ContextLock<'a, T> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
struct ContextLock<'a, T> {
|
||||
_val: std::marker::PhantomData<&'a T>,
|
||||
}
|
||||
impl<'a, T: 'a + 'static> Deref for ContextLock<'a, T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref<'b>(&'b self) -> &'b T {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
The source of the data that gives out context guards
|
||||
*/
|
||||
struct Context<'a> {
|
||||
_p: std::marker::PhantomData<&'a ()>,
|
||||
}
|
||||
|
||||
impl<'a> Context<'a> {
|
||||
fn use_context<'b, I, O: 'b>(&self, f: fn(&'b I) -> O) -> ContextGuard2<O> {
|
||||
todo!()
|
||||
}
|
||||
fn add_listener(&self, f: impl Fn(()) + 'static) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn view(self, f: impl FnOnce(&'a String) + 'a) {}
|
||||
// fn view(self, f: impl for<'b> FnOnce(&'a String) + 'a) {}
|
||||
// fn view(self, f: impl for<'b> FnOnce(&'b String) + 'a) {}
|
||||
}
|
||||
|
||||
struct Example {
|
||||
value: String,
|
||||
}
|
||||
/*
|
||||
Example compiling
|
||||
*/
|
||||
fn t<'a>(ctx: Context<'a>) {
|
||||
let value = ctx.use_context(|b: &Example| &b.value);
|
||||
|
||||
// Works properly, value is moved by copy into the closure
|
||||
let refed = value.get();
|
||||
println!("Value is {}", refed.as_str());
|
||||
let r2 = refed.as_str();
|
||||
|
||||
ctx.add_listener(move |_| {
|
||||
// let val = value.get().as_str();
|
||||
let val2 = r2.as_bytes();
|
||||
});
|
||||
|
||||
// let refed = value.deref();
|
||||
// returns &String
|
||||
|
||||
// returns &String
|
||||
// let refed = value.deref(); // returns &String
|
||||
// let refed = value.deref(); // returns &String
|
||||
|
||||
// Why does this work? This closure should be static but is holding a reference to refed
|
||||
// The context guard is meant to prevent any references moving into the closure
|
||||
// if the references move they might become invalid due to mutlithreading issues
|
||||
ctx.add_listener(move |_| {
|
||||
// let val = value.as_str();
|
||||
// let val2 = refed.as_bytes();
|
||||
});
|
||||
|
||||
ctx.view(move |b| {});
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
Reference in new issue
Block a user