Execute

Struct Execute 

Source
pub struct Execute<E>(/* private fields */);
Expand description

Wraps a function or closure as a Service.

Use Execute to turn any async function into a service without defining a custom type.

§Examples

async fn handle_input(data: String) -> String {
    format!("Processed: {}", data)
}

let service = Execute::new(handle_input);
let result = service.execute("test".to_string()).await;
assert_eq!(result, "Processed: test");
let service = Execute::new(move |x: i32| async move { x * 2 });
let result = service.execute(5).await;
assert_eq!(result, 10);

Implementations§

Source§

impl<E> Execute<E>

Source

pub fn new<In, Out, F>(e: E) -> Self
where E: Fn(In) -> F + Send + Sync + 'static, In: Send + 'static, F: Future<Output = Out> + Send + 'static, Out: Send + 'static,

Creates a new service from a function or closure.

§Examples
let service = Execute::new(|msg: String| async move { format!("Echo: {}", msg) });

Trait Implementations§

Source§

impl<E: Clone> Clone for Execute<E>

Source§

fn clone(&self) -> Execute<E>

Returns a duplicate 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<E> Debug for Execute<E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<E, F, In, Out> Service<In> for Execute<E>
where E: Fn(In) -> F + Send + Sync, F: Future<Output = Out> + Send,

Source§

type Out = Out

The output type returned by this service.
Source§

fn execute(&self, input: In) -> impl Future<Output = Self::Out> + Send

Processes the input and returns the output. Read more
Source§

impl<E, F, Req, Res, Err> Service<Req> for Execute<E>
where E: Fn(Req) -> F + Send + Sync, F: Future<Output = Result<Res, Err>> + Send,

Available on crate features tower-service only.
Source§

type Response = Res

Responses given by the service.
Source§

type Error = Err

Errors produced by the service.
Source§

type Future = F

The future response value.
Source§

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>

Returns Poll::Ready(Ok(())) when the service is able to process requests. Read more
Source§

fn call(&mut self, req: Req) -> Self::Future

Process the request and return the response asynchronously. Read more

Auto Trait Implementations§

§

impl<E> Freeze for Execute<E>
where E: Freeze,

§

impl<E> RefUnwindSafe for Execute<E>
where E: RefUnwindSafe,

§

impl<E> Send for Execute<E>
where E: Send,

§

impl<E> Sync for Execute<E>
where E: Sync,

§

impl<E> Unpin for Execute<E>
where E: Unpin,

§

impl<E> UnwindSafe for Execute<E>
where E: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<In, Out, T> DynamicServiceExt<In, Out> for T
where In: Send + 'static, Out: Send + 'static, T: Service<In, Out = Out> + 'static,

Source§

fn into_dynamic(self) -> DynamicService<In, Out>

Available on crate features dynamic-service only.
Converts this service into a type-erased DynamicService.
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> ToOwned for T
where T: Clone,

Source§

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 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.