Skip to main content

JoinMap

Struct JoinMap 

Source
pub struct JoinMap<K, V> { /* private fields */ }
Expand description

A collection of keyed tasks spawned on a Tokio runtime. Hacky implementation of a join set that allows for a key to be associated with each task by having the task return a tuple of (key, value).

Implementations§

Source§

impl<K, V> JoinMap<K, V>

Source

pub fn new() -> Self

Create a new JoinSet.

Source

pub fn len(&self) -> usize

Returns the number of tasks currently in the JoinSet.

Source

pub fn is_empty(&self) -> bool

Returns whether the JoinSet is empty.

Source§

impl<K, V> JoinMap<K, V>
where K: Eq + Hash + Clone + Send + Sync + 'static, V: 'static,

Source

pub fn spawn<F>(&mut self, key: K, future: F)
where F: Future<Output = (K, V)> + Send + 'static, V: Send,

Spawns a task onto the Tokio runtime that will execute the given future ONLY IF there is not already a task in the set with the same key.

Source

pub fn contains_key(&self, key: &K) -> bool

Returns true if the JoinSet contains a task for the given key.

Source

pub async fn join_next(&mut self) -> Option<Result<(K, V), JoinError>>

Waits until one of the tasks in the set completes and returns its output.

Returns None if the set is empty.

§Cancel Safety

This method is cancel safe. If join_next is used as the event in a tokio::select! statement and some other branch completes first, it is guaranteed that no tasks were removed from this JoinSet.

Source

pub fn poll_join_next( &mut self, cx: &mut Context<'_>, ) -> Poll<Option<Result<(K, V), JoinError>>>

Polls for one of the tasks in the set to complete.

If this returns Poll::Ready(Some(_)), then the task that completed is removed from the set.

When the method returns Poll::Pending, the Waker in the provided Context is scheduled to receive a wakeup when a task in the JoinSet completes. Note that on multiple calls to poll_join_next, only the Waker from the Context passed to the most recent call is scheduled to receive a wakeup.

§Returns

This function returns:

  • Poll::Pending if the JoinSet is not empty but there is no task whose output is available right now.
  • Poll::Ready(Some(Ok(value))) if one of the tasks in this JoinSet has completed. The value is the return value of one of the tasks that completed.
  • Poll::Ready(Some(Err(err))) if one of the tasks in this JoinSet has panicked or been aborted. The err is the JoinError from the panicked/aborted task.
  • Poll::Ready(None) if the JoinSet is empty.

Note that this method may return Poll::Pending even if one of the tasks has completed. This can happen if the [coop budget] is reached.

Trait Implementations§

Source§

impl<K: Debug, V: Debug> Debug for JoinMap<K, V>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<K: Default, V: Default> Default for JoinMap<K, V>

Source§

fn default() -> JoinMap<K, V>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for JoinMap<K, V>

§

impl<K, V> RefUnwindSafe for JoinMap<K, V>
where K: RefUnwindSafe,

§

impl<K, V> Send for JoinMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for JoinMap<K, V>
where K: Send + Sync, V: Send,

§

impl<K, V> Unpin for JoinMap<K, V>
where K: Unpin,

§

impl<K, V> UnsafeUnpin for JoinMap<K, V>

§

impl<K, V> UnwindSafe for JoinMap<K, V>
where K: UnwindSafe,

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> SpanExt<T> for T

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more