Struct Deferred

Source
pub struct Deferred<T, E> { /* private fields */ }
Expand description

Stores a function and delays its execution until it’s transform to a promise. T : Type of value returned E : Type of error returned

Implementations§

Source§

impl<T, E> Deferred<T, E>
where T: Send + 'static, E: Send + 'static,

Source

pub fn new<F>(f: F) -> Deferred<T, E>
where F: Send + 'static + FnOnce() -> Result<T, E>,

Create a new task in deferred.

let deferred = asynchronous::Deferred::new(|| {
   // Do something  
   if true { Ok("value a") } else { Err("Error description") }
});
// At this point "deferred" is not executed
Source

pub fn sync(self) -> Result<T, E>

Syncronizes the execution with the caller, and returns its value.

use asynchronous::Deferred;
 
let deferred_a = Deferred::<_,&str>::new(|| {
   // Do something  
   Ok("value a")
});
let deferred_b = Deferred::<_,&str>::new(|| {    
   // Do something 
   Ok("value b")
});
// Do something
assert_eq!(deferred_a.sync(), Ok("value a"));
assert_eq!(deferred_b.sync(), Ok("value b"));
Source

pub fn then<TT, EE, FT, FE>(self, ft: FT, fe: FE) -> Deferred<TT, EE>
where TT: Send + 'static, EE: Send + 'static, FT: Send + 'static + FnOnce(T) -> Result<TT, EE>, FE: Send + 'static + FnOnce(E) -> Result<TT, EE>,

Executes only one of the two functions received depending on the result of the previous task deferred (Ok or Err). Returns a new Deferred

let r = asynchronous::Deferred::new(|| {
   if false { Ok(1.23) } else { Err("Final error")}
}).then(|res| {
   unreachable!();
   assert_eq!(res, 1.23);
   Ok(34)
}, |err| {
   assert_eq!(err, "Final error");
   if true { Ok(35) } else { Err(44u64)}
}).sync();
assert_eq!(r, Ok(35));
Source

pub fn chain<TT, EE, FF>(self, f: FF) -> Deferred<TT, EE>
where TT: Send + 'static, EE: Send + 'static, FF: Send + 'static + FnOnce(Result<T, E>) -> Result<TT, EE>,

The same functionality as then, but wraps the result in only one function

let r = asynchronous::Deferred::new(|| {
   if false { Ok(1.23) } else { Err("Final error")}
}).chain(|res| {
   // Do something that executes always even on error
   assert!(res.is_err());
   res
}).then(|res| {
   unreachable!();
   assert_eq!(res, 1.23);
   Ok(34)
}, |err| {
   assert_eq!(err, "Final error");
   if true { Ok(35) } else { Err(44u64)}
}).sync();
assert_eq!(r, Ok(35));
Source

pub fn success<TT, F>(self, f: F) -> Deferred<TT, E>
where F: Send + 'static + FnOnce(T) -> Result<TT, E>, TT: Send + 'static,

Executes a new task if the result of the previous promise is Ok. It may return a new type in a correct result (Ok), but it must return the same type of error of its previous promise.

asynchronous::Deferred::new(|| {
   Ok(1.23)
}).success(|res| {
   assert_eq!(res, 1.23);
   Ok(34)
}).success(|res| {
   assert_eq!(res, 34);
   if true { Ok(res) } else { Err("Final error")}
}).sync();
Source

pub fn fail<F>(self, f: F) -> Deferred<T, E>
where F: Send + 'static + FnOnce(E) -> Result<T, E>,

Executes a new task if the result of the previous promise is Err. It may return a new type in a correct result (Ok), but it must return the same type of error of its previous promise.

asynchronous::Deferred::new(|| {
   Err(32)
}).success(|res| {
   unreachable!();
   Ok(res)
}).fail(|err| {
   assert_eq!(err, 32);
   Ok("Value Ok")
}).sync();
Source

pub fn finally<F>(self, f: F)
where F: Send + 'static + FnOnce(Result<T, E>),

Executes one function with the result of the previous deferred. It doesn’t return anything and it’s completly asynchronous.

asynchronous::Deferred::new(|| {
   std::thread::sleep_ms(100);
   if true { Ok(32) } else { Err("Error txt") }
}).finally(|res| { 
   assert_eq!(res.unwrap(), 32);
});

let a = 2 + 3;  // This line is executed before the above Promise
 
Source

pub fn finally_sync<F>(self, f: F)
where F: Send + 'static + FnOnce(Result<T, E>),

Executes one function with the result of the previous deferred. It doesn’t return anything, but it’s synchronized with the caller

use asynchronous::Promise;
 
Promise::new(|| {
   std::thread::sleep_ms(100);
   if true { Ok(32) } else { Err("Error txt") }
}).finally_sync(|res| { 
   assert_eq!(res.unwrap(), 32);
});    

let a = 2 + 3;  // This line is executed after the above Promise
Source

pub fn to_promise(self) -> Promise<T, E>

Executes the task stored and returns a Promise

let deferred = asynchronous::Deferred::new(|| {
   // Do something  
   if true { Ok("value a") } else { Err("Error description") }
});
// deferred is only stored in memory waiting for .sync() or .to_promise()
deferred.to_promise();
// At this point "deferred" is executing
Source

pub fn vec_to_promise( vector: Vec<Deferred<T, E>>, control: ControlFlow, ) -> Promise<Vec<T>, Vec<Result<T, E>>>

Executes a vector of tasks and returns a Promise with a vector of values in case that all tasks ended ok. If there’s one or more tasks with error, the promise fails and returns a vector with all the Results.

use asynchronous::{Deferred, ControlFlow};
 
let mut vec_deferred = Vec::new();
for i in 0..5 { vec_deferred.push(Deferred::<_,&str>::new(move || Ok(i) )) }
let promise = Deferred::vec_to_promise(vec_deferred, ControlFlow::ParallelCPUS);
// At this point all tasks in "vec_deferred" are executing
assert_eq!(promise.sync().unwrap(), vec![0,1,2,3,4]);
Source

pub fn first_to_promise( num_first: usize, wait: bool, vector: Vec<Deferred<T, E>>, control: ControlFlow, ) -> Promise<Vec<T>, Vec<Result<T, E>>>

Executes a vector of tasks and returns a Promise with a vector of the first num_first tasks that return Ok. If the wait parameter is true, the promise waits for all tasks that are running at that moment. If it’s false, the promise returns once reaches the num_first tasks with Ok. If it’s not possible achieve this number, the promise fails and returns a vector with all the Results.

use asynchronous::{Deferred, ControlFlow};    
 
let mut v = vec![];
for i in 0..20 { v.push(Deferred::<u32, ()>::new(move ||{ Ok(i) })); }
// The first 5 tasks that ended with Ok
let _ = Deferred::first_to_promise(5, false, v, ControlFlow::ParallelLimit(3))   
.finally_sync(|res| {           
   assert_eq!(res.unwrap().len(), 5);                
});

Auto Trait Implementations§

§

impl<T, E> Freeze for Deferred<T, E>

§

impl<T, E> RefUnwindSafe for Deferred<T, E>

§

impl<T, E> Send for Deferred<T, E>
where T: Send, E: Send,

§

impl<T, E> !Sync for Deferred<T, E>

§

impl<T, E> Unpin for Deferred<T, E>

§

impl<T, E> UnwindSafe for Deferred<T, 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<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.