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