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,
impl<M> Fr<M>where
M: Message,
Sourcepub fn new(future: BoxFuture<'static, M::Response>) -> Fr<M>
pub fn new(future: BoxFuture<'static, M::Response>) -> Fr<M>
Creates a new future response from the provided boxed future.
Trait Implementations§
Auto Trait Implementations§
impl<M> Freeze for Fr<M>
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> 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