use {Bits, BitsMut};
use range_compat::*;
pub trait BitSliceable<Range>: Bits {
type Slice: Bits<Block = Self::Block>;
fn bit_slice(self, range: Range) -> Self::Slice;
}
pub trait BitSliceableMut<Range>: BitSliceable<Range> {
fn bit_slice_mut(self, range: Range) -> Self::Slice where Self: Sized {
self.bit_slice(range)
}
}
impl<Range, T> BitSliceableMut<Range> for T
where T: BitSliceable<Range>,
T::Slice: BitsMut { }
impl<'a> BitSliceable<RangeFull> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, _: RangeFull) -> &'a [bool] {
self
}
}
impl<'a> BitSliceable<RangeFull> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, _: RangeFull) -> &'a mut [bool] {
self
}
}
impl<'a> BitSliceable<Range<u64>> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, range: Range<u64>) -> &'a [bool] {
&self[range.start as usize .. range.end as usize]
}
}
impl<'a> BitSliceable<Range<u64>> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, range: Range<u64>) -> &'a mut [bool] {
&mut self[range.start as usize .. range.end as usize]
}
}
#[cfg(inclusive_range)]
impl<'a> BitSliceable<RangeInclusive<u64>> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, range: RangeInclusive<u64>) -> &'a [bool] {
let (start, end) = get_inclusive_bounds(range)
.expect("<&[bool]>::bit_slice: bad inclusive range");
&self[start as usize .. end as usize + 1]
}
}
#[cfg(inclusive_range)]
impl<'a> BitSliceable<RangeInclusive<u64>> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, range: RangeInclusive<u64>) -> &'a mut [bool] {
let (start, end) = get_inclusive_bounds(range)
.expect("<&mut [bool]>::bit_slice: bad inclusive range");
&mut self[start as usize .. end as usize + 1]
}
}
impl<'a> BitSliceable<RangeFrom<u64>> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, range: RangeFrom<u64>) -> &'a [bool] {
&self[range.start as usize ..]
}
}
impl<'a> BitSliceable<RangeFrom<u64>> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, range: RangeFrom<u64>) -> &'a mut [bool] {
&mut self[range.start as usize ..]
}
}
impl<'a> BitSliceable<RangeTo<u64>> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, range: RangeTo<u64>) -> &'a [bool] {
&self[.. range.end as usize]
}
}
impl<'a> BitSliceable<RangeTo<u64>> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, range: RangeTo<u64>) -> &'a mut [bool] {
&mut self[.. range.end as usize]
}
}
#[cfg(inclusive_range)]
impl<'a> BitSliceable<RangeToInclusive<u64>> for &'a [bool] {
type Slice = &'a [bool];
fn bit_slice(self, range: RangeToInclusive<u64>) -> &'a [bool] {
&self[RangeToInclusive { end: range.end as usize }]
}
}
#[cfg(inclusive_range)]
impl<'a> BitSliceable<RangeToInclusive<u64>> for &'a mut [bool] {
type Slice = &'a mut [bool];
fn bit_slice(self, range: RangeToInclusive<u64>) -> &'a mut [bool] {
&mut self[RangeToInclusive { end: range.end as usize }]
}
}