From a4c06ca083eab433d987a1369a45bae23583a5b0 Mon Sep 17 00:00:00 2001 From: Atul Bhosale Date: Thu, 24 Oct 2019 23:41:12 +0530 Subject: [PATCH] Format code using 'cargo fmt' --- overflower-plugin/src/lib.rs | 111 +++-- overflower/src/lib.rs | 845 +++++++++++++++++++++++++++------ overflower/tests/macro.rs | 4 +- overflower/tests/quickcheck.rs | 165 ++++--- overflower/tests/simple.rs | 6 +- 5 files changed, 868 insertions(+), 263 deletions(-) diff --git a/overflower-plugin/src/lib.rs b/overflower-plugin/src/lib.rs index 7875ff5..530a067 100644 --- a/overflower-plugin/src/lib.rs +++ b/overflower-plugin/src/lib.rs @@ -1,6 +1,6 @@ extern crate proc_macro; -extern crate syn; extern crate quote; +extern crate syn; use self::proc_macro::TokenStream; use quote::quote; @@ -26,7 +26,7 @@ enum Overflower { } impl Parse for Overflower { - fn parse(input: ParseStream) -> Result { + fn parse(input: ParseStream) -> Result { let ident = input.parse::()?; if ident == "wrap" { Ok(Overflower::Wrap) @@ -44,9 +44,12 @@ impl Parse for Overflower { impl Overflower { fn is_overflow(&self, attrs: &[Attribute]) -> bool { - if let Overflower::Default = *self { return true; } - attrs.iter().any(|a| a.path.segments.iter() - .next().unwrap().ident == "overflow") + if let Overflower::Default = *self { + return true; + } + attrs + .iter() + .any(|a| a.path.segments.iter().next().unwrap().ident == "overflow") } fn method_path(&self, method: &str) -> syn::Path { @@ -54,15 +57,22 @@ impl Overflower { Overflower::Wrap => "Wrap", Overflower::Panic => "Panic", Overflower::Saturate => "Saturate", - Overflower::Default => "Default" + Overflower::Default => "Default", }; let crate_name = syn::parse_str::("overflower").unwrap(); - let trait_name = syn::parse_str::(&(method.split("_").flat_map(|s| { - let mut me = s.chars(); - me.next().unwrap().to_uppercase().chain(me) - }).collect::() + mo)).unwrap(); - let method_name = syn::parse_str::(&format!("{}_{}", - method, &mo.to_lowercase())).unwrap(); + let trait_name = syn::parse_str::( + &(method + .split("_") + .flat_map(|s| { + let mut me = s.chars(); + me.next().unwrap().to_uppercase().chain(me) + }) + .collect::() + + mo), + ) + .unwrap(); + let method_name = + syn::parse_str::(&format!("{}_{}", method, &mo.to_lowercase())).unwrap(); parse_quote!(#crate_name :: #trait_name :: #method_name) } @@ -70,7 +80,7 @@ impl Overflower { let method_path = Expr::Path(syn::ExprPath { attrs: vec![], qself: None, - path: self.method_path(m) + path: self.method_path(m), }); parse_quote!(#method_path ( #(#args),* )) } @@ -95,13 +105,16 @@ impl Overflower { op, right, } = a; - let mut args = vec![Expr::Reference(ExprReference { + let mut args = vec![ + Expr::Reference(ExprReference { attrs: vec![], and_token: Default::default(), raw: Default::default(), mutability: Some(Default::default()), - expr: Box::new(self.fold_expr(*left)) - }), self.fold_expr(*right)]; + expr: Box::new(self.fold_expr(*left)), + }), + self.fold_expr(*right), + ]; match op { syn::BinOp::AddEq(_) => self.make_method("add_assign", args), syn::BinOp::SubEq(_) => self.make_method("sub_assign", args), @@ -125,7 +138,6 @@ impl Overflower { }) } } - } fn make_binary(&mut self, b: ExprBinary) -> Expr { @@ -165,15 +177,16 @@ impl Overflower { } let is_abs = if let syn::Expr::Path(ref p) = *c.func { let segments = &p.path.segments; - static FACADE : [&str; 2] = ["std", "core"]; - static TYPES : [&str; 6] = ["i8", "i16", "i32", "i64", "i128", - "isize"]; - static FUNCTION : [&str; 1] = ["abs"]; - static ABS_MATCHERS : [&[&str]; 3] = [&FACADE, &TYPES, &FUNCTION]; + static FACADE: [&str; 2] = ["std", "core"]; + static TYPES: [&str; 6] = ["i8", "i16", "i32", "i64", "i128", "isize"]; + static FUNCTION: [&str; 1] = ["abs"]; + static ABS_MATCHERS: [&[&str]; 3] = [&FACADE, &TYPES, &FUNCTION]; - if segments.iter().zip(&ABS_MATCHERS[3 - segments.len()..]).all( - |(seg, m)| seg.arguments.is_empty() && m.iter().any( - |s| seg.ident == s)) { + if segments + .iter() + .zip(&ABS_MATCHERS[3 - segments.len()..]) + .all(|(seg, m)| seg.arguments.is_empty() && m.iter().any(|s| seg.ident == s)) + { true } else { false @@ -198,62 +211,80 @@ impl Overflower { return Expr::Macro(m); } let mut m = m; - m.attrs.extend(syn::parse_str::(match *self { - Overflower::Wrap => "#[overflow(wrap)]", - Overflower::Panic => "#[overflow(panic)]", - Overflower::Saturate => "#[overflow(saturate)]", - Overflower::Default => "#[overflow(default)]" - }).unwrap().0); + m.attrs.extend( + syn::parse_str::(match *self { + Overflower::Wrap => "#[overflow(wrap)]", + Overflower::Panic => "#[overflow(panic)]", + Overflower::Saturate => "#[overflow(saturate)]", + Overflower::Default => "#[overflow(default)]", + }) + .unwrap() + .0, + ); Expr::Macro(m) } } impl Fold for Overflower { fn fold_impl_item_method(&mut self, i: ImplItemMethod) -> ImplItemMethod { - if self.is_overflow(&i.attrs) { return i; } + if self.is_overflow(&i.attrs) { + return i; + } fold::fold_impl_item_method(self, i) } fn fold_item_fn(&mut self, i: ItemFn) -> ItemFn { - if self.is_overflow(&i.attrs) { return i; } + if self.is_overflow(&i.attrs) { + return i; + } fold::fold_item_fn(self, i) } fn fold_item_impl(&mut self, i: ItemImpl) -> ItemImpl { - if self.is_overflow(&i.attrs) { return i; } + if self.is_overflow(&i.attrs) { + return i; + } fold::fold_item_impl(self, i) } fn fold_item_mod(&mut self, i: ItemMod) -> ItemMod { - if self.is_overflow(&i.attrs) { return i; } + if self.is_overflow(&i.attrs) { + return i; + } fold::fold_item_mod(self, i) } fn fold_item_trait(&mut self, i: ItemTrait) -> ItemTrait { - if self.is_overflow(&i.attrs) { return i; } + if self.is_overflow(&i.attrs) { + return i; + } fold::fold_item_trait(self, i) } fn fold_trait_item_method(&mut self, i: TraitItemMethod) -> TraitItemMethod { - if self.is_overflow(&i.attrs) { return i; } + if self.is_overflow(&i.attrs) { + return i; + } fold::fold_trait_item_method(self, i) } fn fold_expr(&mut self, e: Expr) -> Expr { macro_rules! foldexpr { ($s:expr, $ty:path, $t:ident, $f:path) => { - $ty(if self.is_overflow(& $t . attrs) { + $ty(if self.is_overflow(&$t.attrs) { $t } else { $f($s, $t) }) - } + }; } match e { Expr::Box(b) => foldexpr!(self, Expr::Box, b, fold::fold_expr_box), Expr::Array(a) => foldexpr!(self, Expr::Array, a, fold::fold_expr_array), Expr::Call(c) => self.make_call(c), - Expr::MethodCall(c) => foldexpr!(self, Expr::MethodCall, c, fold::fold_expr_method_call), + Expr::MethodCall(c) => { + foldexpr!(self, Expr::MethodCall, c, fold::fold_expr_method_call) + } Expr::Tuple(t) => foldexpr!(self, Expr::Tuple, t, fold::fold_expr_tuple), Expr::Binary(b) => self.make_binary(b), Expr::Unary(u) => self.make_unary(u), diff --git a/overflower/src/lib.rs b/overflower/src/lib.rs index 5896a94..376cbcd 100644 --- a/overflower/src/lib.rs +++ b/overflower/src/lib.rs @@ -20,8 +20,8 @@ #![cfg_attr(feature = "wrapping_int_impl", feature(wrapping_int_impl))] #![deny(missing_docs, unsafe_code)] -use std::ops::*; use std::cmp::*; +use std::ops::*; #[cfg(feature = "proc_macro")] pub use overflower_plugin::overflow; @@ -84,7 +84,10 @@ pub trait RemPanic { macro_rules! panic_biself { ($trait_name:ident, $trait_panic:ident, $fn_name:ident, $fn_panic:ident, $checked_fn:ident) => { #[cfg(feature = "specialization")] - impl $trait_panic for T where T: $trait_name { + impl $trait_panic for T + where + T: $trait_name, + { type Output = >::Output; default fn $fn_panic(self, rhs: R) -> Self::Output { std::ops::$trait_name::$fn_name(self, rhs) @@ -108,8 +111,11 @@ macro_rules! panic_biself { type Output = $ty; fn $fn_panic(self, rhs: $ty) -> $ty { - if let Some(x) = self.$checked_fn(rhs) { x } - else { panic!("arithmetic overflow") } + if let Some(x) = self.$checked_fn(rhs) { + x + } else { + panic!("arithmetic overflow") + } } } @@ -118,8 +124,11 @@ macro_rules! panic_biself { type Output = $ty; fn $fn_panic(self, rhs: $ty) -> $ty { - if let Some(x) = self.$checked_fn(rhs) { x } - else { panic!("arithmetic overflow") } + if let Some(x) = self.$checked_fn(rhs) { + x + } else { + panic!("arithmetic overflow") + } } } @@ -128,8 +137,11 @@ macro_rules! panic_biself { type Output = $ty; fn $fn_panic(self, rhs: &$ty) -> $ty { - if let Some(x) = self.$checked_fn(*rhs) { x } - else { panic!("arithmetic overflow") } + if let Some(x) = self.$checked_fn(*rhs) { + x + } else { + panic!("arithmetic overflow") + } } } @@ -138,11 +150,14 @@ macro_rules! panic_biself { type Output = $ty; fn $fn_panic(self, rhs: &$ty) -> $ty { - if let Some(x) = self.$checked_fn(*rhs) { x } - else { panic!("arithmetic overflow") } + if let Some(x) = self.$checked_fn(*rhs) { + x + } else { + panic!("arithmetic overflow") + } } } - } + }; } panic_biself!(Add, AddPanic, add, add_panic, checked_add); @@ -199,7 +214,10 @@ pub trait RemAssignPanic { macro_rules! panic_assign_biself { ($trait_name:ident, $trait_panic:ident, $fn_name:ident, $fn_panic:ident, $checked_fn:ident) => { #[cfg(feature = "specialization")] - impl $trait_panic for T where T: $trait_name { + impl $trait_panic for T + where + T: $trait_name, + { default fn $fn_panic(&mut self, rhs: R) { std::ops::$trait_name::$fn_name(self, rhs) } @@ -219,17 +237,51 @@ macro_rules! panic_assign_biself { ($trait_panic:ident, $fn_panic:ident, $checked_fn:ident, $ty:ty) => { impl $trait_panic<$ty> for $ty { fn $fn_panic(&mut self, rhs: $ty) { - *self = if let Some(x) = self.$checked_fn(rhs) { x } else { panic!("arithmetic overflow"); } + *self = if let Some(x) = self.$checked_fn(rhs) { + x + } else { + panic!("arithmetic overflow"); + } } } }; } -panic_assign_biself!(AddAssign, AddAssignPanic, add_assign, add_assign_panic, checked_add); -panic_assign_biself!(SubAssign, SubAssignPanic, sub_assign, sub_assign_panic, checked_sub); -panic_assign_biself!(MulAssign, MulAssignPanic, mul_assign, mul_assign_panic, checked_mul); -panic_assign_biself!(DivAssign, DivAssignPanic, div_assign, div_assign_panic, checked_div); -panic_assign_biself!(RemAssign, RemAssignPanic, rem_assign, rem_assign_panic, checked_rem); +panic_assign_biself!( + AddAssign, + AddAssignPanic, + add_assign, + add_assign_panic, + checked_add +); +panic_assign_biself!( + SubAssign, + SubAssignPanic, + sub_assign, + sub_assign_panic, + checked_sub +); +panic_assign_biself!( + MulAssign, + MulAssignPanic, + mul_assign, + mul_assign_panic, + checked_mul +); +panic_assign_biself!( + DivAssign, + DivAssignPanic, + div_assign, + div_assign_panic, + checked_div +); +panic_assign_biself!( + RemAssign, + RemAssignPanic, + rem_assign, + rem_assign_panic, + checked_rem +); /// Add two values, wrapping on overflow /// @@ -294,7 +346,10 @@ pub trait RemWrap { macro_rules! wrap_biself { ($trait_name:ident, $trait_wrap:ident, $fn_name:ident, $fn_wrap:ident, $wrapped_fn:ident) => { #[cfg(feature = "specialization")] - impl $trait_wrap for T where T: $trait_name { + impl $trait_wrap for T + where + T: $trait_name, + { type Output = >::Output; default fn $fn_wrap(self, rhs: R) -> Self::Output { std::ops::$trait_name::$fn_name(self, rhs) @@ -347,7 +402,7 @@ macro_rules! wrap_biself { self.$wrapped_fn(*rhs) } } - } + }; } wrap_biself!(Add, AddWrap, add, add_wrap, wrapping_add); @@ -405,7 +460,10 @@ pub trait RemAssignWrap { macro_rules! wrap_assign_biself { ($trait_name:ident, $trait_wrap:ident, $fn_name:ident, $fn_wrap:ident, $wrapped_fn:ident) => { #[cfg(feature = "specialization")] - impl $trait_wrap for T where T: $trait_name { + impl $trait_wrap for T + where + T: $trait_name, + { default fn $fn_wrap(&mut self, rhs: R) { std::ops::$trait_name::$fn_name(self, rhs) } @@ -428,14 +486,44 @@ macro_rules! wrap_assign_biself { *self = self.$wrapped_fn(rhs); } } - } + }; } -wrap_assign_biself!(AddAssign, AddAssignWrap, add_assign, add_assign_wrap, wrapping_add); -wrap_assign_biself!(SubAssign, SubAssignWrap, sub_assign, sub_assign_wrap, wrapping_sub); -wrap_assign_biself!(MulAssign, MulAssignWrap, mul_assign, mul_assign_wrap, wrapping_mul); -wrap_assign_biself!(DivAssign, DivAssignWrap, div_assign, div_assign_wrap, wrapping_div); -wrap_assign_biself!(RemAssign, RemAssignWrap, rem_assign, rem_assign_wrap, wrapping_rem); +wrap_assign_biself!( + AddAssign, + AddAssignWrap, + add_assign, + add_assign_wrap, + wrapping_add +); +wrap_assign_biself!( + SubAssign, + SubAssignWrap, + sub_assign, + sub_assign_wrap, + wrapping_sub +); +wrap_assign_biself!( + MulAssign, + MulAssignWrap, + mul_assign, + mul_assign_wrap, + wrapping_mul +); +wrap_assign_biself!( + DivAssign, + DivAssignWrap, + div_assign, + div_assign_wrap, + wrapping_div +); +wrap_assign_biself!( + RemAssign, + RemAssignWrap, + rem_assign, + rem_assign_wrap, + wrapping_rem +); //---- @@ -545,11 +633,13 @@ pub trait RemAssignSaturate { fn rem_assign_saturate(&mut self, rhs: RHS); } - macro_rules! saturate_biself { ($trait_name:ident, $trait_saturate:ident, $fn_name:ident, $fn_saturate:ident, $saturated_fn:ident) => { #[cfg(feature = "specialization")] - impl $trait_saturate for T where T: $trait_name { + impl $trait_saturate for T + where + T: $trait_name, + { type Output = >::Output; default fn $fn_saturate(self, rhs: R) -> Self::Output { std::ops::$trait_name::$fn_name(self, rhs) @@ -603,18 +693,20 @@ macro_rules! saturate_biself { self.$saturated_fn(*rhs) } } - } + }; } saturate_biself!(Add, AddSaturate, add, add_saturate, saturating_add); saturate_biself!(Sub, SubSaturate, sub, sub_saturate, saturating_sub); saturate_biself!(Mul, MulSaturate, mul, mul_saturate, saturating_mul); - macro_rules! saturate_assign_biself { ($trait_name:ident, $trait_saturate:ident, $fn_name:ident, $fn_saturate:ident, $saturated_fn:ident) => { #[cfg(feature = "specialization")] - impl $trait_saturate for T where T: $trait_name { + impl $trait_saturate for T + where + T: $trait_name, + { default fn $fn_saturate(&mut self, rhs: R) { std::ops::$trait_name::$fn_name(self, rhs) } @@ -637,16 +729,45 @@ macro_rules! saturate_assign_biself { *self = self.$saturated_fn(rhs); } } - } + }; } -saturate_assign_biself!(AddAssign, AddAssignSaturate, add_assign, add_assign_saturate, saturating_add); -saturate_assign_biself!(SubAssign, SubAssignSaturate, sub_assign, sub_assign_saturate, saturating_sub); -saturate_assign_biself!(MulAssign, MulAssignSaturate, mul_assign, mul_assign_saturate, saturating_mul); +saturate_assign_biself!( + AddAssign, + AddAssignSaturate, + add_assign, + add_assign_saturate, + saturating_add +); +saturate_assign_biself!( + SubAssign, + SubAssignSaturate, + sub_assign, + sub_assign_saturate, + saturating_sub +); +saturate_assign_biself!( + MulAssign, + MulAssignSaturate, + mul_assign, + mul_assign_saturate, + saturating_mul +); // TODO: by zero → min/max value depending on sign -saturate_assign_biself!(DivAssign, DivAssignSaturate, div_assign, div_assign_saturate, div); -saturate_assign_biself!(RemAssign, RemAssignSaturate, rem_assign, rem_assign_saturate, rem); - +saturate_assign_biself!( + DivAssign, + DivAssignSaturate, + div_assign, + div_assign_saturate, + div +); +saturate_assign_biself!( + RemAssign, + RemAssignSaturate, + rem_assign, + rem_assign_saturate, + rem +); #[cfg(feature = "specialization")] impl> DivSaturate for T { @@ -676,10 +797,16 @@ macro_rules! saturate_signed { 0 => match self.cmp(&0) { Ordering::Greater => $max, Ordering::Equal => 0, - Ordering::Less => $min + Ordering::Less => $min, }, - -1 => if self == $min { $max } else { -self }, - _ => self / rhs + -1 => { + if self == $min { + $max + } else { + -self + } + } + _ => self / rhs, } } } @@ -689,8 +816,15 @@ macro_rules! saturate_signed { type Output = $ty; fn rem_saturate(self, rhs: $ty) -> $ty { - if rhs == 0 { if self == 0 { 0 } else { $max } - } else { self % rhs } + if rhs == 0 { + if self == 0 { + 0 + } else { + $max + } + } else { + self % rhs + } } } }; @@ -705,8 +839,14 @@ macro_rules! saturate_unsigned { fn div_saturate(self, rhs: $ty) -> $ty { match rhs { - 0 => if self == 0 { 0 } else { $max }, - _ => self / rhs + 0 => { + if self == 0 { + 0 + } else { + $max + } + } + _ => self / rhs, } } } @@ -716,22 +856,29 @@ macro_rules! saturate_unsigned { type Output = $ty; fn rem_saturate(self, rhs: $ty) -> $ty { - if rhs == 0 { if self == 0 { 0 } else { $max } - } else { self % rhs } + if rhs == 0 { + if self == 0 { + 0 + } else { + $max + } + } else { + self % rhs + } } } }; } -saturate_unsigned!(u8, std::u8::MAX); -saturate_unsigned!(u16, std::u16::MAX); -saturate_unsigned!(u32, std::u32::MAX); -saturate_unsigned!(u64, std::u64::MAX); +saturate_unsigned!(u8, std::u8::MAX); +saturate_unsigned!(u16, std::u16::MAX); +saturate_unsigned!(u32, std::u32::MAX); +saturate_unsigned!(u64, std::u64::MAX); saturate_unsigned!(usize, std::usize::MAX); -saturate_signed!(i8, std::i8::MIN, std::i8::MAX); -saturate_signed!(i16, std::i16::MIN, std::i16::MAX); -saturate_signed!(i32, std::i32::MIN, std::i32::MAX); -saturate_signed!(i64, std::i64::MIN, std::i64::MAX); +saturate_signed!(i8, std::i8::MIN, std::i8::MAX); +saturate_signed!(i16, std::i16::MIN, std::i16::MAX); +saturate_signed!(i32, std::i32::MIN, std::i32::MAX); +saturate_signed!(i64, std::i64::MIN, std::i64::MAX); saturate_signed!(isize, std::isize::MIN, std::isize::MAX); /// Shift right, panic if the number of bits shifted are higher than the width @@ -739,7 +886,7 @@ saturate_signed!(isize, std::isize::MIN, std::isize::MAX); /// /// This does the same as the `std::ops::Shr` trait for most types. /// it is specialized for integer types to panic on over- or underflow. -pub trait ShrPanic { +pub trait ShrPanic { /// THe output type of the shift operation type Output; @@ -753,7 +900,7 @@ pub trait ShrPanic { /// /// This does the same as the `std::ops::ShrAssign` trait for most types. /// it is specialized for integer types to panic on over- or underflow. -pub trait ShrAssignPanic { +pub trait ShrAssignPanic { /// shift right in place, panic if the number of bits shifted are higher /// than the width of the type fn shr_assign_panic(&mut self, rhs: RHS); @@ -770,7 +917,10 @@ macro_rules! panic_shifts { $fn_assign_panic:ident, $checked_fn:ident) => { #[cfg(feature = "specialization")] - impl $trait_panic for T where T: $trait_name { + impl $trait_panic for T + where + T: $trait_name, + { type Output = >::Output; default fn $fn_panic(self, rhs: R) -> Self::Output { std::ops::$trait_name::$fn_name(self, rhs) @@ -778,34 +928,187 @@ macro_rules! panic_shifts { } #[cfg(feature = "specialization")] - impl $trait_assign_panic for T where T: $trait_assign_name { + impl $trait_assign_panic for T + where + T: $trait_assign_name, + { default fn $fn_assign_panic(&mut self, rhs: R) { $trait_assign_name::$fn_assign_name(self, rhs) } } - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, u8); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, u16); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, u32); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, u64); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, usize); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, i8); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, i16); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, i32); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, i64); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, isize); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + u8 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + u16 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + u32 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + u64 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + usize + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + i8 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + i16 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + i32 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + i64 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + isize + ); }; ($trait_panic:ident, $trait_assign_panic:ident, $fn_panic:ident, $fn_assign_panic:ident, $checked_fn:ident, $ty:ty) => { - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, $ty, u8); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, $ty, u16); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, $ty, u32); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, $ty, u64); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, $ty, usize); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, $ty, i8); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, $ty, i16); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, $ty, i32); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, $ty, i64); - panic_shifts!($trait_panic, $trait_assign_panic, $fn_panic, $fn_assign_panic, $checked_fn, $ty, isize); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + $ty, + u8 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + $ty, + u16 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + $ty, + u32 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + $ty, + u64 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + $ty, + usize + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + $ty, + i8 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + $ty, + i16 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + $ty, + i32 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + $ty, + i64 + ); + panic_shifts!( + $trait_panic, + $trait_assign_panic, + $fn_panic, + $fn_assign_panic, + $checked_fn, + $ty, + isize + ); }; ($trait_panic:ident, $trait_assign_panic:ident, $fn_panic:ident, $fn_assign_panic:ident, $checked_fn:ident, $ty:ty, $rty:ty) => { impl $trait_panic<$rty> for $ty { @@ -813,18 +1116,24 @@ macro_rules! panic_shifts { type Output = $ty; fn $fn_panic(self, rhs: $rty) -> Self::Output { - if let Some(x) = self.$checked_fn(rhs as u32) { x } else - { panic!("Arithmetic overflow") } + if let Some(x) = self.$checked_fn(rhs as u32) { + x + } else { + panic!("Arithmetic overflow") + } } } impl $trait_assign_panic<$rty> for $ty { fn $fn_assign_panic(&mut self, rhs: $rty) { - *self = if let Some(x) = self.$checked_fn(rhs as u32) { x } else - { panic!("Arithmetic overflow") } + *self = if let Some(x) = self.$checked_fn(rhs as u32) { + x + } else { + panic!("Arithmetic overflow") + } } } - } + }; } panic_shifts!(@Shr, ShrAssign, ShrPanic, ShrAssignPanic, shr, shr_assign, shr_panic, shr_assign_panic, checked_shr); @@ -834,7 +1143,7 @@ panic_shifts!(@Shr, ShrAssign, ShrPanic, ShrAssignPanic, shr, shr_assign, shr_pa /// /// This does the same as the `std::ops::Shl` trait for most types. /// it is specialized for integer types to return zero on over- or underflow. -pub trait ShlWrap { +pub trait ShlWrap { /// the return type of our shift operation type Output; /// shift left, return 0 if the number of bits shifted are higher than the @@ -847,7 +1156,7 @@ pub trait ShlWrap { /// /// This does the same as the `std::ops::ShlAssign` trait for most types. /// it is specialized for integer types to return zero on over- or underflow. -pub trait ShlAssignWrap { +pub trait ShlAssignWrap { /// Shift left in place, set self to 0 if the number of bits shifted are /// higher than the width of the type fn shl_assign_wrap(&mut self, rhs: RHS); @@ -858,7 +1167,7 @@ pub trait ShlAssignWrap { /// /// This does the same as the `std::ops::Shr` trait for most types. /// it is specialized for integer types to return zero on over- or underflow. -pub trait ShrWrap { +pub trait ShrWrap { /// the return type of our shift operation type Output; /// shift right, return 0 if the number of bits shifted are higher than the @@ -871,7 +1180,7 @@ pub trait ShrWrap { /// /// This does the same as the `std::ops::ShrAssign` trait for most types. /// it is specialized for integer types to return zero on over- or underflow. -pub trait ShrAssignWrap { +pub trait ShrAssignWrap { /// Shift right in place, set self to 0 if the number of bits shifted are /// higher than the width of the type fn shr_assign_wrap(&mut self, rhs: RHS); @@ -880,7 +1189,10 @@ pub trait ShrAssignWrap { macro_rules! wrap_shifts { (@$trait_name:ident, $trait_assign_name:ident, $trait_wrap:ident, $trait_assign_wrap:ident, $fn_name:ident, $fn_assign_name:ident, $fn_wrap:ident, $fn_assign_wrap:ident, $wrapping_fn:ident) => { #[cfg(feature = "specialization")] - impl $trait_wrap for T where T: $trait_name { + impl $trait_wrap for T + where + T: $trait_name, + { type Output = >::Output; default fn $fn_wrap(self, rhs: R) -> Self::Output { std::ops::$trait_name::$fn_name(self, rhs) @@ -888,34 +1200,187 @@ macro_rules! wrap_shifts { } #[cfg(feature = "specialization")] - impl $trait_assign_wrap for T where T: $trait_assign_name { + impl $trait_assign_wrap for T + where + T: $trait_assign_name, + { default fn $fn_assign_wrap(&mut self, rhs: R) { $trait_assign_name::$fn_assign_name(self, rhs) } } - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, u8); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, u16); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, u32); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, u64); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, usize); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, i8); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, i16); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, i32); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, i64); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, isize); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + u8 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + u16 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + u32 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + u64 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + usize + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + i8 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + i16 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + i32 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + i64 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + isize + ); }; ($trait_wrap:ident, $trait_assign_wrap:ident, $fn_wrap:ident, $fn_assign_wrap:ident, $wrapping_fn:ident, $ty:ty) => { - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, $ty, u8); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, $ty, u16); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, $ty, u32); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, $ty, u64); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, $ty, usize); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, $ty, i8); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, $ty, i16); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, $ty, i32); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, $ty, i64); - wrap_shifts!($trait_wrap, $trait_assign_wrap, $fn_wrap, $fn_assign_wrap, $wrapping_fn, $ty, isize); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + $ty, + u8 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + $ty, + u16 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + $ty, + u32 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + $ty, + u64 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + $ty, + usize + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + $ty, + i8 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + $ty, + i16 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + $ty, + i32 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + $ty, + i64 + ); + wrap_shifts!( + $trait_wrap, + $trait_assign_wrap, + $fn_wrap, + $fn_assign_wrap, + $wrapping_fn, + $ty, + isize + ); }; ($trait_wrap:ident, $trait_assign_wrap:ident, $fn_wrap:ident, $fn_assign_wrap:ident, $wrapping_fn:ident, $ty:ty, $rty:ty) => { impl $trait_wrap<$rty> for $ty { @@ -932,7 +1397,7 @@ macro_rules! wrap_shifts { *self = self.$wrapping_fn(rhs as u32) } } - } + }; } wrap_shifts!(@Shl, ShlAssign, ShlWrap, ShlAssignWrap, shl, shl_assign, shl_wrap, shl_assign_wrap, wrapping_shl); @@ -943,7 +1408,7 @@ wrap_shifts!(@Shr, ShrAssign, ShrWrap, ShrAssignWrap, shr, shr_assign, shr_wrap, /// /// This does the same as the `std::ops::Shr` trait for most types. /// it is specialized for integer types to return zero on over- or underflow. -pub trait ShrSaturate { +pub trait ShrSaturate { /// the return type of our shift operation type Output; /// shift right, return 0 if the number of bits shifted are higher than the @@ -951,13 +1416,12 @@ pub trait ShrSaturate { fn shr_saturate(self, rhs: RHS) -> Self::Output; } - /// Shift right in place, set self to 0 if the number of bits shifted are /// higher than the width of the type /// /// This does the same as the `std::ops::ShrAssign` trait for most types. /// it is specialized for integer types to return zero on over- or underflow. -pub trait ShrSaturateAssign { +pub trait ShrSaturateAssign { /// shift right in place, set self to 0 if the number of bits shifted are /// higher than the width of the type fn shr_assign_saturate(&mut self, rhs: RHS); @@ -966,12 +1430,16 @@ pub trait ShrSaturateAssign { #[cfg(feature = "specialization")] impl> ShrSaturate for T { type Output = >::Output; - default fn shr_saturate(self, rhs: R) -> Self::Output { self >> rhs } + default fn shr_saturate(self, rhs: R) -> Self::Output { + self >> rhs + } } #[cfg(feature = "specialization")] impl> ShrSaturateAssign for T { - default fn shr_assign_saturate(&mut self, rhs: R) { *self >>= rhs; } + default fn shr_assign_saturate(&mut self, rhs: R) { + *self >>= rhs; + } } /// Shift left, return 0 if the number of bits shifted are higher than the @@ -979,7 +1447,7 @@ impl> ShrSaturateAssign for T { /// /// This does the same as the `std::ops::Shl` trait for most types. /// it is specialized for integer types to return zero on over- or underflow. -pub trait ShlSaturate { +pub trait ShlSaturate { /// the return type of our shift operation type Output; /// shift left, return 0 if the number of bits shifted are higher than the @@ -992,7 +1460,7 @@ pub trait ShlSaturate { /// /// This does the same as the `std::ops::ShlAssign` trait for most types. /// it is specialized for integer types to return zero on over- or underflow. -pub trait ShlAssignSaturate { +pub trait ShlAssignSaturate { /// shift left in place, set self to 0 if the number of bits shifted are /// higher than the width of the type fn shl_assign_saturate(&mut self, rhs: RHS); @@ -1001,19 +1469,23 @@ pub trait ShlAssignSaturate { #[cfg(feature = "specialization")] impl> ShlSaturate for T { type Output = >::Output; - default fn shl_saturate(self, rhs: R) -> Self::Output { self << rhs } + default fn shl_saturate(self, rhs: R) -> Self::Output { + self << rhs + } } #[cfg(feature = "specialization")] impl> ShlAssignSaturate for T { - default fn shl_assign_saturate(&mut self, rhs: R) { *self <<= rhs; } + default fn shl_assign_saturate(&mut self, rhs: R) { + *self <<= rhs; + } } /// Shift left, panic if bits are shifted out of the value /// /// This does the same as the `std::ops::Shl` trait for most types. /// it is specialized for integer types to panic on over- or underflow. -pub trait ShlPanic { +pub trait ShlPanic { /// the result type of our left shift type Output; @@ -1022,7 +1494,10 @@ pub trait ShlPanic { } #[cfg(feature = "specialization")] -impl ShlPanic for T where T: Shl { +impl ShlPanic for T +where + T: Shl, +{ type Output = >::Output; default fn shl_panic(self, rhs: R) -> Self::Output { std::ops::Shl::shl(self, rhs) @@ -1033,13 +1508,16 @@ impl ShlPanic for T where T: Shl { /// /// This does the same as the `std::ops::Shl` trait for most types. /// it is specialized for integer types to panic on over- or underflow. -pub trait ShlAssignPanic { +pub trait ShlAssignPanic { /// Shift left in place, panic if bits are shifted out of the value fn shl_assign_panic(&mut self, rhs: RHS); } #[cfg(feature = "specialization")] -impl ShlAssignPanic for T where T: ShlAssign { +impl ShlAssignPanic for T +where + T: ShlAssign, +{ default fn shl_assign_panic(&mut self, rhs: R) { ShlAssign::shl_assign(self, rhs) } @@ -1050,7 +1528,7 @@ impl ShlAssignPanic for T where T: ShlAssign { /// /// This does the same as the `std::ops::ShlAssign` trait for most types. /// it is specialized for integer types to return zero on over- or underflow. -pub trait ShrAssignSaturate { +pub trait ShrAssignSaturate { /// shift right in place, set self to 0 if the number of bits shifted are /// higher than the width of the type fn shr_assign_saturate(&mut self, rhs: RHS); @@ -1075,7 +1553,9 @@ macro_rules! saturate_shl_unsigned { type Output = $ty; fn shl_saturate(self, rhs: $rty) -> Self::Output { - if self == 0 { return 0; } + if self == 0 { + return 0; + } if rhs as usize >= $bits || ((!0) >> rhs) < self { $max } else { @@ -1089,13 +1569,19 @@ macro_rules! saturate_shl_unsigned { type Output = $ty; fn shr_saturate(self, rhs: $rty) -> Self::Output { - if rhs as usize >= $bits { 0 } else { self >> rhs } + if rhs as usize >= $bits { + 0 + } else { + self >> rhs + } } } impl ShlAssignSaturate<$rty> for $ty { fn shl_assign_saturate(&mut self, rhs: $rty) { - if *self == 0 { return; } + if *self == 0 { + return; + } *self = if rhs as usize >= $bits || (!0) >> rhs < *self { $max } else { @@ -1115,7 +1601,9 @@ macro_rules! saturate_shl_unsigned { type Output = $ty; fn shl_panic(self, rhs: $rty) -> Self::Output { - if self == 0 { return 0; } + if self == 0 { + return 0; + } if (rhs as usize >= $bits || ((!0) >> rhs) < self) && self != 0 { panic!("Arithmetic overflow"); } @@ -1125,7 +1613,9 @@ macro_rules! saturate_shl_unsigned { impl ShlAssignPanic<$rty> for $ty { fn shl_assign_panic(&mut self, rhs: $rty) { - if *self == 0 { return; } + if *self == 0 { + return; + } *self = if rhs as usize >= $bits || (!0) >> rhs < *self { panic!("Arithmetic overflow"); } else { @@ -1173,10 +1663,18 @@ macro_rules! saturate_shl_signed { match self.cmp(&0) { Ordering::Equal => 0, Ordering::Greater => { - if rhs as usize >= $bits || ($max >> rhs) < self { $max } else { self << rhs } + if rhs as usize >= $bits || ($max >> rhs) < self { + $max + } else { + self << rhs + } } Ordering::Less => { - if rhs as usize >= $bits || ($min >> rhs) > self { $min } else { self << rhs } + if rhs as usize >= $bits || ($min >> rhs) > self { + $min + } else { + self << rhs + } } } } @@ -1187,7 +1685,11 @@ macro_rules! saturate_shl_signed { type Output = $ty; fn shr_saturate(self, rhs: $rty) -> Self::Output { - if rhs as usize >= $bits { 0 } else { self >> rhs } + if rhs as usize >= $bits { + 0 + } else { + self >> rhs + } } } @@ -1197,10 +1699,18 @@ macro_rules! saturate_shl_signed { *self = match s.cmp(&0) { Ordering::Equal => 0, Ordering::Greater => { - if rhs as usize >= $bits || ($max >> rhs) < s { $max } else { s << rhs } + if rhs as usize >= $bits || ($max >> rhs) < s { + $max + } else { + s << rhs + } } Ordering::Less => { - if rhs as usize >= $bits || ($min >> rhs) > s { $min } else { s << rhs } + if rhs as usize >= $bits || ($min >> rhs) > s { + $min + } else { + s << rhs + } } } } @@ -1220,10 +1730,14 @@ macro_rules! saturate_shl_signed { match self.cmp(&0) { Ordering::Equal => return 0, Ordering::Greater => { - if rhs as usize >= $bits || ($max >> rhs) < self { panic!("Arithmetic overflow"); } + if rhs as usize >= $bits || ($max >> rhs) < self { + panic!("Arithmetic overflow"); + } } Ordering::Less => { - if rhs as usize >= $bits || ($min >> rhs) > self { panic!("Arithmetic overflow"); } + if rhs as usize >= $bits || ($min >> rhs) > self { + panic!("Arithmetic overflow"); + } } } self << rhs @@ -1236,10 +1750,14 @@ macro_rules! saturate_shl_signed { match s.cmp(&0) { Ordering::Equal => return, Ordering::Greater => { - if rhs as usize >= $bits || ($max >> rhs) < s { panic!("Arithmetic overflow"); } + if rhs as usize >= $bits || ($max >> rhs) < s { + panic!("Arithmetic overflow"); + } } Ordering::Less => { - if rhs as usize >= $bits || ($min >> rhs) > s { panic!("Arithmetic overflow"); } + if rhs as usize >= $bits || ($min >> rhs) > s { + panic!("Arithmetic overflow"); + } } } *self <<= rhs @@ -1257,7 +1775,6 @@ const ISIZE_BITS: usize = 31; #[cfg(target_pointer_width = "64")] const ISIZE_BITS: usize = 63; - saturate_shl_signed!(i8, std::i8::MAX, std::i8::MIN, 7); saturate_shl_signed!(i16, std::i16::MAX, std::i16::MIN, 15); saturate_shl_signed!(i32, std::i32::MAX, std::i32::MIN, 31); @@ -1276,7 +1793,10 @@ pub trait NegPanic { } #[cfg(feature = "specialization")] -impl NegPanic for T where T: Neg { +impl NegPanic for T +where + T: Neg, +{ type Output = ::Output; default fn neg_panic(self) -> Self::Output { -self @@ -1290,11 +1810,14 @@ macro_rules! neg_panic { type Output = $ty; fn neg_panic(self) -> Self::Output { - if let Some(x) = self.checked_neg() { x } - else { panic!("arithmetic overflow") } + if let Some(x) = self.checked_neg() { + x + } else { + panic!("arithmetic overflow") + } } } - } + }; } neg_panic!(i8); @@ -1315,7 +1838,10 @@ pub trait NegWrap { } #[cfg(feature = "specialization")] -impl NegWrap for T where T: Neg { +impl NegWrap for T +where + T: Neg, +{ type Output = ::Output; default fn neg_wrap(self) -> Self::Output { -self @@ -1332,7 +1858,7 @@ macro_rules! neg_wrap { self.wrapping_neg() } } - } + }; } neg_wrap!(i8); @@ -1358,7 +1884,11 @@ macro_rules! neg_saturate { type Output = Self; fn neg_saturate(self) -> Self { - if self == $min { $max } else { -self } + if self == $min { + $max + } else { + -self + } } } }; @@ -1427,19 +1957,31 @@ macro_rules! abs_signed { ($ty:ty) => { impl AbsPanic for $ty { fn abs_panic(self) -> Self { - if self < 0 { 0.sub_panic(self) } else { self } + if self < 0 { + 0.sub_panic(self) + } else { + self + } } } impl AbsWrap for $ty { fn abs_wrap(self) -> Self { - if self < 0 { 0.sub_wrap(self) } else { self } + if self < 0 { + 0.sub_wrap(self) + } else { + self + } } } impl AbsSaturate for $ty { fn abs_saturate(self) -> Self { - if self < 0 { 0.sub_saturate(self) } else { self } + if self < 0 { + 0.sub_saturate(self) + } else { + self + } } } }; @@ -1684,5 +2226,4 @@ mod wrapping_int_impls { impls!(core::num::Wrapping; abs); impls!(core::num::Wrapping; abs); impls!(core::num::Wrapping; abs); - } diff --git a/overflower/tests/macro.rs b/overflower/tests/macro.rs index 82fd172..fb8f41b 100644 --- a/overflower/tests/macro.rs +++ b/overflower/tests/macro.rs @@ -4,7 +4,9 @@ use overflower::overflow; macro_rules! id { - ($x:expr) => { $x }; + ($x:expr) => { + $x + }; } #[test] diff --git a/overflower/tests/quickcheck.rs b/overflower/tests/quickcheck.rs index 1fdb8a8..2d229a4 100644 --- a/overflower/tests/quickcheck.rs +++ b/overflower/tests/quickcheck.rs @@ -1,17 +1,18 @@ +use overflower::*; +use quickcheck::quickcheck; use std::cmp::Ordering; use std::panic::{self, catch_unwind}; use std::sync::Once; -use quickcheck::quickcheck; -use overflower::*; -static HANDLER : Once = Once::new(); +static HANDLER: Once = Once::new(); fn install_handler() { HANDLER.call_once(|| { let p = panic::take_hook(); - panic::set_hook(Box::new(move|info| { - if info.location().map_or(false, |l| l.file() != "src/lib.rs" && - !l.file().ends_with("/num/mod.rs")) { + panic::set_hook(Box::new(move |info| { + if info.location().map_or(false, |l| { + l.file() != "src/lib.rs" && !l.file().ends_with("/num/mod.rs") + }) { p(info); } })); @@ -24,8 +25,7 @@ macro_rules! test_add_panic { fn $name() { fn check(args: ($ty, $ty)) -> bool { let expected = args.0.checked_add(args.1); - let actual = catch_unwind( - || AddPanic::add_panic(args.0, args.1)).ok(); + let actual = catch_unwind(|| AddPanic::add_panic(args.0, args.1)).ok(); expected == actual } install_handler(); @@ -38,12 +38,12 @@ test_add_panic!(usize, test_add_panic_usize); test_add_panic!(u64, test_add_panic_u64); test_add_panic!(u32, test_add_panic_u32); test_add_panic!(u16, test_add_panic_u16); -test_add_panic!(u8, test_add_panic_u8); +test_add_panic!(u8, test_add_panic_u8); test_add_panic!(isize, test_add_panic_isize); test_add_panic!(i64, test_add_panic_i64); test_add_panic!(i32, test_add_panic_i32); test_add_panic!(i16, test_add_panic_i16); -test_add_panic!(i8, test_add_panic_i8); +test_add_panic!(i8, test_add_panic_i8); macro_rules! test_add_wrap { ($ty:ty, $name:ident) => { @@ -64,12 +64,12 @@ test_add_wrap!(usize, test_add_wrap_usize); test_add_wrap!(u64, test_add_wrap_u64); test_add_wrap!(u32, test_add_wrap_u32); test_add_wrap!(u16, test_add_wrap_u16); -test_add_wrap!(u8, test_add_wrap_u8); +test_add_wrap!(u8, test_add_wrap_u8); test_add_wrap!(isize, test_add_wrap_isize); test_add_wrap!(i64, test_add_wrap_i64); test_add_wrap!(i32, test_add_wrap_i32); test_add_wrap!(i16, test_add_wrap_i16); -test_add_wrap!(i8, test_add_wrap_i8); +test_add_wrap!(i8, test_add_wrap_i8); macro_rules! test_add_saturate { ($ty:ty, $name:ident) => { @@ -90,12 +90,12 @@ test_add_saturate!(usize, test_add_saturate_usize); test_add_saturate!(u64, test_add_saturate_u64); test_add_saturate!(u32, test_add_saturate_u32); test_add_saturate!(u16, test_add_saturate_u16); -test_add_saturate!(u8, test_add_saturate_u8); +test_add_saturate!(u8, test_add_saturate_u8); test_add_saturate!(isize, test_add_saturate_isize); test_add_saturate!(i64, test_add_saturate_i64); test_add_saturate!(i32, test_add_saturate_i32); test_add_saturate!(i16, test_add_saturate_i16); -test_add_saturate!(i8, test_add_saturate_i8); +test_add_saturate!(i8, test_add_saturate_i8); macro_rules! test_sub_panic { ($ty:ty, $name:ident) => { @@ -103,8 +103,7 @@ macro_rules! test_sub_panic { fn $name() { fn check(args: ($ty, $ty)) -> bool { let expected = args.0.checked_sub(args.1); - let actual = catch_unwind( - || SubPanic::sub_panic(args.0, args.1)).ok(); + let actual = catch_unwind(|| SubPanic::sub_panic(args.0, args.1)).ok(); expected == actual } install_handler(); @@ -117,12 +116,12 @@ test_sub_panic!(usize, test_sub_panic_usize); test_sub_panic!(u64, test_sub_panic_u64); test_sub_panic!(u32, test_sub_panic_u32); test_sub_panic!(u16, test_sub_panic_u16); -test_sub_panic!(u8, test_sub_panic_u8); +test_sub_panic!(u8, test_sub_panic_u8); test_sub_panic!(isize, test_sub_panic_isize); test_sub_panic!(i64, test_sub_panic_i64); test_sub_panic!(i32, test_sub_panic_i32); test_sub_panic!(i16, test_sub_panic_i16); -test_sub_panic!(i8, test_sub_panic_i8); +test_sub_panic!(i8, test_sub_panic_i8); macro_rules! test_sub_wrap { ($ty:ty, $name:ident) => { @@ -143,12 +142,12 @@ test_sub_wrap!(usize, test_sub_wrap_usize); test_sub_wrap!(u64, test_sub_wrap_u64); test_sub_wrap!(u32, test_sub_wrap_u32); test_sub_wrap!(u16, test_sub_wrap_u16); -test_sub_wrap!(u8, test_sub_wrap_u8); +test_sub_wrap!(u8, test_sub_wrap_u8); test_sub_wrap!(isize, test_sub_wrap_isize); test_sub_wrap!(i64, test_sub_wrap_i64); test_sub_wrap!(i32, test_sub_wrap_i32); test_sub_wrap!(i16, test_sub_wrap_i16); -test_sub_wrap!(i8, test_sub_wrap_i8); +test_sub_wrap!(i8, test_sub_wrap_i8); macro_rules! test_sub_saturate { ($ty:ty, $name:ident) => { @@ -169,12 +168,12 @@ test_sub_saturate!(usize, test_sub_saturate_usize); test_sub_saturate!(u64, test_sub_saturate_u64); test_sub_saturate!(u32, test_sub_saturate_u32); test_sub_saturate!(u16, test_sub_saturate_u16); -test_sub_saturate!(u8, test_sub_saturate_u8); +test_sub_saturate!(u8, test_sub_saturate_u8); test_sub_saturate!(isize, test_sub_saturate_isize); test_sub_saturate!(i64, test_sub_saturate_i64); test_sub_saturate!(i32, test_sub_saturate_i32); test_sub_saturate!(i16, test_sub_saturate_i16); -test_sub_saturate!(i8, test_sub_saturate_i8); +test_sub_saturate!(i8, test_sub_saturate_i8); macro_rules! test_mul_panic { ($ty:ty, $name:ident) => { @@ -182,8 +181,7 @@ macro_rules! test_mul_panic { fn $name() { fn check(args: ($ty, $ty)) -> bool { let expected = args.0.checked_mul(args.1); - let actual = catch_unwind( - || MulPanic::mul_panic(args.0, args.1)).ok(); + let actual = catch_unwind(|| MulPanic::mul_panic(args.0, args.1)).ok(); expected == actual } install_handler(); @@ -196,12 +194,12 @@ test_mul_panic!(usize, test_mul_panic_usize); test_mul_panic!(u64, test_mul_panic_u64); test_mul_panic!(u32, test_mul_panic_u32); test_mul_panic!(u16, test_mul_panic_u16); -test_mul_panic!(u8, test_mul_panic_u8); +test_mul_panic!(u8, test_mul_panic_u8); test_mul_panic!(isize, test_mul_panic_isize); test_mul_panic!(i64, test_mul_panic_i64); test_mul_panic!(i32, test_mul_panic_i32); test_mul_panic!(i16, test_mul_panic_i16); -test_mul_panic!(i8, test_mul_panic_i8); +test_mul_panic!(i8, test_mul_panic_i8); macro_rules! test_mul_wrap { ($ty:ty, $name:ident) => { @@ -222,12 +220,12 @@ test_mul_wrap!(usize, test_mul_wrap_usize); test_mul_wrap!(u64, test_mul_wrap_u64); test_mul_wrap!(u32, test_mul_wrap_u32); test_mul_wrap!(u16, test_mul_wrap_u16); -test_mul_wrap!(u8, test_mul_wrap_u8); +test_mul_wrap!(u8, test_mul_wrap_u8); test_mul_wrap!(isize, test_mul_wrap_isize); test_mul_wrap!(i64, test_mul_wrap_i64); test_mul_wrap!(i32, test_mul_wrap_i32); test_mul_wrap!(i16, test_mul_wrap_i16); -test_mul_wrap!(i8, test_mul_wrap_i8); +test_mul_wrap!(i8, test_mul_wrap_i8); macro_rules! test_mul_saturate { ($ty:ty, $name:ident) => { @@ -248,12 +246,12 @@ test_mul_saturate!(usize, test_mul_saturate_usize); test_mul_saturate!(u64, test_mul_saturate_u64); test_mul_saturate!(u32, test_mul_saturate_u32); test_mul_saturate!(u16, test_mul_saturate_u16); -test_mul_saturate!(u8, test_mul_saturate_u8); +test_mul_saturate!(u8, test_mul_saturate_u8); test_mul_saturate!(isize, test_mul_saturate_isize); test_mul_saturate!(i64, test_mul_saturate_i64); test_mul_saturate!(i32, test_mul_saturate_i32); test_mul_saturate!(i16, test_mul_saturate_i16); -test_mul_saturate!(i8, test_mul_saturate_i8); +test_mul_saturate!(i8, test_mul_saturate_i8); macro_rules! test_div_panic { ($ty:ty, $name:ident) => { @@ -261,8 +259,7 @@ macro_rules! test_div_panic { fn $name() { fn check(args: ($ty, $ty)) -> bool { let expected = args.0.checked_div(args.1); - let actual = catch_unwind( - || DivPanic::div_panic(args.0, args.1)).ok(); + let actual = catch_unwind(|| DivPanic::div_panic(args.0, args.1)).ok(); expected == actual } install_handler(); @@ -275,12 +272,12 @@ test_div_panic!(usize, test_div_panic_usize); test_div_panic!(u64, test_div_panic_u64); test_div_panic!(u32, test_div_panic_u32); test_div_panic!(u16, test_div_panic_u16); -test_div_panic!(u8, test_div_panic_u8); +test_div_panic!(u8, test_div_panic_u8); test_div_panic!(isize, test_div_panic_isize); test_div_panic!(i64, test_div_panic_i64); test_div_panic!(i32, test_div_panic_i32); test_div_panic!(i16, test_div_panic_i16); -test_div_panic!(i8, test_div_panic_i8); +test_div_panic!(i8, test_div_panic_i8); macro_rules! test_div_wrap { ($ty:ty, $name:ident) => { @@ -288,8 +285,7 @@ macro_rules! test_div_wrap { fn $name() { fn check(args: ($ty, $ty)) -> bool { let expected = args.0.checked_div(args.1); - let actual = catch_unwind( - || DivWrap::div_wrap(args.0, args.1)).ok(); + let actual = catch_unwind(|| DivWrap::div_wrap(args.0, args.1)).ok(); expected == actual } install_handler(); @@ -302,12 +298,12 @@ test_div_wrap!(usize, test_div_wrap_usize); test_div_wrap!(u64, test_div_wrap_u64); test_div_wrap!(u32, test_div_wrap_u32); test_div_wrap!(u16, test_div_wrap_u16); -test_div_wrap!(u8, test_div_wrap_u8); +test_div_wrap!(u8, test_div_wrap_u8); test_div_wrap!(isize, test_div_wrap_isize); test_div_wrap!(i64, test_div_wrap_i64); test_div_wrap!(i32, test_div_wrap_i32); test_div_wrap!(i16, test_div_wrap_i16); -test_div_wrap!(i8, test_div_wrap_i8); +test_div_wrap!(i8, test_div_wrap_i8); macro_rules! test_div_saturate { ($ty:ty, $name:ident, $max:expr) => { @@ -315,8 +311,14 @@ macro_rules! test_div_saturate { fn $name() { fn check(args: ($ty, $ty)) -> bool { let expected = if args.1 == 0 { - if args.0 == 0 { 0 } else { $max } - } else { args.0 / args.1 }; + if args.0 == 0 { + 0 + } else { + $max + } + } else { + args.0 / args.1 + }; let actual = DivSaturate::div_saturate(args.0, args.1); expected == actual } @@ -330,7 +332,7 @@ test_div_saturate!(usize, test_div_saturate_usize, std::usize::MAX); test_div_saturate!(u64, test_div_saturate_u64, std::u64::MAX); test_div_saturate!(u32, test_div_saturate_u32, std::u32::MAX); test_div_saturate!(u16, test_div_saturate_u16, std::u16::MAX); -test_div_saturate!(u8, test_div_saturate_u8, std::u8::MAX); +test_div_saturate!(u8, test_div_saturate_u8, std::u8::MAX); macro_rules! test_idiv_saturate { ($ty:ty, $name:ident, $max:expr, $min:expr) => { @@ -341,10 +343,16 @@ macro_rules! test_idiv_saturate { 0 => match args.0.cmp(&0) { Ordering::Greater => $max, Ordering::Equal => 0, - Ordering::Less => $min + Ordering::Less => $min, }, - -1 => if args.0 == $min { $max } else { -args.0 }, - _ => args.0 / args.1 + -1 => { + if args.0 == $min { + $max + } else { + -args.0 + } + } + _ => args.0 / args.1, }; let actual = DivSaturate::div_saturate(args.0, args.1); expected == actual @@ -355,11 +363,16 @@ macro_rules! test_idiv_saturate { }; } -test_idiv_saturate!(isize, test_div_saturate_isize, std::isize::MAX, std::isize::MIN); +test_idiv_saturate!( + isize, + test_div_saturate_isize, + std::isize::MAX, + std::isize::MIN +); test_idiv_saturate!(i64, test_div_saturate_i64, std::i64::MAX, std::i64::MIN); test_idiv_saturate!(i32, test_div_saturate_i32, std::i32::MAX, std::i32::MIN); test_idiv_saturate!(i16, test_div_saturate_i16, std::i16::MAX, std::i16::MIN); -test_idiv_saturate!(i8, test_div_saturate_i8, std::i8::MAX, std::i8::MIN); +test_idiv_saturate!(i8, test_div_saturate_i8, std::i8::MAX, std::i8::MIN); macro_rules! test_rem_panic { ($ty:ty, $name:ident) => { @@ -367,8 +380,7 @@ macro_rules! test_rem_panic { fn $name() { fn check(args: ($ty, $ty)) -> bool { let expected = args.0.checked_rem(args.1); - let actual = catch_unwind( - || RemPanic::rem_panic(args.0, args.1)).ok(); + let actual = catch_unwind(|| RemPanic::rem_panic(args.0, args.1)).ok(); expected == actual } install_handler(); @@ -381,12 +393,12 @@ test_rem_panic!(usize, test_rem_panic_usize); test_rem_panic!(u64, test_rem_panic_u64); test_rem_panic!(u32, test_rem_panic_u32); test_rem_panic!(u16, test_rem_panic_u16); -test_rem_panic!(u8, test_rem_panic_u8); +test_rem_panic!(u8, test_rem_panic_u8); test_rem_panic!(isize, test_rem_panic_isize); test_rem_panic!(i64, test_rem_panic_i64); test_rem_panic!(i32, test_rem_panic_i32); test_rem_panic!(i16, test_rem_panic_i16); -test_rem_panic!(i8, test_rem_panic_i8); +test_rem_panic!(i8, test_rem_panic_i8); macro_rules! test_rem_wrap { ($ty:ty, $name:ident) => { @@ -394,8 +406,7 @@ macro_rules! test_rem_wrap { fn $name() { fn check(args: ($ty, $ty)) -> bool { let expected = args.0.checked_rem(args.1); - let actual = catch_unwind( - || RemWrap::rem_wrap(args.0, args.1)).ok(); + let actual = catch_unwind(|| RemWrap::rem_wrap(args.0, args.1)).ok(); expected == actual } install_handler(); @@ -408,12 +419,12 @@ test_rem_wrap!(usize, test_rem_wrap_usize); test_rem_wrap!(u64, test_rem_wrap_u64); test_rem_wrap!(u32, test_rem_wrap_u32); test_rem_wrap!(u16, test_rem_wrap_u16); -test_rem_wrap!(u8, test_rem_wrap_u8); +test_rem_wrap!(u8, test_rem_wrap_u8); test_rem_wrap!(isize, test_rem_wrap_isize); test_rem_wrap!(i64, test_rem_wrap_i64); test_rem_wrap!(i32, test_rem_wrap_i32); test_rem_wrap!(i16, test_rem_wrap_i16); -test_rem_wrap!(i8, test_rem_wrap_i8); +test_rem_wrap!(i8, test_rem_wrap_i8); macro_rules! test_rem_saturate { ($ty:ty, $name:ident, $max:expr) => { @@ -421,7 +432,11 @@ macro_rules! test_rem_saturate { fn $name() { fn check(args: ($ty, $ty)) -> bool { let expected = if args.1 == 0 { - if args.0 == 0 { 0 } else { $max } + if args.0 == 0 { + 0 + } else { + $max + } } else { args.0 % args.1 }; @@ -438,12 +453,12 @@ test_rem_saturate!(usize, test_rem_saturate_usize, std::usize::MAX); test_rem_saturate!(u64, test_rem_saturate_u64, std::u64::MAX); test_rem_saturate!(u32, test_rem_saturate_u32, std::u32::MAX); test_rem_saturate!(u16, test_rem_saturate_u16, std::u16::MAX); -test_rem_saturate!(u8, test_rem_saturate_u8, std::u8::MAX); +test_rem_saturate!(u8, test_rem_saturate_u8, std::u8::MAX); test_rem_saturate!(isize, test_rem_saturate_isize, std::isize::MAX); test_rem_saturate!(i64, test_rem_saturate_i64, std::i64::MAX); test_rem_saturate!(i32, test_rem_saturate_i32, std::i32::MAX); test_rem_saturate!(i16, test_rem_saturate_i16, std::i16::MAX); -test_rem_saturate!(i8, test_rem_saturate_i8, std::i8::MAX); +test_rem_saturate!(i8, test_rem_saturate_i8, std::i8::MAX); #[cfg(target_pointer_width = "16")] const USIZE_BITS: usize = 16; @@ -466,8 +481,7 @@ macro_rules! test_shl_panic { } else { None }; - let actual = catch_unwind( - || ShlPanic::shl_panic(args.0, args.1)).ok(); + let actual = catch_unwind(|| ShlPanic::shl_panic(args.0, args.1)).ok(); expected == actual } install_handler(); @@ -480,7 +494,7 @@ test_shl_panic!(usize, test_shl_panic_usize, USIZE_BITS); test_shl_panic!(u64, test_shl_panic_u64, 64); test_shl_panic!(u32, test_shl_panic_u32, 32); test_shl_panic!(u16, test_shl_panic_u16, 16); -test_shl_panic!(u8, test_shl_panic_u8, 8); +test_shl_panic!(u8, test_shl_panic_u8, 8); macro_rules! test_ishl_panic { ($ty:ty, $name:ident, $bits:expr, $max:expr, $min:expr) => { @@ -490,14 +504,21 @@ macro_rules! test_ishl_panic { let expected = match args.0.cmp(&0) { Ordering::Equal => Some(0), Ordering::Greater => { - if args.1 as usize >= $bits || ($max >> args.1) < args.0 { None } else { Some(args.0 << args.1) } + if args.1 as usize >= $bits || ($max >> args.1) < args.0 { + None + } else { + Some(args.0 << args.1) + } } Ordering::Less => { - if args.1 as usize >= $bits || ($min >> args.1) > args.0 { None } else { Some(args.0 << args.1) } + if args.1 as usize >= $bits || ($min >> args.1) > args.0 { + None + } else { + Some(args.0 << args.1) + } } }; - let actual = catch_unwind( - || ShlPanic::shl_panic(args.0, args.1)).ok(); + let actual = catch_unwind(|| ShlPanic::shl_panic(args.0, args.1)).ok(); expected == actual } install_handler(); @@ -506,11 +527,17 @@ macro_rules! test_ishl_panic { }; } -test_ishl_panic!(isize, test_shl_panic_isize, (USIZE_BITS - 1), std::isize::MAX, std::isize::MIN); +test_ishl_panic!( + isize, + test_shl_panic_isize, + (USIZE_BITS - 1), + std::isize::MAX, + std::isize::MIN +); test_ishl_panic!(i64, test_shl_panic_i64, 63, std::i64::MAX, std::i64::MIN); test_ishl_panic!(i32, test_shl_panic_i32, 31, std::i32::MAX, std::i32::MIN); test_ishl_panic!(i16, test_shl_panic_i16, 15, std::i16::MAX, std::i16::MIN); -test_ishl_panic!(i8, test_shl_panic_i8, 7, std::i8::MAX, std::i8::MIN); +test_ishl_panic!(i8, test_shl_panic_i8, 7, std::i8::MAX, std::i8::MIN); #[test] fn check_shl_wrap_usize() { @@ -527,7 +554,11 @@ fn check_shl_wrap_usize() { fn check_shl_saturate_usize() { fn check(args: (usize, usize)) -> bool { let expected = if args.1 as usize >= 64 || ((!0) >> args.1) < args.0 { - if args.0 == 0 { 0 } else { std::usize::MAX } + if args.0 == 0 { + 0 + } else { + std::usize::MAX + } } else { args.0 << args.1 }; diff --git a/overflower/tests/simple.rs b/overflower/tests/simple.rs index 3507881..ba9fd82 100644 --- a/overflower/tests/simple.rs +++ b/overflower/tests/simple.rs @@ -60,13 +60,13 @@ fn test_refs() { fn test_strings() { let s = String::from("Hello, "); let _ = s + "World!"; - + let mut x = String::from("What's "); x += "up"; - + let cow = Cow::Borrowed("Hi, "); cow + "there!"; - + let mut cow = Cow::Borrowed("Hi, "); cow += "you!"; cow += Cow::Borrowed(" Rust is great!");