Struct interlink::msg::Fr

source ·
pub struct Fr<M: Message> { /* private fields */ }
Expand description

Future Response

Response type from a handler containing a future that is to be spawned into a another task where the response will then be sent to the sender. This should be used when the response is computed in a future that can run independently from the service.

use interlink::prelude::*;
use std::time::Duration;
use tokio::time::sleep;

#[derive(Service)]
struct Test { value: String };

#[derive(Message)]
#[msg(rtype = "String")]
struct TestMessage {
    value: String,
}

impl Handler<TestMessage> for Test {
    type Response = Fr<TestMessage>;

    fn handle(&mut self, msg: TestMessage, ctx: &mut ServiceContext<Self>) -> Self::Response {
        // Additional logic can be run here before the future
        // response is created

        Fr::new(Box::pin(async move {
            // Some future that must be polled in another task
            sleep(Duration::from_millis(1000)).await;

            // You can return the response type of the message here
            "Response".to_string()
       }))
    }
}

#[tokio::test]
async fn test() {
    let service = Test { value: "Default".to_string() };
    let link = service.start();
     
    let res: String = link
        .send(TestMessage {
            value: "Example".to_string()
        })
        .await
        .unwrap();
     
    assert_eq!(&res, "Response")    

}

Implementations§

source§

impl<M> Fr<M>where M: Message,

source

pub fn new(future: BoxFuture<'static, M::Response>) -> Fr<M>

Creates a new future response from the provided boxed future.

source

pub fn new_box<F>(future: F) -> Fr<M>where F: Future<Output = M::Response> + Send + 'static,

Creates a Fr wrapping the provided future creating a boxed future from the provided future. Don’t use this if you’ve already boxed the future

source

pub fn ready(value: M::Response) -> Fr<M>

Creates a new future response for a ready future containing a value that is already ready.

Trait Implementations§

source§

impl<S, M> ResponseHandler<S, M> for Fr<M>where S: Service, M: Message,

source§

fn respond( self, _service: &mut S, _ctx: &mut ServiceContext<S>, tx: Option<Sender<M::Response>> )

Auto Trait Implementations§

§

impl<M> !RefUnwindSafe for Fr<M>

§

impl<M> Send for Fr<M>

§

impl<M> !Sync for Fr<M>

§

impl<M> Unpin for Fr<M>

§

impl<M> !UnwindSafe for Fr<M>

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,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.