use std::any::type_name;
use std::collections::{HashMap, VecDeque};
use std::fmt::{self, Debug};
use std::marker::PhantomData;
use std::ops::Receiver;
use std::rc::{Rc, Weak};
use std::time::Duration;
use slotmap::new_key_type;
use waft_embedder::{
ClipboardEvent, DragReport, EditCommand, MenuId, NotificationActivation, OpenRequest,
PlatformRef, RequestId, TrayIconEvent, TrayIconId, ViewError, ViewFocusEvent, ViewId,
};
use crate::app_cell::{AppCell, AsyncApp};
use crate::change_notifier::Listener;
use crate::entity::EntityMap;
use crate::executor::{ExecutorHandle, ForegroundExecutor, Task};
use crate::handle_map::HandleMap;
pub use crate::handle_map::{AnyRef, OptionRetained, Ref, Retained};
use crate::timers::{Timer, TimerMode, Timers};
const MICROTASK_BUDGET: usize = 100_000;
new_key_type! {
pub struct HandleId;
}
pub struct Handle<T> {
id: HandleId,
state: PhantomData<fn() -> T>,
}
impl<T> Handle<T> {
pub(crate) fn new(id: HandleId) -> Handle<T> {
Handle {
id,
state: PhantomData,
}
}
pub fn from_id(id: HandleId) -> Handle<T> {
Handle::new(id)
}
pub fn id(self) -> HandleId {
self.id
}
}
impl<T: 'static> Handle<T> {
pub fn upgrade(self, app: &App) -> Option<Ref<T>> {
app.handles.upgrade(self)
}
#[track_caller]
pub fn keep(self, app: &App) -> Ref<T> {
Retained::keep(app, self)
}
}
impl HandleId {
pub fn identity_hash(self) -> u64 {
slotmap::Key::data(&self).as_ffi() & 0xFFFF_FFFF
}
pub fn to_bits(self) -> u64 {
slotmap::Key::data(&self).as_ffi()
}
pub fn from_bits(bits: u64) -> HandleId {
slotmap::KeyData::from_ffi(bits).into()
}
pub fn upgrade(self, app: &App) -> Option<AnyRef> {
app.handles.upgrade_id(self)
}
#[track_caller]
pub fn keep(self, app: &App) -> AnyRef {
Retained::keep(app, self)
}
}
impl<T> Clone for Handle<T> {
fn clone(&self) -> Handle<T> {
*self
}
}
impl<T> Copy for Handle<T> {}
impl<T> Receiver for Handle<T> {
type Target = T;
}
impl<T> PartialEq for Handle<T> {
fn eq(&self, other: &Handle<T>) -> bool {
self.id == other.id
}
}
impl<T> Eq for Handle<T> {}
impl<T> std::hash::Hash for Handle<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}
impl<T> Debug for Handle<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Handle<{}>({:?})", type_name::<T>(), self.id)
}
}
impl<T> From<Handle<T>> for HandleId {
fn from(handle: Handle<T>) -> HandleId {
handle.id
}
}
#[non_exhaustive]
pub struct App {
this: Weak<AppCell>,
handles: HandleMap,
pub(crate) entities: EntityMap,
singletons: HashMap<std::any::TypeId, AnyRef>,
#[cfg(debug_assertions)]
skipped_tear_off_receivers: std::collections::HashSet<HandleId>,
#[cfg(debug_assertions)]
skipped_tear_offs: Vec<String>,
microtasks: VecDeque<Listener>,
timers: Timers,
debug_flags: HashMap<std::any::TypeId, bool>,
executor: ForegroundExecutor,
platform: PlatformRef,
dispatcher: std::sync::Arc<dyn waft_embedder::Dispatcher>,
platform_callbacks: PlatformCallbacks,
open_requests: UntilHandled<OpenRequest>,
notification_activations: UntilHandled<NotificationActivation>,
}
pub trait DebugFlag: 'static {
const DEFAULT: bool = false;
}
struct UntilHandled<T> {
handler: Option<Handler<T>>,
waiting: Vec<T>,
}
impl<T> Default for UntilHandled<T> {
fn default() -> UntilHandled<T> {
UntilHandled {
handler: None,
waiting: Vec::new(),
}
}
}
type Handler<T> = Rc<dyn Fn(&mut App, T)>;
pub type OpenRequestedCallback = Rc<dyn Fn(&mut App, OpenRequest)>;
pub type NotificationActivatedCallback = Rc<dyn Fn(&mut App, NotificationActivation)>;
#[derive(Default)]
pub struct PlatformCallbacks {
pub on_platform_brightness_changed: Option<Listener>,
pub on_locale_changed: Option<Listener>,
pub on_view_focus_change: Option<ViewFocusChangeCallback>,
pub on_drag: Option<DragCallback>,
pub on_clipboard_event: Option<ClipboardEventCallback>,
pub on_edit_command: Option<EditCommandCallback>,
pub on_menu_selected: Option<MenuSelectedCallback>,
pub on_dock_menu_selected: Option<MenuSelectedCallback>,
pub on_tray_icon_event: Option<TrayIconEventCallback>,
pub on_pop_route: Option<PopRouteCallback>,
pub on_view_added: Option<ViewChangeCallback>,
pub on_view_removed: Option<ViewChangeCallback>,
pub on_view_close_requested: Option<ViewCloseRequestedCallback>,
pub on_window_changed: Option<WindowChangedCallback>,
pub on_view_request_failed: Option<ViewRequestFailedCallback>,
}
pub type ViewFocusChangeCallback = Rc<dyn Fn(&mut App, ViewFocusEvent)>;
pub type DragCallback = Rc<dyn Fn(&mut App, DragReport)>;
pub type ClipboardEventCallback = Rc<dyn Fn(&mut App, ViewId, ClipboardEvent)>;
pub type EditCommandCallback = Rc<dyn Fn(&mut App, ViewId, EditCommand)>;
pub type MenuSelectedCallback = Rc<dyn Fn(&mut App, MenuId)>;
pub type TrayIconEventCallback = Rc<dyn Fn(&mut App, TrayIconId, TrayIconEvent)>;
pub type PopRouteCallback = Rc<dyn Fn(&mut App, ViewId) -> bool>;
pub type ViewChangeCallback = Rc<dyn Fn(&mut App, ViewId)>;
pub type ViewCloseRequestedCallback = Rc<dyn Fn(&mut App, ViewId)>;
pub type WindowChangedCallback = Rc<dyn Fn(&mut App, ViewId)>;
pub type ViewRequestFailedCallback = Rc<dyn Fn(&mut App, RequestId, ViewError)>;
impl App {
pub(crate) fn build(
this: Weak<AppCell>,
platform: PlatformRef,
executor: ForegroundExecutor,
) -> App {
App {
this,
handles: HandleMap::new(),
entities: EntityMap::new(),
singletons: HashMap::new(),
#[cfg(debug_assertions)]
skipped_tear_off_receivers: std::collections::HashSet::new(),
#[cfg(debug_assertions)]
skipped_tear_offs: Vec::new(),
microtasks: VecDeque::new(),
timers: Timers::default(),
debug_flags: HashMap::new(),
executor,
dispatcher: platform.dispatcher(),
platform,
platform_callbacks: PlatformCallbacks::default(),
open_requests: UntilHandled::default(),
notification_activations: UntilHandled::default(),
}
}
pub(crate) fn executor_handle(&self) -> ExecutorHandle {
self.executor.handle()
}
pub fn to_async(&self) -> AsyncApp {
AsyncApp::new(self.this.clone(), self.executor.handle())
}
pub fn spawn<R: 'static>(&self, f: impl AsyncFnOnce(&mut AsyncApp) -> R + 'static) -> Task<R> {
let mut cx = self.to_async();
self.executor.spawn(async move { f(&mut cx).await })
}
pub fn run_in_background<T: Send + 'static>(
&self,
work: impl FnOnce() -> T + Send + 'static,
) -> Task<T> {
self.executor.run_in_background(work)
}
pub fn platform(&self) -> PlatformRef {
Rc::clone(&self.platform)
}
pub fn platform_callbacks(&self) -> &PlatformCallbacks {
&self.platform_callbacks
}
pub fn platform_callbacks_mut(&mut self) -> &mut PlatformCallbacks {
&mut self.platform_callbacks
}
pub fn set_open_requested_handler(&mut self, handler: Option<OpenRequestedCallback>) {
self.set_handler(|app| &mut app.open_requests, handler);
}
pub fn open_requested(&mut self, request: OpenRequest) {
self.hand_to_handler(|app| &mut app.open_requests, request);
}
pub fn set_notification_activated_handler(
&mut self,
handler: Option<NotificationActivatedCallback>,
) {
self.set_handler(|app| &mut app.notification_activations, handler);
}
pub fn notification_activated(&mut self, activation: NotificationActivation) {
self.hand_to_handler(|app| &mut app.notification_activations, activation);
}
fn set_handler<T>(
&mut self,
queue: fn(&mut App) -> &mut UntilHandled<T>,
handler: Option<Handler<T>>,
) {
queue(self).handler = handler.clone();
let Some(handler) = handler else {
return;
};
for waiting in std::mem::take(&mut queue(self).waiting) {
handler(self, waiting);
}
}
fn hand_to_handler<T>(&mut self, queue: fn(&mut App) -> &mut UntilHandled<T>, asked: T) {
match queue(self).handler.clone() {
Some(handler) => handler(self, asked),
None => queue(self).waiting.push(asked),
}
}
#[track_caller]
pub fn singleton<T: 'static + Default>(&mut self) -> Handle<T> {
match self.get_singleton() {
Some(existing) => existing,
None => self.set_singleton(T::default()),
}
}
#[track_caller]
pub fn set_singleton<T: 'static>(&mut self, state: T) -> Handle<T> {
assert!(
self.get_singleton::<T>().is_none(),
"the App has a {} already",
std::any::type_name::<T>()
);
let singleton = self.create(state);
let handle = singleton.handle();
self.singletons
.insert(std::any::TypeId::of::<T>(), singleton.to_any());
handle
}
pub fn get_singleton<T: 'static>(&self) -> Option<Handle<T>> {
self.singletons
.get(&std::any::TypeId::of::<T>())
.map(|singleton| Handle::new(singleton.handle()))
}
pub fn schedule_microtask(&mut self, callback: Listener) {
self.microtasks.push_back(callback);
}
pub fn drain_microtasks(&mut self) {
let mut drained = 0usize;
while let Some(callback) = self.microtasks.pop_front() {
drained += 1;
assert!(
drained <= MICROTASK_BUDGET,
"microtasks did not converge after {MICROTASK_BUDGET} callbacks; \
are two callbacks scheduling each other in a cycle?"
);
callback.call(self);
}
}
pub fn has_pending_microtasks(&self) -> bool {
!self.microtasks.is_empty()
}
pub fn schedule_timer(&mut self, duration: Duration, callback: Listener) -> Timer {
let timer = self.timers.schedule(duration, callback);
self.update_timer_wakeup();
timer
}
pub fn schedule_periodic_timer(
&mut self,
duration: Duration,
callback: impl Fn(&mut App, Timer) + 'static,
) -> Timer {
let timer = self.timers.schedule_periodic(duration, Rc::new(callback));
self.update_timer_wakeup();
timer
}
pub fn cancel_timer(&mut self, timer: Timer) {
self.timers.cancel(timer);
self.update_timer_wakeup();
}
pub fn timer_is_active(&self, timer: Timer) -> bool {
self.timers.is_active(timer)
}
pub fn timer_tick(&self, timer: Timer) -> u64 {
self.timers.tick(timer)
}
pub fn clock(&self) -> Duration {
self.timers.now()
}
pub fn has_pending_timers(&self) -> bool {
self.timers.has_pending()
}
pub fn debug_flag<F: DebugFlag>(&self) -> bool {
self.debug_flags
.get(&std::any::TypeId::of::<F>())
.copied()
.unwrap_or(F::DEFAULT)
}
pub fn set_debug_flag<F: DebugFlag>(&mut self, value: bool) {
self.debug_flags.insert(std::any::TypeId::of::<F>(), value);
}
pub(crate) fn fire_next_due(&mut self, target: Duration, mode: TimerMode) -> bool {
let Some((timer, callback)) = self.timers.pop_next_due(target, mode) else {
return false;
};
callback(self, timer);
true
}
pub(crate) fn begin_timer_turn(&mut self) {
self.timers.begin_turn();
}
pub(crate) fn advance_clock_to(&mut self, target: Duration) {
self.timers.advance_to(target);
}
pub(crate) fn update_timer_wakeup(&mut self) {
if let Some(delay) = self.timers.timer_wakeup_if_changed() {
let wakeup = delay.map(|delay| self.platform.now() + delay);
self.dispatcher.wake_at(wakeup);
}
}
#[track_caller]
pub fn create<T: 'static>(&mut self, state: T) -> Ref<T> {
self.handles.create(state)
}
pub(crate) fn adopt_tree_node(&mut self, id: HandleId, keep: Rc<crate::handle_map::Keep>) {
self.handles.adopt_tree_node(id, keep);
}
pub fn release_tree_node(&mut self, node: impl Into<HandleId>) {
self.handles.release_tree_node(node.into());
}
pub(crate) fn not_kept(&self, id: HandleId) -> String {
self.handles.not_kept(id)
}
pub fn contains(&self, handle: impl Into<HandleId>) -> bool {
self.handles.contains(handle.into())
}
pub fn mark_disposed(&mut self, handle: impl Into<HandleId>) {
self.handles.mark_disposed(handle.into());
}
pub fn tear_off_receiver_is_live(&mut self, receiver: impl Into<HandleId>) -> bool {
let receiver = receiver.into();
let live = self.handles.contains(receiver);
#[cfg(debug_assertions)]
if !live && self.skipped_tear_off_receivers.insert(receiver) {
let message = format!(
"waft: a listener whose receiver was freed without removing it was skipped: {}",
self.handles.debug_describe(receiver)
);
eprintln!("{message}");
self.skipped_tear_offs.push(message);
}
live
}
pub fn expect_dispose(&mut self, handle: impl Into<HandleId>) {
self.handles.expect_dispose(handle.into());
}
#[cfg(debug_assertions)]
pub fn debug_take_not_disposed(&mut self) -> Vec<String> {
self.handles.take_not_disposed()
}
#[cfg(debug_assertions)]
pub fn debug_take_not_collected(&mut self) -> Vec<String> {
self.handles.take_not_collected()
}
#[cfg(debug_assertions)]
pub fn debug_live_objects(&self) -> Vec<crate::DebugObject> {
self.handles.debug_live_objects()
}
#[cfg(debug_assertions)]
pub fn debug_take_skipped_tear_offs(&mut self) -> Vec<String> {
std::mem::take(&mut self.skipped_tear_offs)
}
pub(crate) fn begin_checkpoint(&mut self) {
self.handles.begin_checkpoint();
}
pub(crate) fn free_dropped_objects(&mut self) {
self.handles.free_dropped();
}
pub fn is_disposed(&self, handle: impl Into<HandleId>) -> bool {
self.handles.is_disposed(handle.into())
}
pub fn handle<T: 'static>(&self, handle: impl Into<HandleId>) -> Option<Handle<T>> {
self.handles.handle(handle.into())
}
pub fn get<T: 'static>(&self, handle: impl ToHandle<T>) -> &T {
self.handles.get(handle.to_handle())
}
pub fn get_mut<T: 'static>(&mut self, handle: impl ToHandle<T>) -> &mut T {
self.handles.get_mut(handle.to_handle())
}
pub fn get_disjoint_mut<A: 'static, B: 'static>(
&mut self,
a: impl ToHandle<A>,
b: impl ToHandle<B>,
) -> (&mut A, &mut B) {
self.handles.get_disjoint_mut(a.to_handle(), b.to_handle())
}
}
pub trait ToHandle<T> {
fn to_handle(self) -> Handle<T>;
}
impl<T> ToHandle<T> for Handle<T> {
fn to_handle(self) -> Handle<T> {
self
}
}
impl<T> ToHandle<T> for &Handle<T> {
fn to_handle(self) -> Handle<T> {
*self
}
}
impl<T> ToHandle<T> for &Ref<T> {
fn to_handle(self) -> Handle<T> {
self.handle()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Timer;
#[derive(Debug, Default, PartialEq)]
struct Counter(i32);
#[derive(Debug, PartialEq)]
struct Label(&'static str);
#[test]
fn state_survives_a_round_trip_through_a_handle() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let counter_ref = app.create(Counter(1));
let counter = counter_ref.handle();
assert_eq!(app.get(counter), &Counter(1));
app.get_mut(counter).0 = 7;
assert_eq!(app.get(counter), &Counter(7));
}
#[test]
fn handles_of_different_types_share_one_arena() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let counter_ref = app.create(Counter(1));
let counter = counter_ref.handle();
let label_ref = app.create(Label("hello"));
let label = label_ref.handle();
assert_eq!(app.get(counter), &Counter(1));
assert_eq!(app.get(label), &Label("hello"));
}
#[test]
fn a_freed_entry_is_reused_but_its_handle_is_not() {
let cell = AppCell::new();
let first_id = cell.borrow_mut().create(Counter(1)).id();
cell.checkpoint();
let mut app = cell.borrow_mut();
let second_ref = app.create(Counter(2));
let second = second_ref.handle();
let entry_index = |id: HandleId| slotmap::Key::data(&id).as_ffi() & 0xffff_ffff;
assert_eq!(
entry_index(second.id()),
entry_index(first_id),
"the freed entry is reused, so the version is what tells them apart"
);
assert_ne!(second.id(), first_id);
assert!(!app.contains(first_id));
assert!(app.contains(second));
}
#[test]
#[cfg(debug_assertions)]
fn a_dropped_ref_keeps_the_object_readable_until_the_checkpoint() {
let cell = AppCell::new();
let counter_ref = cell.borrow_mut().create(Counter(3));
let counter = counter_ref.handle();
drop(counter_ref);
{
let app = cell.borrow();
assert_eq!(app.get(counter), &Counter(3), "still readable");
assert!(app.contains(counter), "still in the arena");
}
cell.checkpoint();
assert!(!cell.borrow().contains(counter), "freed at the checkpoint");
}
#[test]
#[should_panic(expected = "stale handle")]
fn reading_through_a_stale_handle_panics() {
let cell = AppCell::new();
let counter = cell.borrow_mut().create(Counter(1)).handle();
cell.checkpoint();
cell.borrow().get(counter);
}
#[test]
fn the_checked_path_from_an_untyped_id_answers_none_rather_than_panicking() {
let cell = AppCell::new();
let counter_ref = cell.borrow_mut().create(Counter(1));
let counter = counter_ref.handle();
let id = counter.id();
assert_eq!(cell.borrow().handle::<Counter>(id), Some(counter));
assert_eq!(cell.borrow().handle::<Label>(id), None, "wrong type");
drop(counter_ref);
cell.checkpoint();
assert_eq!(cell.borrow().handle::<Counter>(id), None, "stale");
}
#[test]
fn a_singleton_the_app_made_is_the_one_read_back() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let set = app.set_singleton(Counter(7));
assert_eq!(app.get_singleton::<Counter>(), Some(set));
assert_eq!(app.get(set).0, 7);
let asked: Handle<Counter> = app.singleton();
assert_eq!(asked, set, "asking makes no second one");
}
#[test]
#[should_panic(expected = "already")]
fn a_second_singleton_of_a_type_is_refused() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
app.set_singleton(Counter(1));
app.set_singleton(Counter(2));
}
#[test]
fn a_singleton_is_one_handle_per_type_per_app() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let first: Handle<Counter> = app.singleton();
let second: Handle<Counter> = app.singleton();
assert_eq!(first, second);
app.get_mut(first).0 = 9;
assert_eq!(app.get(second).0, 9, "one entry behind both handles");
let other_cell = AppCell::new();
let mut other_app = other_cell.borrow_mut();
let elsewhere: Handle<Counter> = other_app.singleton();
assert_eq!(elsewhere.id(), first.id(), "ids may collide across Apps");
assert_eq!(other_app.get(elsewhere).0, 0, "but the state is per App");
}
#[test]
fn a_handle_is_copy_so_an_edge_can_be_a_plain_field() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let counter_ref = app.create(Counter(1));
let counter = counter_ref.handle();
struct Edge {
target: Handle<Counter>,
}
let edge = Edge { target: counter };
let copied = edge.target;
assert_eq!(copied, counter);
assert_eq!(app.get(edge.target).0, 1);
}
#[test]
fn a_microtask_runs_on_drain_not_inline() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let counter_ref = app.create(Counter(0));
let counter = counter_ref.handle();
app.schedule_microtask(crate::Listener::new(move |app| {
app.get_mut(counter).0 = 1;
}));
assert_eq!(app.get(counter).0, 0);
assert!(app.has_pending_microtasks());
app.drain_microtasks();
assert_eq!(app.get(counter).0, 1);
assert!(!app.has_pending_microtasks());
}
#[test]
fn a_microtask_scheduled_during_drain_runs_in_the_same_drain() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let counter_ref = app.create(Counter(0));
let counter = counter_ref.handle();
app.schedule_microtask(crate::Listener::new(move |app| {
app.get_mut(counter).0 = 1;
app.schedule_microtask(crate::Listener::new(move |app| {
app.get_mut(counter).0 = 2;
}));
}));
app.drain_microtasks();
assert_eq!(app.get(counter).0, 2);
}
#[test]
fn timers_fire_in_due_order_after_microtasks() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let log_ref = app.create(Vec::<&'static str>::new());
let log = log_ref.handle();
app.schedule_microtask(crate::Listener::new(move |app| {
app.get_mut(log).push("micro");
}));
Timer::new(
&mut app,
Duration::from_millis(200),
crate::Listener::new(move |app| {
app.get_mut(log).push("late");
}),
);
Timer::new(
&mut app,
Duration::from_millis(100),
crate::Listener::new(move |app| {
app.get_mut(log).push("early");
}),
);
Timer::new(
&mut app,
Duration::from_millis(100),
crate::Listener::new(move |app| {
app.get_mut(log).push("early-second");
}),
);
assert!(app.get(log).is_empty());
drop(app);
cell.elapse(Duration::from_millis(200));
let app = cell.borrow();
assert_eq!(
app.get(log).as_slice(),
["micro", "early", "early-second", "late"]
);
}
#[test]
fn cancel_prevents_a_timer_from_firing() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let counter_ref = app.create(Counter(0));
let counter = counter_ref.handle();
let timer = Timer::new(
&mut app,
Duration::from_millis(10),
crate::Listener::new(move |app| {
app.get_mut(counter).0 = 1;
}),
);
assert!(timer.is_active(&app));
timer.cancel(&mut app);
assert!(!timer.is_active(&app));
drop(app);
cell.elapse(Duration::from_millis(10));
let app = cell.borrow();
assert_eq!(app.get(counter).0, 0);
}
#[test]
fn get_disjoint_mut_borrows_two_entries_at_once() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let a_ref = app.create(1u32);
let a = a_ref.handle();
let b_ref = app.create(String::from("x"));
let b = b_ref.handle();
let (a_value, b_value) = app.get_disjoint_mut(a, b);
*a_value += 1;
b_value.push('y');
assert_eq!(*app.get(a), 2);
assert_eq!(app.get(b), "xy");
}
#[test]
#[should_panic(expected = "not two live entries")]
fn get_disjoint_mut_rejects_the_same_entry_twice() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let a_ref = app.create(1u32);
let a = a_ref.handle();
let _ = app.get_disjoint_mut(a, a);
}
#[test]
fn from_id_reads_like_the_minted_handle() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let minted_ref = app.create(Counter(7));
let minted = minted_ref.handle();
let rebuilt = Handle::<Counter>::from_id(minted.id());
assert_eq!(rebuilt, minted);
assert_eq!(app.get(rebuilt).0, 7);
}
#[test]
#[should_panic(expected = "stale handle")]
fn from_id_defers_the_stale_check_to_get() {
let cell = AppCell::new();
let id = cell.borrow_mut().create(Counter(0)).id();
cell.checkpoint();
let rebuilt = Handle::<Counter>::from_id(id);
cell.borrow().get(rebuilt);
}
}