Created
June 28, 2019 15:51
-
-
Save kepstin/aa022316369c36124dccb623557b5971 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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