diff options
| author | Andreas Grois <andi@grois.info> | 2023-04-02 21:59:22 +0200 |
|---|---|---|
| committer | Andreas Grois <andi@grois.info> | 2023-04-02 21:59:22 +0200 |
| commit | cebdd3be32d50be379663e92d4428e6bba19ba51 (patch) | |
| tree | 8c5b57f2e2ed3c089e0546fdb95ef8c2082d1952 /tests/multiple_generics_lifetime.rs | |
| parent | 418f514fc46f45ae2901753e3398adb33664bed9 (diff) | |
Run cargo fmt
I think readability was better before that...
Diffstat (limited to 'tests/multiple_generics_lifetime.rs')
| -rw-r--r-- | tests/multiple_generics_lifetime.rs | 70 |
1 files changed, 42 insertions, 28 deletions
diff --git a/tests/multiple_generics_lifetime.rs b/tests/multiple_generics_lifetime.rs index 48a9bd1..8d24f0e 100644 --- a/tests/multiple_generics_lifetime.rs +++ b/tests/multiple_generics_lifetime.rs @@ -2,32 +2,39 @@ #![deny(clippy::all)] //! Tests if multiple generic parameters work, if the return value's lifetime depends on the mapping function lifetime. -use std::rc::Rc; +use higher::{Apply, Bind, Functor}; use higher_free_macro::free; -use higher::{Functor, Bind, Apply}; +use std::rc::Rc; #[derive(Clone)] -struct TestFunctor<'a, 'b, A, B>{ - data : &'b B, - next : Rc<dyn Fn(i32)->A + 'a>, +struct TestFunctor<'a, 'b, A, B> { + data: &'b B, + next: Rc<dyn Fn(i32) -> A + 'a>, } -impl<'a,'b,A : 'a,B> Functor<'a,A> for TestFunctor<'a, 'b, A, B>{ +impl<'a, 'b, A: 'a, B> Functor<'a, A> for TestFunctor<'a, 'b, A, B> { type Target<T> = TestFunctor<'a, 'b, T, B>; fn fmap<C, F>(self, f: F) -> Self::Target<C> where - F: Fn(A) -> C + 'a { - TestFunctor{ data : self.data, next : Rc::new(move |x| f((self.next)(x)))} + F: Fn(A) -> C + 'a, + { + TestFunctor { + data: self.data, + next: Rc::new(move |x| f((self.next)(x))), + } } } free!(<'xx>, FreeTest<'xx,'yy,AA,BB>, TestFunctor<'xx, 'yy, FreeTest<'xx, 'yy, AA, BB>, BB>); #[test] -fn test_lifetime_multiple_generics(){ - let free_monad = FreeTest::lift_f(TestFunctor{ data : &"Listening to NSP while writing this.", next : Rc::new(|x| f64::from(x)*0.5f64)}); - let functions = FreeTest::Pure(|x : f64| -> bool {x > 0.7f64} ).fmap(Into::into); +fn test_lifetime_multiple_generics() { + let free_monad = FreeTest::lift_f(TestFunctor { + data: &"Listening to NSP while writing this.", + next: Rc::new(|x| f64::from(x) * 0.5f64), + }); + let functions = FreeTest::Pure(|x: f64| -> bool { x > 0.7f64 }).fmap(Into::into); let free_monad_after_apply = free_monad.apply(functions); match free_monad_after_apply { FreeTest::Free(m) => { @@ -35,44 +42,51 @@ fn test_lifetime_multiple_generics(){ let x = m.next.clone(); let y = m.next.clone(); let m1 = x(1); - match m1{ + match m1 { FreeTest::Pure(v) => assert!(!v), FreeTest::Free(_) => unreachable!(), } let m2 = y(3); - match m2{ + match m2 { FreeTest::Pure(v) => assert!(v), FreeTest::Free(_) => unreachable!(), } - }, - FreeTest::Pure(_) => unreachable!() + } + FreeTest::Pure(_) => unreachable!(), } } //just to appease clippy without disabling the lint.... macro_rules! assert_nearly_equal { ($a:expr, $b:expr, $c:expr) => { - assert!((($a)-($b)).abs() < $c) + assert!((($a) - ($b)).abs() < $c) }; } #[test] -fn test_lifetime_multiple_generics_bind(){ - let m = FreeTest::lift_f(TestFunctor{ data : &"Listening to Soilwork while writing this.", next : Rc::new(|x| f64::from(x)*0.5f64)}); - let m = m.bind(|x : f64| -> FreeTest<_,_> { +fn test_lifetime_multiple_generics_bind() { + let m = FreeTest::lift_f(TestFunctor { + data: &"Listening to Soilwork while writing this.", + next: Rc::new(|x| f64::from(x) * 0.5f64), + }); + let m = m.bind(|x: f64| -> FreeTest<_, _> { if x < 0.0 { FreeTest::Pure(x.abs().floor()) } else { - FreeTest::lift_f(TestFunctor{data : &"Now it's Little Big.", next : Rc::new(move |y| f64::from(y) + x.ceil())}) - }}); - match m{ + FreeTest::lift_f(TestFunctor { + data: &"Now it's Little Big.", + next: Rc::new(move |y| f64::from(y) + x.ceil()), + }) + } + }); + match m { FreeTest::Free(m) => { assert_eq!(m.data, &"Listening to Soilwork while writing this."); - match (m.next)(-3){ + match (m.next)(-3) { FreeTest::Pure(v) => assert_nearly_equal!(v, 1f64, f64::EPSILON), FreeTest::Free(_) => unreachable!(), } - match (m.next)(3){ + match (m.next)(3) { FreeTest::Pure(_) => unreachable!(), FreeTest::Free(v) => { assert_eq!(v.data, &"Now it's Little Big."); @@ -80,9 +94,9 @@ fn test_lifetime_multiple_generics_bind(){ FreeTest::Pure(v) => assert_nearly_equal!(v, 7f64, f64::EPSILON), FreeTest::Free(_) => unreachable!(), } - }, + } } - }, - FreeTest::Pure(_) => unreachable!() + } + FreeTest::Pure(_) => unreachable!(), } -}
\ No newline at end of file +} |
