Struct DynTimeout

Source
pub struct DynTimeout { /* private fields */ }
Expand description

Dynamic timeout, async implementation with the tokio library.

§Example

use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;
const TWENTY: Duration = Duration::from_millis(20);

let mut rt = Runtime::new().unwrap();
rt.spawn(async {
   let dyn_timeout = DynTimeout::new(TWENTY, || {
       println!("after forty milliseconds");
   });
   dyn_timeout.add(TWENTY).await.unwrap();
});

Implementations§

Source§

impl DynTimeout

Source

pub fn new(dur: Duration, callback: fn()) -> Self

Create a new dynamic timeout in a new thread. Execute the callback function in the separated thread after a given duration.

§Example
use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;
const TWENTY: Duration = Duration::from_millis(20);

let mut rt = Runtime::new().unwrap();
rt.spawn(async {
   let dyn_timeout = DynTimeout::new(TWENTY, || {
       println!("after forty milliseconds");
   });
   dyn_timeout.add(TWENTY).await.unwrap();
});
Source

pub fn with_sender(dur: Duration, sender_in: Sender<()>) -> Self

Create a new dynamic timeout in a new thread. Call the mpsc sender on timeout reached.

§Example
use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;
const TWENTY: Duration = Duration::from_millis(20);

let mut rt = Runtime::new().unwrap();
rt.spawn(async {
   let (sender, mut receiver) = tokio::sync::mpsc::channel::<()>(1);
   let dyn_timeout = DynTimeout::with_sender(TWENTY, sender);
   tokio::select! {
    _ = receiver.recv() => println!("Timeout!")
   }
});
Source

pub fn set_max_waiting_time(&mut self, duration: Duration)

Set a muximum time we can wait, dismiss the add call if overflow.

Source

pub async fn add(&self, dur: Duration) -> Result<()>

Increase the delay before the timeout.

§Return

Return a result with an error if the timeout already appened. Otherwise it return an empty success.

§Example
use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;
const TWENTY: Duration = Duration::from_millis(20);

let mut rt = Runtime::new().unwrap();
rt.spawn(async {
   let dyn_timeout = DynTimeout::new(TWENTY, || {
       println!("after some milliseconds");
   });
   dyn_timeout.add(TWENTY).await.unwrap();
});
Source

pub async fn sub(&self, dur: Duration) -> Result<()>

Try to decrease the delay before the timeout. (bad precision, work in progress)

§Return

Return a result with an error if the timeout already appened. Otherwise it return an empty success.

§Example
use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;

const TWENTY: Duration = Duration::from_millis(20);
const TEN: Duration = Duration::from_millis(10);

let mut rt = Runtime::new().unwrap();
rt.spawn(async {
   let dyn_timeout = DynTimeout::new(TWENTY, || {
       println!("after some milliseconds");
   });
   dyn_timeout.add(TEN).await.unwrap();
   dyn_timeout.add(TWENTY).await.unwrap();
   dyn_timeout.sub(TEN).await.unwrap();
});
Source

pub async fn cancel(&mut self) -> Result<()>

Dismiss the timeout callback and cancel all delays added. Stop immediatelly all waiting process and join the created thread.

§Return

Return a result with an error if the timeout already appened. Otherwise it return an empty success.

§Example
use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;

const TWENTY: Duration = Duration::from_millis(20);
const TEN: Duration = Duration::from_millis(10);

let mut rt = Runtime::new().unwrap();
rt.spawn(async {
   let mut dyn_timeout = DynTimeout::new(TWENTY, || {
       println!("never append");
   });
   dyn_timeout.cancel().await.unwrap();
});
Source

pub async fn wait(&mut self) -> Result<()>

Wait for the end of the timeout

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.