Skip to content

Commit

Permalink
feat: add invert to OptionStoreExt (#3534)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdi739 authored Feb 14, 2025
1 parent 590728e commit 68f4d46
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions reactive_stores/src/option.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{StoreField, Subfield};
use reactive_graph::traits::{Read, ReadUntracked};
use reactive_graph::traits::{FlattenOptionRefOption, Read, ReadUntracked};
use std::ops::Deref;

/// Extends optional store fields, with the ability to unwrap or map over them.
Expand All @@ -13,6 +13,13 @@ where
/// Provides access to the inner value, as a subfield, unwrapping the outer value.
fn unwrap(self) -> Subfield<Self, Option<Self::Output>, Self::Output>;

/// Inverts a subfield of an `Option` to an `Option` of a subfield.
fn invert(
self,
) -> Option<Subfield<Self, Option<Self::Output>, Self::Output>> {
self.map(|f| f)
}

/// Reactively maps over the field.
///
/// This returns `None` if the subfield is currently `None`,
Expand Down Expand Up @@ -56,7 +63,7 @@ where
self,
map_fn: impl FnOnce(Subfield<S, Option<T>, T>) -> U,
) -> Option<U> {
if self.read().is_some() {
if self.try_read().as_deref().flatten().is_some() {
Some(map_fn(self.unwrap()))
} else {
None
Expand All @@ -67,7 +74,7 @@ where
self,
map_fn: impl FnOnce(Subfield<S, Option<T>, T>) -> U,
) -> Option<U> {
if self.read_untracked().is_some() {
if self.try_read_untracked().as_deref().flatten().is_some() {
Some(map_fn(self.unwrap()))
} else {
None
Expand Down

0 comments on commit 68f4d46

Please sign in to comment.