pub trait TryOp: Send + Sync {
type Input: Send + Sync;
type Output: Send + Sync;
type Error: Send + Sync;
// Required method
fn try_call(
&self,
input: Self::Input,
) -> impl Future<Output = Result<Self::Output, Self::Error>> + Send;
// Provided methods
fn try_batch_call<I>(
&self,
n: usize,
input: I,
) -> impl Future<Output = Result<Vec<Self::Output>, Self::Error>> + Send
where I: IntoIterator<Item = Self::Input> + Send,
I::IntoIter: Send,
Self: Sized { ... }
fn map_ok<F, Output>(self, f: F) -> MapOk<Self, Map<F, Self::Output>>
where F: Fn(Self::Output) -> Output + Send + Sync,
Output: Send + Sync,
Self: Sized { ... }
fn map_err<F, E>(self, f: F) -> MapErr<Self, Map<F, Self::Error>>
where F: Fn(Self::Error) -> E + Send + Sync,
E: Send + Sync,
Self: Sized { ... }
fn and_then<F, Fut, Output>(
self,
f: F,
) -> AndThen<Self, Then<F, Self::Output>>
where F: Fn(Self::Output) -> Fut + Send + Sync,
Fut: Future<Output = Result<Output, Self::Error>> + Send + Sync,
Output: Send + Sync,
Self: Sized { ... }
fn or_else<F, Fut, E>(self, f: F) -> OrElse<Self, Then<F, Self::Error>>
where F: Fn(Self::Error) -> Fut + Send + Sync,
Fut: Future<Output = Result<Self::Output, E>> + Send + Sync,
E: Send + Sync,
Self: Sized { ... }
fn chain_ok<T>(self, op: T) -> TrySequential<Self, T>
where T: Op<Input = Self::Output>,
Self: Sized { ... }
}
Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn try_batch_call<I>(
&self,
n: usize,
input: I,
) -> impl Future<Output = Result<Vec<Self::Output>, Self::Error>> + Send
fn try_batch_call<I>( &self, n: usize, input: I, ) -> impl Future<Output = Result<Vec<Self::Output>, Self::Error>> + Send
Execute the current op with the given inputs. n
is the number of concurrent
inputs that will be processed concurrently.
If the op fails for one of the inputs, the entire operation will fail and the error will
be returned.
§Example
use rig::pipeline::{self, TryOp};
let op = pipeline::new()
.map(|x: i32| if x % 2 == 0 { Ok(x + 1) } else { Err("x is odd") });
// Execute the pipeline concurrently with 2 inputs
let result = op.try_batch_call(2, vec![2, 4]).await;
assert_eq!(result, Ok(vec![3, 5]));
Sourcefn map_ok<F, Output>(self, f: F) -> MapOk<Self, Map<F, Self::Output>>
fn map_ok<F, Output>(self, f: F) -> MapOk<Self, Map<F, Self::Output>>
Map the success return value (i.e., Ok
) of the current op to a different value
using the provided closure.
§Example
use rig::pipeline::{self, TryOp};
let op = pipeline::new()
.map(|x: i32| if x % 2 == 0 { Ok(x) } else { Err("x is odd") })
.map_ok(|x| x * 2);
let result = op.try_call(2).await;
assert_eq!(result, Ok(4));
Sourcefn map_err<F, E>(self, f: F) -> MapErr<Self, Map<F, Self::Error>>
fn map_err<F, E>(self, f: F) -> MapErr<Self, Map<F, Self::Error>>
Map the error return value (i.e., Err
) of the current op to a different value
using the provided closure.
§Example
use rig::pipeline::{self, TryOp};
let op = pipeline::new()
.map(|x: i32| if x % 2 == 0 { Ok(x) } else { Err("x is odd") })
.map_err(|err| format!("Error: {}", err));
let result = op.try_call(1).await;
assert_eq!(result, Err("Error: x is odd".to_string()));
Sourcefn and_then<F, Fut, Output>(self, f: F) -> AndThen<Self, Then<F, Self::Output>>
fn and_then<F, Fut, Output>(self, f: F) -> AndThen<Self, Then<F, Self::Output>>
Chain a function to the current op. The function will only be called
if the current op returns Ok
. The function must return a Future
with value
Result<T, E>
where E
is the same type as the error type of the current.
§Example
use rig::pipeline::{self, TryOp};
let op = pipeline::new()
.map(|x: i32| if x % 2 == 0 { Ok(x) } else { Err("x is odd") })
.and_then(|x| async move { Ok(x * 2) });
let result = op.try_call(2).await;
assert_eq!(result, Ok(4));
Sourcefn or_else<F, Fut, E>(self, f: F) -> OrElse<Self, Then<F, Self::Error>>
fn or_else<F, Fut, E>(self, f: F) -> OrElse<Self, Then<F, Self::Error>>
Chain a function f
to the current op. The function f
will only be called
if the current op returns Err
. f
must return a Future
with value
Result<T, E>
where T
is the same type as the output type of the current op.
§Example
use rig::pipeline::{self, TryOp};
let op = pipeline::new()
.map(|x: i32| if x % 2 == 0 { Ok(x) } else { Err("x is odd") })
.or_else(|err| async move { Err(format!("Error: {}", err)) });
let result = op.try_call(1).await;
assert_eq!(result, Err("Error: x is odd".to_string()));
Sourcefn chain_ok<T>(self, op: T) -> TrySequential<Self, T>
fn chain_ok<T>(self, op: T) -> TrySequential<Self, T>
Chain a new op op
to the current op. The new op will be called with the success
return value of the current op (i.e.: Ok
value). The chained op can be any type that
implements the Op
trait.
§Example
use rig::pipeline::{self, TryOp};
struct AddOne;
impl Op for AddOne {
type Input = i32;
type Output = i32;
async fn call(&self, input: Self::Input) -> Self::Output {
input + 1
}
}
let op = pipeline::new()
.map(|x: i32| if x % 2 == 0 { Ok(x) } else { Err("x is odd") })
.chain_ok(MyOp);
let result = op.try_call(2).await;
assert_eq!(result, Ok(3));
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.