diff --git a/crates/ui-contract/src/runtime/egui/memory.rs b/crates/ui-contract/src/runtime/egui/memory.rs index 6f741b3bc..a1cd2998f 100644 --- a/crates/ui-contract/src/runtime/egui/memory.rs +++ b/crates/ui-contract/src/runtime/egui/memory.rs @@ -68,12 +68,17 @@ pub(crate) fn put_boxed(data: &mut Map, id: Id, va /// Runs `f` on the slot under `id` (`None` when no slot of type `T` is stored) without running slot code under the /// data lock: the entry moves out, `f` reads it in place and it moves back. pub(crate) fn peek_with(ctx: &Context, id: Id, f: impl FnOnce(Option<&T>) -> R) -> R { - let Some(moved) = take::(ctx, id) else { return f(None) }; + let Some(moved) = take_boxed::(ctx, id).and_then(|stored| stored.downcast::().ok()) else { return f(None) }; let out = f(Some(&moved)); - ctx.data_mut(|data| data.insert_temp(id, moved)); + restore(ctx, id, moved); out } +/// Return the original slot allocation. Any replaced value drops after the data lock is released. +fn restore(ctx: &Context, id: Id, value: Box) { + drop(ctx.data_mut(|data| put_boxed(data, id, value))); +} + /// A copy of the slot under `id`, read as [`peek_with`] reads it. pub(crate) fn peek_clone(ctx: &Context, id: Id) -> Option { peek_with(ctx, id, |slot: Option<&T>| slot.cloned())