#[macro_export]
macro_rules! pp
{
($integer:ident) =>
{
$integer += 1;
}
}
#[macro_export]
macro_rules! pp_mut
{
($integer:ident) =>
{
{
*$integer += 1;
*$integer
}
}
}
#[macro_export]
macro_rules! checked_pp_mut
{
($integer:ident) =>
{
if let Some(res) = $integer.checked_add(1)
{
*$integer = res;
Some(*$integer)
}
else
{
None
}
}
}
#[macro_export]
macro_rules! ppf
{
($float:ident) =>
{
$float += 1.0;
}
}
#[macro_export]
macro_rules! ppf_mut
{
($float:ident) =>
{
{
*$float += 1.0;
*$float
}
}
}
#[macro_export]
macro_rules! mm
{
($integer:ident) =>
{
$integer -= 1;
}
}
#[macro_export]
macro_rules! mm_mut
{
($integer:ident) =>
{
{
*$integer -= 1;
*$integer
}
}
}
#[macro_export]
macro_rules! mmf
{
($float:ident) =>
{
$float -= 1.0;
}
}
#[macro_export]
macro_rules! mmf_mut
{
($float:ident) =>
{
{
*$float -= 1.0;
*$float
}
}
}
#[macro_export]
macro_rules! checked_mm_mut
{
($integer:ident) =>
{
if let Some(res) = $integer.checked_sub(1)
{
*$integer = res;
Some(*$integer)
}
else
{
None
}
}
}
pub trait IncDecSelf
where Self: Sized + Copy
{
fn pp(&mut self) -> Self;
fn try_pp(&mut self) -> Option<Self>;
fn mm(&mut self) -> Self;
fn try_mm(&mut self) -> Option<Self>;
}
impl IncDecSelf for f32
{
fn pp(&mut self) -> Self
{
ppf_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
if *self < (f32::MAX - 0.9)
{
Some(ppf_mut!(self))
}
else
{
None
}
}
fn mm(&mut self) -> Self
{
mmf_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
if *self > (f32::MIN + 0.9)
{
Some(mmf_mut!(self))
}
else
{
None
}
}
}
impl IncDecSelf for f64
{
fn pp(&mut self) -> Self
{
ppf_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
if *self < (f64::MAX - 0.9)
{
Some(ppf_mut!(self))
}
else
{
None
}
}
fn mm(&mut self) -> Self
{
mmf_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
if *self > (f64::MIN + 0.9)
{
Some(mmf_mut!(self))
}
else
{
None
}
}
}
impl IncDecSelf for i8
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
impl IncDecSelf for i16
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
impl IncDecSelf for i32
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
impl IncDecSelf for i64
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
impl IncDecSelf for i128
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
impl IncDecSelf for isize
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
impl IncDecSelf for u8
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
impl IncDecSelf for u16
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
impl IncDecSelf for u32
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
impl IncDecSelf for u64
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
impl IncDecSelf for u128
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
impl IncDecSelf for usize
{
fn pp(&mut self) -> Self
{
pp_mut!(self)
}
fn try_pp(&mut self) -> Option<Self>
{
checked_pp_mut!(self)
}
fn mm(&mut self) -> Self
{
mm_mut!(self)
}
fn try_mm(&mut self) -> Option<Self>
{
checked_mm_mut!(self)
}
}
pub trait IntIncDecSelf
where Self: Sized + Copy
{
fn opp(&mut self) -> (Self, bool);
fn omm(&mut self) -> (Self, bool);
fn wpp(&mut self) -> Self;
fn wmm(&mut self) -> Self;
}
#[macro_export]
macro_rules! opp_mut
{
($integer:ident) =>
{
{
let res = $integer.overflowing_add(1);
*$integer = res.0;
res
}
}
}
#[macro_export]
macro_rules! omm_mut
{
($integer:ident) =>
{
{
let res = $integer.overflowing_sub(1);
*$integer = res.0;
res
}
}
}
#[macro_export]
macro_rules! wpp_mut
{
($integer:ident) =>
{
{
*$integer = $integer.wrapping_add(1);
*$integer
}
}
}
#[macro_export]
macro_rules! wmm_mut
{
($integer:ident) =>
{
{
*$integer = $integer.wrapping_sub(1);
*$integer
}
}
}
impl IntIncDecSelf for i8
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
impl IntIncDecSelf for i16
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
impl IntIncDecSelf for i32
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
impl IntIncDecSelf for i64
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
impl IntIncDecSelf for i128
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
impl IntIncDecSelf for isize
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
impl IntIncDecSelf for u8
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
impl IntIncDecSelf for u16
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
impl IntIncDecSelf for u32
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
impl IntIncDecSelf for u64
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
impl IntIncDecSelf for u128
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
impl IntIncDecSelf for usize
{
fn opp(&mut self) -> (Self, bool)
{
opp_mut!(self)
}
fn omm(&mut self) -> (Self, bool)
{
omm_mut!(self)
}
fn wpp(&mut self) -> Self
{
wpp_mut!(self)
}
fn wmm(&mut self) -> Self
{
wmm_mut!(self)
}
}
#[cfg(test)]
mod tests
{
#[test]
fn try_pp()
{
let mut int_val = 1;
pp!(int_val);
assert_eq!(2, int_val);
}
#[test]
fn try_ppf()
{
let mut f32_val: f32 = 1.0;
ppf!(f32_val);
assert_eq!(2.0, f32_val);
let mut f64_val = 1.0;
ppf!(f64_val);
assert_eq!(2.0, f64_val);
}
#[test]
fn try_mm()
{
let mut int_val = 2;
mm!(int_val);
assert_eq!(1, int_val);
}
#[test]
fn try_mmf()
{
let mut f32_val: f32 = 2.0;
mmf!(f32_val);
assert_eq!(1.0, f32_val);
let mut f64_val = 2.0;
mmf!(f64_val);
assert_eq!(1.0, f64_val);
}
}