use error;
#[derive(Clone, Copy, Eq, PartialEq, PartialOrd)]
pub struct BitLength(pub usize);
impl BitLength {
#[inline]
pub fn from_usize_bits(bits: usize) -> BitLength { BitLength(bits) }
#[inline]
pub fn from_usize_bytes(bytes: usize)
-> Result<BitLength, error::Unspecified> {
let bits = try!(bytes.checked_mul(8).ok_or(error::Unspecified));
Ok(BitLength::from_usize_bits(bits))
}
#[inline]
pub fn half_rounded_up(&self) -> BitLength {
let round_up = self.0 & 1;
BitLength((self.0 / 2) + round_up)
}
#[inline]
pub fn as_usize_bits(&self) -> usize { self.0 }
#[inline]
pub fn as_usize_bytes_rounded_up(&self) -> usize {
let round_up = ((self.0 >> 2) | (self.0 >> 1) | self.0) & 1;
(self.0 / 8) + round_up
}
#[inline]
pub fn try_sub(self, other: BitLength)
-> Result<BitLength, error::Unspecified> {
let sum = try!(self.0.checked_sub(other.0).ok_or(error::Unspecified));
Ok(BitLength(sum))
}
}
pub const ONE: BitLength = BitLength(1);