mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 02:54:25 +00:00
ui-contract: the in-place slot read hands its closure the slot or None, so a missing slot runs it once
This commit is contained in:
@@ -61,18 +61,18 @@ pub(crate) fn put_boxed<T: Any + Clone + Send + Sync>(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<T: Any + Clone + Send + Sync, R>(ctx: &Context, id: Id, f: impl FnOnce(&T) -> R) -> Option<R> {
|
||||
let moved = take::<T>(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<T: Any + Clone + Send + Sync, R>(ctx: &Context, id: Id, f: impl FnOnce(Option<&T>) -> R) -> R {
|
||||
let Some(moved) = take::<T>(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<T: Any + Clone + Send + Sync>(ctx: &Context, id: Id) -> Option<T> {
|
||||
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`.
|
||||
|
||||
@@ -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<T: Slot, R>(ctx: &Context, id: Id, f: impl FnOnce(&T) -> R) -> R {
|
||||
store::peek_with::<Wrapped<SHOWN, T>, R>(ctx, id, |wrapped| f(&wrapped.0)).unwrap_or_else(|| f(&T::default()))
|
||||
store::peek_with::<Wrapped<SHOWN, T>, 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.
|
||||
|
||||
Reference in New Issue
Block a user