Skip to content

Commit

Permalink
fix: add Write implementatations for Field<T> and ArcField<T> (c…
Browse files Browse the repository at this point in the history
…loses #3257)
  • Loading branch information
gbj committed Nov 18, 2024
1 parent 36132a5 commit 9718041
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
20 changes: 19 additions & 1 deletion reactive_stores/src/arc_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use reactive_graph::{
owner::Storage,
traits::{
DefinedAt, IsDisposed, Notify, ReadUntracked, Track, UntrackableGuard,
Write,
},
};
use std::{
Expand All @@ -31,7 +32,8 @@ where
trigger: StoreFieldTrigger,
get_trigger: Arc<dyn Fn(StorePath) -> StoreFieldTrigger + Send + Sync>,
read: Arc<dyn Fn() -> Option<StoreFieldReader<T>> + Send + Sync>,
write: Arc<dyn Fn() -> Option<StoreFieldWriter<T>> + Send + Sync>,
pub(crate) write:
Arc<dyn Fn() -> Option<StoreFieldWriter<T>> + Send + Sync>,
keys: Arc<dyn Fn() -> Option<KeyMap> + Send + Sync>,
track_field: Arc<dyn Fn() + Send + Sync>,
}
Expand Down Expand Up @@ -329,6 +331,22 @@ impl<T> ReadUntracked for ArcField<T> {
}
}

impl<T> Write for ArcField<T> {
type Value = T;

fn try_write(&self) -> Option<impl UntrackableGuard<Target = Self::Value>> {
(self.write)()
}

fn try_write_untracked(
&self,
) -> Option<impl DerefMut<Target = Self::Value>> {
let mut guard = (self.write)()?;
guard.untrack();
Some(guard)
}
}

impl<T> IsDisposed for ArcField<T> {
fn is_disposed(&self) -> bool {
false
Expand Down
30 changes: 28 additions & 2 deletions reactive_stores/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
///
Expand Down Expand Up @@ -204,6 +212,24 @@ where
}
}

impl<T> Write for Field<T> {
type Value = T;

fn try_write(&self) -> Option<impl UntrackableGuard<Target = Self::Value>> {
self.inner.try_get_value().and_then(|inner| (inner.write)())
}

fn try_write_untracked(
&self,
) -> Option<impl DerefMut<Target = Self::Value>> {
self.inner.try_get_value().and_then(|inner| {
let mut guard = (inner.write)()?;
guard.untrack();
Some(guard)
})
}
}

impl<T, S> IsDisposed for Field<T, S> {
fn is_disposed(&self) -> bool {
self.inner.is_disposed()
Expand Down

0 comments on commit 9718041

Please sign in to comment.