-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Method yield_after_every to tune iteration guards
Add combinator method .yield_after_every(n) to stream and future combinator types that have been provided with iteration guards. This gives the user means to fine tune the yielding behavior.
- Loading branch information
Showing
22 changed files
with
200 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use core::num::NonZeroU32; | ||
|
||
// Default for repetition limits on eager polling loops, to prevent | ||
// stream-consuming combinators like ForEach from starving other tasks. | ||
pub(crate) const DEFAULT_YIELD_AFTER_LIMIT: NonZeroU32 = unsafe { NonZeroU32::new_unchecked(100) }; | ||
|
||
macro_rules! future_method_yield_after_every { | ||
() => { | ||
future_method_yield_after_every! { | ||
#[doc = "the underlying stream"] | ||
#[doc = "the stream consecutively yields items,"] | ||
} | ||
}; | ||
(#[$pollee:meta] #[$why_busy:meta]) => { | ||
/// Changes the maximum number of iterations before `poll` yields. | ||
/// | ||
/// The implementation of [`poll`] on this future | ||
/** polls */#[$pollee]/** in a loop. */ | ||
/// To prevent blocking in the call to `poll` for too long while | ||
#[$why_busy] | ||
/// the number of iterations is capped to an internal limit, | ||
/// after which the `poll` function wakes up the task | ||
/// and returns [`Pending`]. The `yield_after_every` combinator | ||
/// can be used to tune the iteration limit, returning a future | ||
/// with the limit updated to the provided value. | ||
/// | ||
/// [`poll`]: core::future::Future::poll | ||
/// [`Pending`]: core::task::poll::Poll::Pending | ||
/// | ||
/// # Panics | ||
/// | ||
/// If called with 0 as the number of iterations, this method panics. | ||
/// | ||
pub fn yield_after_every(mut self, iterations: u32) -> Self { | ||
self.yield_after = $crate::core_reexport::num::NonZeroU32::new(iterations) | ||
.expect("iteration limit can't be 0"); | ||
self | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! try_future_method_yield_after_every { | ||
() => { | ||
future_method_yield_after_every! { | ||
#[doc = "the underlying stream"] | ||
#[doc = "the stream consecutively yields `Ok` items,"] | ||
} | ||
} | ||
} | ||
|
||
macro_rules! stream_method_yield_after_every { | ||
() => { | ||
stream_method_yield_after_every! { | ||
#[doc = "the underlying stream"] | ||
#[doc = "the stream consecutively yields items,"] | ||
} | ||
}; | ||
(#[$pollee:meta] #[$why_busy:meta]) => { | ||
/// Changes the maximum number of iterations before `poll_next` yields. | ||
/// | ||
/// The implementation of [`poll_next`] on this stream | ||
/** polls */#[$pollee]/** in a loop. */ | ||
/// To prevent blocking in the call to `poll_next` for too long while | ||
#[$why_busy] | ||
/// the number of iterations is capped to an internal limit, | ||
/// after which the `poll_next` function wakes up the task | ||
/// and returns [`Pending`]. The `yield_after_every` combinator | ||
/// can be used to tune the iteration limit, returning a stream | ||
/// with the limit updated to the provided value. | ||
/// | ||
/// [`poll_next`]: crate::stream::Stream::poll_next | ||
/// [`Pending`]: core::task::poll::Poll::Pending | ||
/// | ||
/// # Panics | ||
/// | ||
/// If called with 0 as the number of iterations, this method panics. | ||
/// | ||
pub fn yield_after_every(mut self, iterations: u32) -> Self { | ||
self.yield_after = $crate::core_reexport::num::NonZeroU32::new(iterations) | ||
.expect("iteration limit can't be 0"); | ||
self | ||
} | ||
}; | ||
} |