use num_traits::One;
pub trait Decrement {
type Output;
fn dec(self) -> Self::Output;
}
pub trait DecrementRef {
type Output;
fn dec_ref(&self) -> Self::Output;
}
pub trait DecrementMut {
type Output;
fn dec_mut(&mut self) -> Self::Output;
}
pub trait Increment {
type Output;
fn inc(self) -> Self::Output;
}
pub trait IncrementRef {
type Output;
fn inc_ref(&self) -> Self::Output;
}
pub trait IncrementMut {
type Output;
fn inc_mut(&mut self) -> Self::Output;
}
impl<S> Decrement for S
where
S: One + core::ops::Sub<S, Output = S>,
{
type Output = S;
fn dec(self) -> Self::Output {
self - S::one()
}
}
impl<S> DecrementMut for S
where
S: One,
for<'a> &'a S: core::ops::Sub<S, Output = S>,
{
type Output = S;
fn dec_mut(&mut self) -> Self::Output {
let next = &(*self) - S::one();
core::mem::replace(self, next)
}
}
impl<S> DecrementRef for S
where
S: One,
for<'a> &'a S: core::ops::Sub<S, Output = S>,
{
type Output = S;
fn dec_ref(&self) -> Self::Output {
self - S::one()
}
}
impl<S> Increment for S
where
S: One + core::ops::Add<S, Output = S>,
{
type Output = S;
fn inc(self) -> Self::Output {
self + S::one()
}
}
impl<S> IncrementMut for S
where
S: One,
for<'a> &'a S: core::ops::Add<S, Output = S>,
{
type Output = S;
fn inc_mut(&mut self) -> Self::Output {
let next = &(*self) + S::one();
core::mem::replace(self, next)
}
}
impl<S> IncrementRef for S
where
S: One,
for<'a> &'a S: core::ops::Add<S, Output = S>,
{
type Output = S;
fn inc_ref(&self) -> Self::Output {
self + S::one()
}
}