Struct interlink::msg::Sfr

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

Service Future Response

Response type from a handler where a future must be awaited on the processing loop of the service. While the result of this future is being processed no other messages will be handled.

This provides a mutable borrow of the service and the service context to the future that is being awaited.

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 = Sfr<Self, 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

        Sfr::new(move |service: &mut Test, ctx| {
            Box::pin(async move {
                // Some future that must be polled on the service loop
                sleep(Duration::from_millis(1000)).await;

                // Make use of the mutable access to service
                service.value = msg.value.clone();

                // 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<S, M> Sfr<S, M>where S: Service, M: Message,

source

pub fn new<P>(producer: P) -> Sfr<S, M>where for<'a> P: FnOnce(&'a mut S, &'a mut ServiceContext<S>) -> BoxFuture<'a, M::Response> + Send + 'static,

Creates a new service future response. Takes a fn which accepts mutable access to the service and its context and returns a boxed future with the same lifetime as the borrow

producer The producer fn

Trait Implementations§

source§

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

The response handler for service future responses passes on the producer in an envelope to be handled by the context

source§

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

Auto Trait Implementations§

§

impl<S, M> !RefUnwindSafe for Sfr<S, M>

§

impl<S, M> Send for Sfr<S, M>

§

impl<S, M> !Sync for Sfr<S, M>

§

impl<S, M> Unpin for Sfr<S, M>

§

impl<S, M> !UnwindSafe for Sfr<S, 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.