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§
Trait Implementations§
Source§impl<S, M> ResponseHandler<S, M> for Sfr<S, M>
The response handler for service future responses passes on
the producer in an envelope to be handled by the context
impl<S, M> ResponseHandler<S, M> for Sfr<S, M>
The response handler for service future responses passes on the producer in an envelope to be handled by the context
Auto Trait Implementations§
impl<S, M> Freeze for Sfr<S, M>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more