Skip to main content

DrivenAsyncIterator

Struct DrivenAsyncIterator 

Source
pub struct DrivenAsyncIterator<'a, Iter: AsyncIterator> { /* private fields */ }
Expand description

The AsyncIterator wrapper type that drive_next provides to its closure.

Implementations§

Source§

impl<Iter: AsyncIterator> DrivenAsyncIterator<'_, Iter>

Source

pub async fn next(&self) -> Option<Iter::Item>

Get the next item from the async iterator.

Note that this method takes &self, and multiple futures are allowed to call it concurrently. However, if you manage to call next in parallel from multiple threads, it’s likely to panic.

Implementation details: DrivenAsyncIterator uses an AtomicRefCell internally, rather than a Mutex. This makes it more efficient and also no_std-compatible. The downside is panics when it’s accessed from parallel threads, but this should be extremely unlikely in practice. DrivenAsyncIterator has a local lifetime bound, so it can’t be given to task::spawn or thread::spawn. Scoped tasks also don’t currently exist. To trigger this panic, you’d have to use something like thread::scope in an async context, which would be unusual and potentially an executor-blocking bug in any case.

Note also that if this method is cancelled, drive_next will continue fetching the next item from the async iterator in the background. This is necessary to satisfy the AsyncIterator contract. Once you start fetching the next item, the only way to fully cancel the fetch is to return from the drive_next closure or cancel drive_next itself.

Source

pub fn with_pin_mut<F, Ret>(&self, f: F) -> Ret
where F: FnOnce(Option<Pin<&mut Iter>>) -> Ret,

Get a temporary pinned reference to the driven async iterator, if it’s not yet done.

drive_next drops its iterator as soon as poll_next returns Done. If with_pin_mut is called after that, the closure will receive None.

The closure argument is not async, because this reference can’t be held across .await points. If you try to call this reentrantly, it will panic.

Whenever you call this method, drive_next will immediately re-poll the iterator, in case it’s something like FuturesUnordered and you just added more work to it. This is kind of a hack, and it would be better if collections like FuturesUnordered handled their own wakeups in these cases, but that’s not how things work today.

Source

pub fn with_mut<F, Ret>(&self, f: F) -> Ret
where F: FnOnce(Option<&mut Iter>) -> Ret, Iter: Unpin,

Get a temporary mutable reference to the driven async iterator, if it’s not yet done.

This works like with_pin_mut, except that it requires the iterator to be Unpin.

Trait Implementations§

Source§

impl<Iter: AsyncIterator> Debug for DrivenAsyncIterator<'_, Iter>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, Iter> !RefUnwindSafe for DrivenAsyncIterator<'a, Iter>

§

impl<'a, Iter> !UnwindSafe for DrivenAsyncIterator<'a, Iter>

§

impl<'a, Iter> Freeze for DrivenAsyncIterator<'a, Iter>

§

impl<'a, Iter> Send for DrivenAsyncIterator<'a, Iter>
where Iter: Send + Sync, <Iter as AsyncIterator>::Item: Send + Sync,

§

impl<'a, Iter> Sync for DrivenAsyncIterator<'a, Iter>
where Iter: Send + Sync, <Iter as AsyncIterator>::Item: Send + Sync,

§

impl<'a, Iter> Unpin for DrivenAsyncIterator<'a, Iter>

§

impl<'a, Iter> UnsafeUnpin for DrivenAsyncIterator<'a, Iter>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.