diff --git a/crates/ui-contract/src/runtime/egui/memory.rs b/crates/ui-contract/src/runtime/egui/memory.rs index f6d0911a8..33f852545 100644 --- a/crates/ui-contract/src/runtime/egui/memory.rs +++ b/crates/ui-contract/src/runtime/egui/memory.rs @@ -61,18 +61,18 @@ pub(crate) fn put_boxed(data: &mut Map, id: Id, va data.insert_temp_boxed(id, value) } -/// Runs `f` on the slot under `id` without running slot code under the data lock: the entry moves out, `f` reads -/// it in place and it moves back. `None` when no slot of type `T` is stored. -pub(crate) fn peek_with(ctx: &Context, id: Id, f: impl FnOnce(&T) -> R) -> Option { - let moved = take::(ctx, id)?; - let out = f(&moved); +/// 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 out = f(Some(&moved)); ctx.data_mut(|data| data.insert_temp(id, moved)); - Some(out) + out } /// 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, T::clone) + peek_with(ctx, id, |slot: Option<&T>| slot.cloned()) } /// Whether a slot of type `T` is stored under `id`. diff --git a/crates/ui-contract/src/runtime/memory.rs b/crates/ui-contract/src/runtime/memory.rs index 53ff01345..d9fc95f82 100644 --- a/crates/ui-contract/src/runtime/memory.rs +++ b/crates/ui-contract/src/runtime/memory.rs @@ -474,7 +474,10 @@ impl MemoryStage { /// Like `peek`, but `f` reads the slot in place and only its result is copied; a missing slot reads as the default. pub(crate) fn peek_with(ctx: &Context, id: Id, f: impl FnOnce(&T) -> R) -> R { - store::peek_with::, R>(ctx, id, |wrapped| f(&wrapped.0)).unwrap_or_else(|| f(&T::default())) + store::peek_with::, R>(ctx, id, |wrapped| match wrapped { + Some(wrapped) => f(&wrapped.0), + None => f(&T::default()), + }) } /// Read-only access to a session slot, as `peek` does for a shown one.