use std::{
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use futures::Stream;
use crate::{
pollable::{PollStrategy, PollWith},
selector::{Id, Selector},
};
pub struct WithExt<'s, 'e, 'emut, S: PollStrategy, E: ?Sized, EMut: ?Sized> {
pub(super) selector: &'s mut Selector<S>,
pub(super) ext: &'e E,
pub(super) ext_mut: &'emut mut EMut,
}
impl<'e, S, E, EMut> Stream for WithExt<'_, 'e, '_, S, E, EMut>
where
S: PollStrategy,
S: PollWith<'e, E, EMut>,
E: ?Sized,
EMut: ?Sized,
{
type Item = S::Progress;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
this.selector
.poll_next_inner(this.ext, this.ext_mut, |_| (), cx)
.map(|opt| opt.map(|(result, ())| result))
}
}
pub struct WithId<'s, S: PollStrategy> {
pub(super) selector: &'s mut Selector<S>,
}
impl<S> Stream for WithId<'_, S>
where
S: PollStrategy,
S: PollWith<'static, (), ()>,
{
type Item = (S::Progress, Id<S::Pollable>);
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.get_mut()
.selector
.poll_next_inner(&(), &mut (), |task| Id(Arc::downgrade(task)), cx)
}
}
pub struct WithExtAndId<'s, 'e, 'emut, S: PollStrategy, E: ?Sized, EMut: ?Sized> {
pub(super) selector: &'s mut Selector<S>,
pub(super) ext: &'e E,
pub(super) ext_mut: &'emut mut EMut,
}
impl<'e, S, E, EMut> Stream for WithExtAndId<'_, 'e, '_, S, E, EMut>
where
S: PollStrategy,
S: PollWith<'e, E, EMut>,
E: ?Sized,
EMut: ?Sized,
{
type Item = (S::Progress, Id<S::Pollable>);
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
this.selector
.poll_next_inner(this.ext, this.ext_mut, |task| Id(Arc::downgrade(task)), cx)
}
}