From ba8a4f836c75dff63f026bd432d3a8659e5de394 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Wed, 16 Sep 2020 02:04:05 +0100 Subject: fix into_f64 and into_isize askama passes references to the filters (e.g. &f64), and &f64 doesn't implement NumCast, only f64 does. So, the filters should take &T instead of T. Fixes #357 Signed-off-by: Yuxuan Shui --- askama_shared/src/filters/mod.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'askama_shared') diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index f203348..9a5b160 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -249,7 +249,7 @@ pub fn indent(s: &dyn fmt::Display, width: &usize) -> Result { #[cfg(feature = "num-traits")] /// Casts number to f64 -pub fn into_f64(number: T) -> Result +pub fn into_f64(number: &T) -> Result where T: NumCast, { @@ -258,7 +258,7 @@ where #[cfg(feature = "num-traits")] /// Casts number to isize -pub fn into_isize(number: T) -> Result +pub fn into_isize(number: &T) -> Result where T: NumCast, { @@ -460,22 +460,22 @@ mod tests { #[test] #[allow(clippy::float_cmp)] fn test_into_f64() { - assert_eq!(into_f64(1).unwrap(), 1.0 as f64); - assert_eq!(into_f64(1.9).unwrap(), 1.9 as f64); - assert_eq!(into_f64(-1.9).unwrap(), -1.9 as f64); - assert_eq!(into_f64(INFINITY as f32).unwrap(), INFINITY); - assert_eq!(into_f64(-INFINITY as f32).unwrap(), -INFINITY); + assert_eq!(into_f64(&1).unwrap(), 1.0 as f64); + assert_eq!(into_f64(&1.9).unwrap(), 1.9 as f64); + assert_eq!(into_f64(&-1.9).unwrap(), -1.9 as f64); + assert_eq!(into_f64(&(INFINITY as f32)).unwrap(), INFINITY); + assert_eq!(into_f64(&(-INFINITY as f32)).unwrap(), -INFINITY); } #[cfg(feature = "num-traits")] #[test] fn test_into_isize() { - assert_eq!(into_isize(1).unwrap(), 1 as isize); - assert_eq!(into_isize(1.9).unwrap(), 1 as isize); - assert_eq!(into_isize(-1.9).unwrap(), -1 as isize); - assert_eq!(into_isize(1.5 as f64).unwrap(), 1 as isize); - assert_eq!(into_isize(-1.5 as f64).unwrap(), -1 as isize); - match into_isize(INFINITY) { + assert_eq!(into_isize(&1).unwrap(), 1 as isize); + assert_eq!(into_isize(&1.9).unwrap(), 1 as isize); + assert_eq!(into_isize(&-1.9).unwrap(), -1 as isize); + assert_eq!(into_isize(&(1.5 as f64)).unwrap(), 1 as isize); + assert_eq!(into_isize(&(-1.5 as f64)).unwrap(), -1 as isize); + match into_isize(&INFINITY) { Err(Fmt(fmt::Error)) => {} _ => panic!("Should return error of type Err(Fmt(fmt::Error))"), }; -- cgit