dioxus/packages/core/examples/dummy.rs

104 lines
2.5 KiB
Rust
Raw Normal View History

// #![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};
2021-02-24 06:32:50 +00:00
2021-02-03 07:26:04 +00:00
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> {
2021-02-24 06:32:50 +00:00
fn use_context<'b, I, O: 'b>(&self, _f: fn(&'b I) -> O) -> ContextGuard2<O> {
todo!()
}
2021-02-24 06:32:50 +00:00
fn add_listener(&self, _f: impl Fn(()) + 'a) {
todo!()
}
2021-02-24 06:32:50 +00:00
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();
2021-02-24 06:32:50 +00:00
let _val2 = r2.as_bytes();
2021-02-23 20:08:23 +00:00
println!("v2 is {}", r2);
// println!("refed is {}", refed);
});
// 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();
});
2021-02-24 06:32:50 +00:00
ctx.view(move |_b| {});
}
2021-02-03 07:26:04 +00:00
fn main() {}