Struct teloxide::prelude::Handler

source ·
pub struct Handler<'a, Input, Output, Descr = Unspecified> { /* private fields */ }
Expand description

An instance that receives an input and decides whether to break a chain or pass the value further.

In order to create this structure, you can use the predefined functions from crate.

The difference between chaining and branching

Handlers can be chained via Handler::chain and branched via Handler::branch. To understand the difference between the two, consider the following examples: a.chain(b).c and a.branch(b).c.

In a.chain(b).c, the handler a is given the rest of the handler chain, b and c; if a decides to pass the value further, it invokes b. Then, if b decides to pass the value further, it invokes c. Thus, the handler chain is linear.

In a.branch(b).c, if a decides to pass the value further, it invokes b. But since b is “branched”, it receives an empty chain, so it cannot invoke c. Instead, if b decides to continue execution (ControlFlow::Continue), a invokes c; otherwise (ControlFlow::Break), the process is terminated. The handler chain is nested.

To sum up, chaining looks like this:

a -> b -> c

And branching looks like this:

a -> b
  -> c

This is very crucial when b is a filter: if it is chained, it decides whether or not to call c, but when it is branched, whether c is called depends solely on a.

Implementations§

source§

impl<'a, Input, Output, Descr> Handler<'a, Input, Output, Descr>where Input: Send + 'a, Output: 'a, Descr: HandlerDescription,

source

pub fn chain( self, next: Handler<'a, Input, Output, Descr> ) -> Handler<'a, Input, Output, Descr>

Chain two handlers to form a chain of responsibility.

Chaining is different from branching. See “The difference between chaining and branching”.

Examples
use dptree::prelude::*;

let handler: Handler<_, _> =
    dptree::filter(|x: i32| x > 0).chain(dptree::endpoint(|| async { "done" }));

assert_eq!(handler.dispatch(dptree::deps![10]).await, ControlFlow::Break("done"));
assert_eq!(
    handler.dispatch(dptree::deps![-10]).await,
    ControlFlow::Continue(dptree::deps![-10])
);
source

pub fn branch( self, next: Handler<'a, Input, Output, Descr> ) -> Handler<'a, Input, Output, Descr>where Output: Send,

Chain two handlers to make a tree of responsibility.

Chaining is different from branching. See “The difference between chaining and branching”.

Examples
use dptree::prelude::*;


#[derive(Debug, PartialEq)]
enum Output {
    Five,
    One,
    GT,
}

let dispatcher: Handler<_, _> = dptree::entry()
    .branch(dptree::filter(|num: i32| num == 5).endpoint(|| async move { Output::Five }))
    .branch(dptree::filter(|num: i32| num == 1).endpoint(|| async move { Output::One }))
    .branch(dptree::filter(|num: i32| num > 2).endpoint(|| async move { Output::GT }));

assert_eq!(dispatcher.dispatch(dptree::deps![5]).await, ControlFlow::Break(Output::Five));
assert_eq!(dispatcher.dispatch(dptree::deps![1]).await, ControlFlow::Break(Output::One));
assert_eq!(dispatcher.dispatch(dptree::deps![3]).await, ControlFlow::Break(Output::GT));
assert_eq!(
    dispatcher.dispatch(dptree::deps![0]).await,
    ControlFlow::Continue(dptree::deps![0])
);
source

pub async fn execute<Cont, ContFut>( self, container: Input, cont: Cont ) -> impl Future<Output = ControlFlow<Output, Input>>where Cont: FnOnce(Input) -> ContFut + Send + Sync + 'a, ContFut: Future<Output = ControlFlow<Output, Input>> + Send + 'a,

Executes this handler with a continuation.

Usually, you do not want to call this method by yourself, if you do not write your own handler implementation. If you wish to execute handler without a continuation, take a look at the Handler::dispatch method.

Examples
use dptree::prelude::*;

let handler: Handler<_, _> = dptree::filter(|x: i32| x > 0);

let output = handler.execute(dptree::deps![10], |_| async { ControlFlow::Break("done") }).await;
assert_eq!(output, ControlFlow::Break("done"));
source

pub async fn dispatch( &self, container: Input ) -> impl Future<Output = ControlFlow<Output, Input>>

Executes this handler.

Returns ControlFlow::Break when executed successfully, ControlFlow::Continue otherwise.

source

pub fn description(&self) -> &Descr

Returns the set of updates that can be processed by this handler.

source§

impl<'a, Input, Output, Descr> Handler<'a, Input, Output, Descr>where Input: Send + 'a, Output: 'a, Descr: HandlerDescription,

source

pub fn filter<Pred, FnArgs>( self, pred: Pred ) -> Handler<'a, Input, Output, Descr>where Asyncify<Pred>: Injectable<Input, bool, FnArgs> + Send + Sync + 'a,

Chain this handler with the filter predicate pred.

source

pub fn filter_async<Pred, FnArgs>( self, pred: Pred ) -> Handler<'a, Input, Output, Descr>where Pred: Injectable<Input, bool, FnArgs> + Send + Sync + 'a,

Chain this handler with the async filter predicate pred.

source

pub fn filter_map<Proj, NewType, Args>( self, proj: Proj ) -> Handler<'a, Input, Output, Descr>where Input: Insert<NewType> + Clone, Asyncify<Proj>: Injectable<Input, Option<NewType>, Args> + Send + Sync + 'a, NewType: Send,

Chain this handler with the filter projection proj.

source

pub fn filter_map_async<Proj, NewType, Args>( self, proj: Proj ) -> Handler<'a, Input, Output, Descr>where Input: Insert<NewType> + Clone, Proj: Injectable<Input, Option<NewType>, Args> + Send + Sync + 'a, NewType: Send,

Chain this handler with the async filter projection proj.

source

pub fn map<Proj, NewType, Args>( self, proj: Proj ) -> Handler<'a, Input, Output, Descr>where Input: Insert<NewType> + Clone, Asyncify<Proj>: Injectable<Input, NewType, Args> + Send + Sync + 'a, NewType: Send,

Chain this handler with the map projection proj.

source

pub fn map_async<Proj, NewType, Args>( self, proj: Proj ) -> Handler<'a, Input, Output, Descr>where Input: Insert<NewType> + Clone, Proj: Injectable<Input, NewType, Args> + Send + Sync + 'a, NewType: Send,

Chain this handler with the async map projection proj.

source

pub fn inspect<F, Args>(self, f: F) -> Handler<'a, Input, Output, Descr>where Asyncify<F>: Injectable<Input, (), Args> + Send + Sync + 'a,

Chain this handler with the inspection function f.

source

pub fn inspect_async<F, Args>(self, f: F) -> Handler<'a, Input, Output, Descr>where F: Injectable<Input, (), Args> + Send + Sync + 'a,

Chain this handler with the async inspection function f.

source

pub fn endpoint<F, FnArgs>(self, f: F) -> Handler<'a, Input, Output, Descr>where F: Injectable<Input, Output, FnArgs> + Send + Sync + 'a,

Chain this handler with the endpoint handler f.

Trait Implementations§

source§

impl<'a, Input, Output, Descr> Clone for Handler<'a, Input, Output, Descr>

source§

fn clone(&self) -> Handler<'a, Input, Output, Descr>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Output> HandlerExt<Output> for Handler<'static, DependencyMap, Output, DpHandlerDescription>where Output: Send + Sync + 'static,

source§

fn filter_command<C>(self) -> Selfwhere C: BotCommands + Send + Sync + 'static,

Returns a handler that accepts a parsed command C. Read more
source§

fn enter_dialogue<Upd, S, D>(self) -> Selfwhere S: Storage<D> + ?Sized + Send + Sync + 'static, <S as Storage<D>>::Error: Debug + Send, D: Default + Send + Sync + 'static, Upd: GetChatId + Clone + Send + Sync + 'static,

Passes Dialogue<D, S> and D as handler dependencies. Read more

Auto Trait Implementations§

§

impl<'a, Input, Output, Descr = Unspecified> !RefUnwindSafe for Handler<'a, Input, Output, Descr>

§

impl<'a, Input, Output, Descr> Send for Handler<'a, Input, Output, Descr>where Descr: Send + Sync,

§

impl<'a, Input, Output, Descr> Sync for Handler<'a, Input, Output, Descr>where Descr: Send + Sync,

§

impl<'a, Input, Output, Descr> Unpin for Handler<'a, Input, Output, Descr>

§

impl<'a, Input, Output, Descr = Unspecified> !UnwindSafe for Handler<'a, Input, Output, Descr>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Erasable for T

§

unsafe fn unerase(this: NonNull<Erased>) -> NonNull<T>

Unerase this erased pointer. Read more
§

const ACK_1_1_0: bool = true

Available on non-enforce_1_1_0_semantics only.
Whether this implementor has acknowledged the 1.1.0 update to unerase’s documented implementation requirements. Read more
§

fn erase(this: NonNull<Self>) -> NonNull<Erased>

Turn this erasable pointer into an erased pointer. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for Twhere T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more