From b1a9a9e0f13488c4b10eff181d30a692c20fc50f Mon Sep 17 00:00:00 2001 From: qwertz19281 Date: Mon, 1 Mar 2021 14:31:07 +0100 Subject: [PATCH] Test for util::SMA --- src/util/sma.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/util/sma.rs b/src/util/sma.rs index b58caf79..5fbe871f 100644 --- a/src/util/sma.rs +++ b/src/util/sma.rs @@ -31,6 +31,14 @@ impl<'a,T,U,F> SMA<'a,T,U,F> where F: SMALens { p: PhantomData, } } + #[inline] + pub fn fork_with_lens_fn(&self, lens: G) -> SMA<'a,T,V,G> where G: Fn(&mut T) -> &mut V + Clone { + SMA{ + inner: self.inner.refc(), + f: lens, + p: PhantomData, + } + } #[inline] pub fn borrow_mut(&self) -> RefMut { @@ -138,3 +146,22 @@ impl<'w,'a,E,T,U,F> CaptionMut for SMA<'a,T,U,F> where self.borrow_mut().replace(s) } } + +mod tests { + use super::*; + + #[test] + fn test_sma() { + let mut a: &mut (u32,u32) = &mut (5,5); + let mut sma = SMA::new(a); + + let mut sma_a = sma.fork(); + let mut sma_b = sma.fork_with_lens_fn(|v: &mut (u32,u32)| &mut v.0 ); + + assert_eq!(*sma.borrow_mut(),(5,5)); + sma_a.borrow_mut().0 = 6; + assert_eq!(*sma.borrow_mut(),(6,5)); + *sma_b.borrow_mut() = 7; + assert_eq!(*sma.borrow_mut(),(7,5)); + } +}