Struct IoContextWork

Source
pub struct IoContextWork(/* private fields */);
Expand description

The class to delaying until the stop of IoContext is dropped.

§Examples

When dropped the IoContextWork, to stop the IoContext:

use asyncio::IoContext;
use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};

static COUNT: AtomicUsize = ATOMIC_USIZE_INIT;

let ctx = &IoContext::new().unwrap();
let mut work = Some(IoContext::work(ctx));

fn count_if_not_stopped(ctx: &IoContext) {
  if !ctx.stopped() {
    COUNT.fetch_add(1, Ordering::Relaxed);
  }
}
ctx.post(count_if_not_stopped);
ctx.post(move |_| work = None);  // call IoContext::stop()
ctx.post(count_if_not_stopped);
ctx.run();

assert_eq!(COUNT.load(Ordering::Relaxed), 1);

§Examples

A multithreading example code:

use asyncio::IoContext;
use std::thread;
use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};

static COUNT: AtomicUsize = ATOMIC_USIZE_INIT;

let ctx = &IoContext::new().unwrap();
let _work = IoContext::work(ctx);

let mut thrds = Vec::new();
for _ in 0..10 {
  let ctx = ctx.clone();
  thrds.push(thread::spawn(move|| ctx.run()));
}

for _ in 0..100 {
  ctx.post(move|ctx| {
    if COUNT.fetch_add(1, Ordering::SeqCst) == 99 {
      ctx.stop();
    }
  });
}

ctx.run();
for thrd in thrds {
  thrd.join().unwrap();
}

assert_eq!(COUNT.load(Ordering::Relaxed), 100);

Trait Implementations§

Source§

impl AsIoContext for IoContextWork

Source§

impl Drop for IoContextWork

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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.