From 22d7e5707a7862c831838935aee28c347476995d Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Wed, 13 Dec 2023 08:13:36 -0800 Subject: [PATCH] #53 Add Deref and DerefMut for StateHandle --- src/views/state.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/views/state.rs b/src/views/state.rs index 0ea7d88..fbf88c5 100644 --- a/src/views/state.rs +++ b/src/views/state.rs @@ -1,5 +1,5 @@ use crate::*; -use std::any::Any; +use std::{any::Any, ops::Deref, ops::DerefMut}; /// Weak reference to app state. /// @@ -43,6 +43,21 @@ impl Binding for StateHandle { } } +impl Deref for StateHandle { + + type Target = S; + + fn deref(&self) -> &Self::Target { + unsafe { &(*self.cx)[*self] } + } +} + +impl DerefMut for StateHandle { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { &mut (*self.cx)[*self] } + } +} + #[derive(Clone)] struct StateView { default: D,