From 68f4d46c5f3dd999ce0ba6077c7f4732f093407b Mon Sep 17 00:00:00 2001 From: mahdi739 <86552031+mahdi739@users.noreply.github.com> Date: Fri, 14 Feb 2025 22:22:38 +0330 Subject: [PATCH] feat: add `invert` to `OptionStoreExt` (#3534) --- reactive_stores/src/option.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/reactive_stores/src/option.rs b/reactive_stores/src/option.rs index 8343c584ea..c727662548 100644 --- a/reactive_stores/src/option.rs +++ b/reactive_stores/src/option.rs @@ -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. @@ -13,6 +13,13 @@ where /// Provides access to the inner value, as a subfield, unwrapping the outer value. fn unwrap(self) -> Subfield, Self::Output>; + /// Inverts a subfield of an `Option` to an `Option` of a subfield. + fn invert( + self, + ) -> Option, Self::Output>> { + self.map(|f| f) + } + /// Reactively maps over the field. /// /// This returns `None` if the subfield is currently `None`, @@ -56,7 +63,7 @@ where self, map_fn: impl FnOnce(Subfield, T>) -> U, ) -> Option { - if self.read().is_some() { + if self.try_read().as_deref().flatten().is_some() { Some(map_fn(self.unwrap())) } else { None @@ -67,7 +74,7 @@ where self, map_fn: impl FnOnce(Subfield, T>) -> U, ) -> Option { - if self.read_untracked().is_some() { + if self.try_read_untracked().as_deref().flatten().is_some() { Some(map_fn(self.unwrap())) } else { None