use super::task::Task;
use super::StreamUnordered;
use core::marker::PhantomData;
use core::pin::Pin;
use core::sync::atomic::Ordering::Relaxed;
#[derive(Debug)]
pub struct IterPinMut<'a, S> {
pub(super) task: *const Task<S>,
pub(super) len: usize,
pub(super) _marker: PhantomData<&'a mut StreamUnordered<S>>,
}
#[derive(Debug)]
pub struct IterMut<'a, S: Unpin>(pub(super) IterPinMut<'a, S>);
#[derive(Debug)]
pub struct IterPinRef<'a, S> {
pub(super) task: *const Task<S>,
pub(super) len: usize,
pub(super) pending_next_all: *mut Task<S>,
pub(super) _marker: PhantomData<&'a StreamUnordered<S>>,
}
#[derive(Debug)]
pub struct Iter<'a, S: Unpin>(pub(super) IterPinRef<'a, S>);
impl<'a, S> Iterator for IterPinMut<'a, S> {
type Item = Pin<&'a mut S>;
fn next(&mut self) -> Option<Pin<&'a mut S>> {
if self.task.is_null() {
return None;
}
unsafe {
let stream = (*(*self.task).stream.get()).as_mut().unwrap();
let next = (*self.task).next_all.load(Relaxed);
self.task = next;
self.len -= 1;
Some(Pin::new_unchecked(stream))
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
}
impl<S> ExactSizeIterator for IterPinMut<'_, S> {}
impl<'a, S: Unpin> Iterator for IterMut<'a, S> {
type Item = &'a mut S;
fn next(&mut self) -> Option<&'a mut S> {
self.0.next().map(Pin::get_mut)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<S: Unpin> ExactSizeIterator for IterMut<'_, S> {}
impl<'a, S> Iterator for IterPinRef<'a, S> {
type Item = Pin<&'a S>;
fn next(&mut self) -> Option<Pin<&'a S>> {
if self.task.is_null() {
return None;
}
unsafe {
let stream = (*(*self.task).stream.get()).as_ref().unwrap();
let next = (*self.task).spin_next_all(self.pending_next_all, Relaxed);
self.task = next;
self.len -= 1;
Some(Pin::new_unchecked(stream))
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
}
impl<S> ExactSizeIterator for IterPinRef<'_, S> {}
impl<'a, S: Unpin> Iterator for Iter<'a, S> {
type Item = &'a S;
fn next(&mut self) -> Option<&'a S> {
self.0.next().map(Pin::get_ref)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<S: Unpin> ExactSizeIterator for Iter<'_, S> {}