Struct Handle

Source
pub struct Handle<E> { /* private fields */ }
Expand description

A handle to a running service loop.

You can use it to cancel the running loop at the next opportunity (through [Handle::cancel]), or to wait for the loop to terminate (through Handle::wait). You can also use Handle::canceller to get a Canceller handle, which lets you terminate the service loop elsewhere (e.g., while waiting).

Implementations§

Source§

impl<E> Handle<E>

Source

pub fn canceller(&self) -> Canceller

Get another handle for cancelling the service loop.

This can be handy if you want one thread to wait for the service loop to exit, while another watches for exit signals.

Examples found in repository?
examples/basic.rs (line 29)
25fn main() {
26    let s = Service::new();
27    eprintln!("server running");
28    let h = s.spawn();
29    let exit = h.canceller();
30    thread::spawn(move || {
31        thread::sleep(time::Duration::from_secs(10));
32        eprintln!("server terminating");
33        exit.cancel();
34    });
35    h.wait().unwrap();
36    eprintln!("server terminated");
37}
Source

pub fn wait(self) -> Result<(), E>

Block the current thread waiting for the service loop to exit, and return its result.

If the service loop returns an error, this method will return it in the Err value. If the service loop panics, this method will also panic with the same error.

Examples found in repository?
examples/basic.rs (line 35)
25fn main() {
26    let s = Service::new();
27    eprintln!("server running");
28    let h = s.spawn();
29    let exit = h.canceller();
30    thread::spawn(move || {
31        thread::sleep(time::Duration::from_secs(10));
32        eprintln!("server terminating");
33        exit.cancel();
34    });
35    h.wait().unwrap();
36    eprintln!("server terminated");
37}

Methods from Deref<Target = Canceller>§

Source

pub fn cancel(&self)

Cancel the currently running service loop. This method does not block; it sends a signal that the service loop should cease execution and returns immediately.

Note that this will not interrupt a currently executing Cancellable::for_each. Instead, the next time Cancellable::for_each would be called, the service loop will return.

Examples found in repository?
examples/basic.rs (line 33)
25fn main() {
26    let s = Service::new();
27    eprintln!("server running");
28    let h = s.spawn();
29    let exit = h.canceller();
30    thread::spawn(move || {
31        thread::sleep(time::Duration::from_secs(10));
32        eprintln!("server terminating");
33        exit.cancel();
34    });
35    h.wait().unwrap();
36    eprintln!("server terminated");
37}

Trait Implementations§

Source§

impl<E> Deref for Handle<E>

Source§

type Target = Canceller

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<E> Freeze for Handle<E>

§

impl<E> !RefUnwindSafe for Handle<E>

§

impl<E> Send for Handle<E>

§

impl<E> Sync for Handle<E>

§

impl<E> Unpin for Handle<E>

§

impl<E> !UnwindSafe for Handle<E>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.