From cff277b3dbb0e0629c0461069c78475d7705ec0c Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Tue, 19 Nov 2024 10:04:40 -0500 Subject: [PATCH] fix: add `Write` implementatations for `Field` and `ArcField` (closes #3257) (#3262) --- reactive_stores/src/arc_field.rs | 20 +++++++++++++++++++- reactive_stores/src/field.rs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/reactive_stores/src/arc_field.rs b/reactive_stores/src/arc_field.rs index e21abc58cc..baa66eb4c8 100644 --- a/reactive_stores/src/arc_field.rs +++ b/reactive_stores/src/arc_field.rs @@ -7,6 +7,7 @@ use reactive_graph::{ owner::Storage, traits::{ DefinedAt, IsDisposed, Notify, ReadUntracked, Track, UntrackableGuard, + Write, }, }; use std::{ @@ -31,7 +32,8 @@ where trigger: StoreFieldTrigger, get_trigger: Arc StoreFieldTrigger + Send + Sync>, read: Arc Option> + Send + Sync>, - write: Arc Option> + Send + Sync>, + pub(crate) write: + Arc Option> + Send + Sync>, keys: Arc Option + Send + Sync>, track_field: Arc, } @@ -329,6 +331,22 @@ impl ReadUntracked for ArcField { } } +impl Write for ArcField { + type Value = T; + + fn try_write(&self) -> Option> { + (self.write)() + } + + fn try_write_untracked( + &self, + ) -> Option> { + let mut guard = (self.write)()?; + guard.untrack(); + Some(guard) + } +} + impl IsDisposed for ArcField { fn is_disposed(&self) -> bool { false diff --git a/reactive_stores/src/field.rs b/reactive_stores/src/field.rs index 89f0cc4678..d92a1e3fce 100644 --- a/reactive_stores/src/field.rs +++ b/reactive_stores/src/field.rs @@ -6,10 +6,18 @@ use crate::{ }; use reactive_graph::{ owner::{ArenaItem, Storage, SyncStorage}, - traits::{DefinedAt, IsDisposed, Notify, ReadUntracked, Track}, + traits::{ + DefinedAt, IsDisposed, Notify, ReadUntracked, Track, UntrackableGuard, + Write, + }, unwrap_signal, }; -use std::{fmt::Debug, hash::Hash, ops::IndexMut, panic::Location}; +use std::{ + fmt::Debug, + hash::Hash, + ops::{DerefMut, IndexMut}, + panic::Location, +}; /// Wraps access to a single field of type `T`. /// @@ -204,6 +212,24 @@ where } } +impl Write for Field { + type Value = T; + + fn try_write(&self) -> Option> { + self.inner.try_get_value().and_then(|inner| (inner.write)()) + } + + fn try_write_untracked( + &self, + ) -> Option> { + self.inner.try_get_value().and_then(|inner| { + let mut guard = (inner.write)()?; + guard.untrack(); + Some(guard) + }) + } +} + impl IsDisposed for Field { fn is_disposed(&self) -> bool { self.inner.is_disposed()