dioxus_stores/impls/deref.rs
1use std::ops::DerefMut;
2
3use crate::{store::Store, MappedStore};
4use dioxus_signals::Readable;
5
6impl<Lens, T> Store<T, Lens>
7where
8 Lens: Readable<Target = T> + 'static,
9 T: DerefMut + 'static,
10{
11 /// Returns a store that dereferences the original value. The dereferenced store shares the same
12 /// subscriptions and tracking as the original store, but allows you to access the methods of the underlying type.
13 ///
14 /// # Example
15 ///
16 /// ```rust, no_run
17 /// use dioxus_stores::*;
18 /// let store = use_store(|| Box::new(vec![1, 2, 3]));
19 /// let deref_store = store.deref();
20 /// // The dereferenced store can access the store methods of the underlying type.
21 /// assert_eq!(deref_store.len(), 3);
22 /// ```
23 pub fn deref(self) -> MappedStore<T::Target, Lens> {
24 let map: fn(&T) -> &T::Target = |value| value.deref();
25 let map_mut: fn(&mut T) -> &mut T::Target = |value| value.deref_mut();
26 self.into_selector().map(map, map_mut).into()
27 }
28}