pub struct Queue<S> { /* private fields */ }Expand description
A queue of tasks that take &mut S, which the thread that runs the queue
owns. Cheap to clone: clones are handles to the same queue.
Tasks run in the order they were posted, and a timed task once its deadline passed, before the tasks of the same turn. A task that a closed queue gets drops at once, so tasks that hold handles to their own queue can’t keep it alive once its owner closed it.
Implementations§
Source§impl<S> Queue<S>
impl<S> Queue<S>
pub fn new() -> Self
Sourcepub fn post(&self, task: impl FnOnce(&mut S) + Send + 'static)
pub fn post(&self, task: impl FnOnce(&mut S) + Send + 'static)
Runs task on the queue’s thread, after the tasks posted before it.
Sourcepub fn post_at(
&self,
deadline: Instant,
task: impl FnOnce(&mut S) + Send + 'static,
) -> TimedTask
pub fn post_at( &self, deadline: Instant, task: impl FnOnce(&mut S) + Send + 'static, ) -> TimedTask
Runs task on the queue’s thread in its first turn once deadline
passed, before the turn’s other tasks, unless it’s
cancelled first.
Sourcepub fn post_after(
&self,
delay: Duration,
task: impl FnOnce(&mut S) + Send + 'static,
) -> TimedTask
pub fn post_after( &self, delay: Duration, task: impl FnOnce(&mut S) + Send + 'static, ) -> TimedTask
post_at delay from now. A delay past what an
Instant can hold never comes.
Sourcepub fn cancel(&self, task: TimedTask)
pub fn cancel(&self, task: TimedTask)
Drops the timed task that task names, on this thread, unless it
has begun to run or dropped already: it never runs, and from now on
next_deadline and the queue’s parking no
longer wait for it. A host that armed a timeout for its deadline
before still sees that timeout end.
Sourcepub fn set_waker(&self, waker: Option<Arc<dyn Fn() + Send + Sync>>)
pub fn set_waker(&self, waker: Option<Arc<dyn Fn() + Send + Sync>>)
Calls waker, from the posting thread, whenever a task arrives or
the queue is woken, so that a host that runs the queue with
run_once knows to run it. A timed task calls it
when it’s posted, not when its deadline passes.
Sourcepub fn run_once(&self, state: &mut S) -> bool
pub fn run_once(&self, state: &mut S) -> bool
Runs the timed tasks whose deadline passed, then the tasks waiting
now and those they post, with state, until none waits. Returns
whether any task ran.
Sourcepub fn has_pending(&self) -> bool
pub fn has_pending(&self) -> bool
Whether a task waits for run_once, or a timed
task’s deadline passed.
Sourcepub fn next_deadline(&self) -> Option<Instant>
pub fn next_deadline(&self) -> Option<Instant>
The deadline of the earliest timed task that waits, which a cancelled one no longer does.
Sourcepub fn park(&self)
pub fn park(&self)
Sleeps until a task arrives or the queue is woken, or the next timed task is due. It returns at once when that happened since it last returned, as while the queue was running.
Sourcepub fn park_until(&self, deadline: Option<Instant>)
pub fn park_until(&self, deadline: Option<Instant>)
park, but returns at deadline at the latest.
Sourcepub fn run(&self, state: &mut S)
pub fn run(&self, state: &mut S)
Runs the queue with state on this thread until a stop task runs,
parking while it has nothing to do: what a thread of its own does.
Sourcepub fn run_with(&self, state: &mut S, each_turn: impl FnMut(&mut S))
pub fn run_with(&self, state: &mut S, each_turn: impl FnMut(&mut S))
run, and each_turn after each turn, before the
queue parks, for an owner with work of its own on this thread, which
wakes the queue when it has some.
Sourcepub fn stop(&self)
pub fn stop(&self)
Ends the run in progress, or the next one, at the end
of the turn that runs this stop task: once the tasks posted before it
ran, and those that wait in the same turn after it.
Sourcepub fn is_stopped(&self) -> bool
pub fn is_stopped(&self) -> bool
Trait Implementations§
Auto Trait Implementations§
impl<S> !RefUnwindSafe for Queue<S>
impl<S> !UnwindSafe for Queue<S>
impl<S> Forget for Queue<S>
impl<S> Freeze for Queue<S>
impl<S> Move for Queue<S>
impl<S> Send for Queue<S>
impl<S> Sync for Queue<S>
impl<S> Unpin for Queue<S>
impl<S> UnsafeUnpin for Queue<S>where
*const ArcInner<Inner<S>> is !null: UnsafeUnpin,
PhantomData<ArcInner<Inner<S>>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.