Skip to content

Instantly share code, notes, and snippets.

@kepstin
Created June 28, 2019 15:51
Show Gist options
  • Save kepstin/aa022316369c36124dccb623557b5971 to your computer and use it in GitHub Desktop.
Save kepstin/aa022316369c36124dccb623557b5971 to your computer and use it in GitHub Desktop.
use std::ops::{Shr,Neg};
use std::marker::Sized;
trait ShrCeil<Rhs=Self>
{
type Output;
fn shr_ceil(self, rhs: Rhs) -> Self::Output;
}
impl<Rhs, Lhs> ShrCeil<Rhs> for Lhs
where
Lhs: Neg + Sized,
<Lhs as Neg>::Output: Shr<Rhs>,
<<Lhs as Neg>::Output as Shr<Rhs>>::Output: Neg,
{
type Output = <<<Lhs as Neg>::Output as Shr<Rhs>>::Output as Neg>::Output;
fn shr_ceil(self, rhs: Rhs) -> Self::Output {
-((-self) >> rhs)
}
}
#[test]
fn shr_ceil_i8_odd() {
assert_eq!(3i8.shr_ceil(1), 2i8)
}
#[test]
fn shr_ceil_i8_even() {
assert_eq!(4i8.shr_ceil(1), 2i8)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment