pub struct Link<S>(/* private fields */);
Expand description
Links are used to send and receive messages from services
you will receive a link when you start a service or through
the link()
fn on a service context
Links are cheaply clonable and can be passed between threads
Implementations§
Source§impl<S> Link<S>where
S: Service,
impl<S> Link<S>where
S: Service,
Sourcepub fn message_link<M>(&self) -> MessageLink<M>
pub fn message_link<M>(&self) -> MessageLink<M>
Creates a message link type from this link type this allows you to have links to multiple different services that accept a specific message type
Sourcepub async fn wait<F, R>(&self, action: F) -> LinkResult<R>
pub async fn wait<F, R>(&self, action: F) -> LinkResult<R>
Tells the service to complete and wait on the action which produce a future depending on the service and context. While the action is being awaited messages will not be accepted. The result of the action will be returned.
Mutable access to the service and the service context are provided to the closure
use interlink::prelude::*;
use std::time::Duration;
use tokio::time::sleep;
#[derive(Service)]
struct MyService;
#[tokio::test]
async fn test() {
let link: Link<MyService> = MyService {}.start();
let value = link.wait(|service, ctx| Box::pin(async move {
println!("Service waiting on processing loop");
sleep(Duration::from_millis(1000)).await;
println!("Action executed on service");
12
}))
.await
.unwrap();
assert_eq!(value, 12);
}
Sourcepub fn do_wait<F, R>(&self, action: F) -> LinkResult<()>
pub fn do_wait<F, R>(&self, action: F) -> LinkResult<()>
Tells the service to complete and wait on the action which produce a future depending on the service and context. While the action is being awaited messages will not be accepted.
Mutable access to the service and the service context are provided to the closure
use interlink::prelude::*;
use std::time::Duration;
use tokio::time::sleep;
#[derive(Service)]
struct MyService;
#[tokio::test]
async fn test() {
let link: Link<MyService> = MyService {}.start();
link.do_wait(|service, ctx| Box::pin(async move {
println!("Service waiting on processing loop");
sleep(Duration::from_millis(1000)).await;
println!("Action executed on service");
}))
.unwrap();
}
Sourcepub async fn send<M>(&self, msg: M) -> LinkResult<M::Response>
pub async fn send<M>(&self, msg: M) -> LinkResult<M::Response>
Sends a message to the service. The service must implement a Handler for the message. Will return the response value from the handler once awaited
use interlink::prelude::*;
#[derive(Service)]
struct Test;
#[derive(Message)]
#[msg(rtype = "String")]
struct MyMessage {
value: String,
}
impl Handler<MyMessage> for Test {
type Response = Mr<MyMessage>;
fn handle(&mut self, msg: MyMessage, ctx: &mut ServiceContext<Self>) -> Self::Response {
Mr(msg.value)
}
}
#[tokio::test]
async fn test() {
let link = Test {}.start();
let resp = link.send(MyMessage {
value: "Test123".to_string()
})
.await
.unwrap();
assert_eq!(&resp, "Test123")
}
Sourcepub fn do_send<M>(&self, msg: M) -> LinkResult<()>
pub fn do_send<M>(&self, msg: M) -> LinkResult<()>
Sends a message to the service. The service must implement a Handler for the message. Will not wait for a response from the service
use interlink::prelude::*;
#[derive(Service)]
struct Test;
#[derive(Message)]
struct MyMessage {
value: String,
}
impl Handler<MyMessage> for Test {
type Response = ();
fn handle(&mut self, msg: MyMessage, ctx: &mut ServiceContext<Self>) {
assert_eq!(&msg.value, "Test123");
}
}
#[tokio::test]
async fn test() {
let link = Test {}.start();
link.do_send(MyMessage {
value: "Test123".to_string()
})
.unwrap();
}
Sourcepub async fn exec<F, R>(&self, action: F) -> LinkResult<R>
pub async fn exec<F, R>(&self, action: F) -> LinkResult<R>
Executes the provided action on the service and service context awaiting the promise from this function will result in the return value of the closure. The provided closure is given mutable access to the service and context
use interlink::prelude::*;
#[derive(Service)]
struct Test {
value: String
}
#[tokio::test]
async fn test() {
let link = Test { value: "Test".to_string() }.start();
let value = link.exec(|service: &mut Test, _ctx| {
service.value.push('A');
service.value.clone()
})
.await
.expect("Failed to execute action on service");
assert_eq!(value, "TestA");
}
Sourcepub fn do_exec<F, R>(&self, action: F) -> LinkResult<()>
pub fn do_exec<F, R>(&self, action: F) -> LinkResult<()>
Executes the provided action on the service and service context ignoring the result of the action. The provided closure is given mutable access to the service and context
use interlink::prelude::*;
#[derive(Service)]
struct Test {
value: String
}
#[tokio::test]
async fn test() {
let link = Test { value: "Test".to_string() }.start();
link.do_exec(|service: &mut Test, _ctx| {
println!("Value: {}", service.value);
service.value.push('A');
println!("Value: {}", service.value);
})
.expect("Failed to execute action on service");
}