Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions crates/actuate-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,30 +455,32 @@ impl<'a, C> Deref for Scope<'a, C> {
/// Use an immutable reference to a value of type `T`.
///
/// `make_value` will only be called once to initialize this value.
pub fn use_ref<T: 'static>(cx: &ScopeState, make_value: impl FnOnce() -> T) -> &T {
pub fn use_ref<'a, T: 'a>(cx: &'a ScopeState, make_value: impl FnOnce() -> T) -> &'a T {
let hooks = unsafe { &mut *cx.hooks.get() };

let idx = cx.hook_idx.get();
cx.hook_idx.set(idx + 1);

let any = if idx >= hooks.len() {
hooks.push(Box::new(make_value()));
let b = Box::new(make_value());
hooks.push(unsafe { core::mem::transmute::<Box<T>, Box<()>>(b) });
hooks.last().unwrap()
} else {
hooks.get(idx).unwrap()
};
(**any).downcast_ref().unwrap()
let b = (**any).downcast_ref::<()>().unwrap();
unsafe { core::mem::transmute::<&(), &T>(b) }
}

struct MutState<T> {
value: T,
struct MutState<T: ?Sized> {
generation: Cell<u64>,
value: T,
}

/// Use a mutable reference to a value of type `T`.
///
/// `make_value` will only be called once to initialize this value.
pub fn use_mut<T: 'static>(cx: &ScopeState, make_value: impl FnOnce() -> T) -> Mut<T> {
pub fn use_mut<'a, T: 'a>(cx: &'a ScopeState, make_value: impl FnOnce() -> T) -> Mut<'a, T> {
let hooks = unsafe { &mut *cx.hooks.get() };

let idx = cx.hook_idx.get();
Expand All @@ -489,12 +491,14 @@ pub fn use_mut<T: 'static>(cx: &ScopeState, make_value: impl FnOnce() -> T) -> M
value: make_value(),
generation: Cell::new(0),
};
hooks.push(Box::new(state));
let b = Box::new(state);
hooks.push(unsafe { core::mem::transmute::<_, Box<MutState<()>>>(b) });
hooks.last_mut().unwrap()
} else {
hooks.get_mut(idx).unwrap()
};
let state: &mut MutState<T> = any.downcast_mut().unwrap();
let state: &mut MutState<()> = any.downcast_mut().unwrap();
let state = unsafe { core::mem::transmute::<_, &mut MutState<T>>(state) };

Mut {
ptr: &mut state.value as *mut T,
Expand Down Expand Up @@ -967,7 +971,7 @@ where

let cx = Scope { me: self, state };

let cell: &UnsafeCell<Option<Box<dyn AnyCompose>>> = use_ref(&cx, || UnsafeCell::new(None));
let cell: &UnsafeCell<Option<_>> = use_ref(&cx, || UnsafeCell::new(None));
let cell = unsafe { &mut *cell.get() };

let child_state = use_ref(&cx, ScopeState::default);
Expand All @@ -992,14 +996,7 @@ where
*child_state.contexts.borrow_mut() = cx.contexts.borrow().clone();
child_state.is_parent_changed.set(true);

unsafe {
if let Some(ref mut content) = cell {
child.reborrow((**content).as_ptr_mut());
} else {
let boxed: Box<dyn AnyCompose> = Box::new(child);
*cell = Some(mem::transmute(boxed));
}
}
*cell = Some(child);
} else {
child_state.is_parent_changed.set(false);
}
Expand Down