Struct interlink::link::Link

source ·
pub struct Link<S>(_);
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,

source

pub fn is_closed(&self) -> bool

Checks whether the underlying sender is closed

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

source

pub async fn wait<F, R>(&self, action: F) -> LinkResult<R>where for<'a> F: FnOnce(&'a mut S, &'a mut ServiceContext<S>) -> BoxFuture<'a, R> + Send + 'static, R: Send + 'static,

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);
}
source

pub fn do_wait<F, R>(&self, action: F) -> LinkResult<()>where for<'a> F: FnOnce(&'a mut S, &'a mut ServiceContext<S>) -> BoxFuture<'a, R> + Send + 'static, R: Send + 'static,

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();
}
source

pub async fn send<M>(&self, msg: M) -> LinkResult<M::Response>where M: Message, S: Handler<M>,

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")
}
source

pub fn do_send<M>(&self, msg: M) -> LinkResult<()>where M: Message, S: Handler<M>,

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();
}
source

pub async fn exec<F, R>(&self, action: F) -> LinkResult<R>where F: FnOnce(&mut S, &mut ServiceContext<S>) -> R + Send + 'static, R: Send + 'static,

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");
}
source

pub fn do_exec<F, R>(&self, action: F) -> LinkResult<()>where F: FnOnce(&mut S, &mut ServiceContext<S>) -> R + Send + 'static, R: Send + 'static,

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");
}
source

pub fn stop(&self)

Tells the associated service to stop processing messages. After this message is recieved no more messages will be processed.

Trait Implementations§

source§

impl<S> Clone for Link<S>

Clone implementation to clone inner sender for the link

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<S> !RefUnwindSafe for Link<S>

§

impl<S> Send for Link<S>

§

impl<S> Sync for Link<S>

§

impl<S> Unpin for Link<S>

§

impl<S> !UnwindSafe for Link<S>

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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.