use core::{
fmt::{
self,
Debug,
Formatter,
},
iter::FusedIterator,
};
use super::BitBox;
use crate::{
order::BitOrder,
ptr::{
BitPtrRange,
Mut,
},
slice::BitSlice,
store::BitStore,
};
#[cfg(not(tarpaulin_include))]
impl<O, T> IntoIterator for BitBox<O, T>
where
O: BitOrder,
T: BitStore,
{
type IntoIter = IntoIter<O, T>;
type Item = bool;
#[inline(always)]
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self)
}
}
pub struct IntoIter<O, T>
where
O: BitOrder,
T: BitStore,
{
_buf: BitBox<O, T>,
iter: BitPtrRange<Mut, O, T>,
}
impl<O, T> IntoIter<O, T>
where
O: BitOrder,
T: BitStore,
{
fn new(mut this: BitBox<O, T>) -> Self {
let iter = this.as_mut_bitptr_range();
Self { _buf: this, iter }
}
#[inline]
pub fn as_bitslice(&self) -> &BitSlice<O, T> {
self.iter.clone().into_bitspan().to_bitslice_ref()
}
#[doc(hidden)]
#[inline(always)]
#[cfg(not(tarpalin_include))]
#[deprecated = "Use `as_bitslice` to view the underlying slice"]
pub fn as_slice(&self) -> &BitSlice<O, T> {
self.as_bitslice()
}
#[inline]
pub fn as_mut_bitslice(&mut self) -> &mut BitSlice<O, T> {
self.iter.clone().into_bitspan().to_bitslice_mut()
}
#[doc(hidden)]
#[inline(always)]
#[cfg(not(tarpaulin_include))]
#[deprecated = "Use `as_mut_bitslice` to view the underlying slice"]
pub fn as_mut_slice(&mut self) -> &mut BitSlice<O, T> {
self.as_mut_bitslice()
}
}
#[cfg(not(tarpaulin_include))]
impl<O, T> Debug for IntoIter<O, T>
where
O: BitOrder,
T: BitStore,
{
#[inline]
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
fmt.debug_tuple("IntoIter")
.field(&self.as_bitslice())
.finish()
}
}
impl<O, T> Iterator for IntoIter<O, T>
where
O: BitOrder,
T: BitStore,
{
type Item = bool;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(crate::ptr::range::read_raw)
}
#[inline(always)]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
#[inline(always)]
fn count(self) -> usize {
self.len()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.iter.nth(n).map(crate::ptr::range::read_raw)
}
#[inline(always)]
fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}
impl<O, T> DoubleEndedIterator for IntoIter<O, T>
where
O: BitOrder,
T: BitStore,
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(crate::ptr::range::read_raw)
}
#[inline]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.iter.nth_back(n).map(crate::ptr::range::read_raw)
}
}
impl<O, T> ExactSizeIterator for IntoIter<O, T>
where
O: BitOrder,
T: BitStore,
{
#[inline(always)]
fn len(&self) -> usize {
self.iter.len()
}
}
impl<O, T> FusedIterator for IntoIter<O, T>
where
O: BitOrder,
T: BitStore,
{
}