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 IterPinMutWithToken<'a, S> {
pub(super) task: *const Task<S>,
pub(super) len: usize,
pub(super) _marker: PhantomData<&'a mut StreamUnordered<S>>,
}
#[derive(Debug)]
pub struct IterPinMut<'a, S>(pub(super) IterPinMutWithToken<'a, S>);
#[derive(Debug)]
pub struct IterMutWithToken<'a, S: Unpin>(pub(super) IterPinMutWithToken<'a, S>);
#[derive(Debug)]
pub struct IterMut<'a, S: Unpin>(pub(super) IterPinMutWithToken<'a, S>);
#[derive(Debug)]
pub struct IterWithToken<'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>>,
}
impl<'a, S> Iterator for IterPinMutWithToken<'a, S> {
type Item = (Pin<&'a mut S>, usize);
fn next(&mut self) -> Option<Self::Item> {
if self.task.is_null() {
return None;
}
unsafe {
let id = (*self.task).id;
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), id))
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
}
impl<S> ExactSizeIterator for IterPinMutWithToken<'_, S> {}
impl<'a, S> Iterator for IterPinMut<'a, S> {
type Item = Pin<&'a mut S>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(s, _)| s)
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
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<Self::Item> {
self.0.next().map(|(stream, _)| Pin::get_mut(stream))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<S: Unpin> ExactSizeIterator for IterMut<'_, S> {}
impl<'a, S: Unpin> Iterator for IterMutWithToken<'a, S> {
type Item = (&'a mut S, usize);
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(stream, id)| (Pin::get_mut(stream), id))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<S: Unpin> ExactSizeIterator for IterMutWithToken<'_, S> {}
impl<'a, S> Iterator for IterWithToken<'a, S> {
type Item = (&'a S, usize);
fn next(&mut self) -> Option<Self::Item> {
if self.task.is_null() {
return None;
}
unsafe {
let id = (*self.task).id;
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((stream, id))
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len, Some(self.len))
}
}
impl<S> ExactSizeIterator for IterWithToken<'_, S> {}