Skip to content

Commit

Permalink
feat: impl Into<Field<T>> for Store<T> (closes #3102)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbj committed Oct 29, 2024
1 parent 234d138 commit 6e13ff9
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
67 changes: 63 additions & 4 deletions reactive_stores/src/arc_field.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::{
path::{StorePath, StorePathSegment},
AtIndex, AtKeyed, KeyMap, KeyedSubfield, StoreField, StoreFieldTrigger,
Subfield,
ArcStore, AtIndex, AtKeyed, KeyMap, KeyedSubfield, Store, StoreField,
StoreFieldTrigger, Subfield,
};
use reactive_graph::traits::{
DefinedAt, IsDisposed, Notify, ReadUntracked, Track, UntrackableGuard,
use reactive_graph::{
owner::Storage,
traits::{
DefinedAt, IsDisposed, Notify, ReadUntracked, Track, UntrackableGuard,
},
};
use std::{
fmt::Debug,
Expand Down Expand Up @@ -99,6 +102,62 @@ impl<T> StoreField for ArcField<T> {
}
}

impl<T, S> From<Store<T, S>> for ArcField<T>
where
T: 'static,
S: Storage<ArcStore<T>>,
{
#[track_caller]
fn from(value: Store<T, S>) -> Self {
ArcField {
#[cfg(debug_assertions)]
defined_at: Location::caller(),
path: value.path().into_iter().collect(),
trigger: value.get_trigger(value.path().into_iter().collect()),
get_trigger: Arc::new(move |path| value.get_trigger(path)),
read: Arc::new(move || value.reader().map(StoreFieldReader::new)),
write: Arc::new(move || value.writer().map(StoreFieldWriter::new)),
keys: Arc::new(move || value.keys()),
track_field: Arc::new(move || value.track_field()),
}
}
}

impl<T> From<ArcStore<T>> for ArcField<T>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: ArcStore<T>) -> Self {
ArcField {
#[cfg(debug_assertions)]
defined_at: Location::caller(),
path: value.path().into_iter().collect(),
trigger: value.get_trigger(value.path().into_iter().collect()),
get_trigger: Arc::new({
let value = value.clone();
move |path| value.get_trigger(path)
}),
read: Arc::new({
let value = value.clone();
move || value.reader().map(StoreFieldReader::new)
}),
write: Arc::new({
let value = value.clone();
move || value.writer().map(StoreFieldWriter::new)
}),
keys: Arc::new({
let value = value.clone();
move || value.keys()
}),
track_field: Arc::new({
let value = value.clone();
move || value.track_field()
}),
}
}
}

impl<Inner, Prev, T> From<Subfield<Inner, Prev, T>> for ArcField<T>
where
T: Send + Sync,
Expand Down
34 changes: 32 additions & 2 deletions reactive_stores/src/field.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
arc_field::{StoreFieldReader, StoreFieldWriter},
path::{StorePath, StorePathSegment},
ArcField, AtIndex, AtKeyed, KeyMap, KeyedSubfield, StoreField,
StoreFieldTrigger, Subfield,
ArcField, ArcStore, AtIndex, AtKeyed, KeyMap, KeyedSubfield, Store,
StoreField, StoreFieldTrigger, Subfield,
};
use reactive_graph::{
owner::{ArenaItem, Storage, SyncStorage},
Expand Down Expand Up @@ -55,6 +55,36 @@ where
}
}

impl<T, S> From<Store<T, S>> for Field<T, S>
where
T: 'static,
S: Storage<ArcStore<T>> + Storage<ArcField<T>>,
{
#[track_caller]
fn from(value: Store<T, S>) -> Self {
Field {
#[cfg(debug_assertions)]
defined_at: Location::caller(),
inner: ArenaItem::new_with_storage(value.into()),
}
}
}

impl<T, S> From<ArcStore<T>> for Field<T, S>
where
T: Send + Sync + 'static,
S: Storage<ArcStore<T>> + Storage<ArcField<T>>,
{
#[track_caller]
fn from(value: ArcStore<T>) -> Self {
Field {
#[cfg(debug_assertions)]
defined_at: Location::caller(),
inner: ArenaItem::new_with_storage(value.into()),
}
}
}

impl<Inner, Prev, T, S> From<Subfield<Inner, Prev, T>> for Field<T, S>
where
T: Send + Sync,
Expand Down

0 comments on commit 6e13ff9

Please sign in to comment.