Trait remoc::rch::SendResultExt

source ·
pub trait SendResultExt {
    type Err: SendErrorExt;

    // Required methods
    fn into_closed(self) -> Result<bool, Self::Err>;
    fn into_disconnected(self) -> Result<bool, Self::Err>;
}
Available on crate feature rch only.
Expand description

Common functions to query results of send operations for details.

This is implemented by all results from send operations in this module.

Required Associated Types§

source

type Err: SendErrorExt

The error type of this result.

Required Methods§

source

fn into_closed(self) -> Result<bool, Self::Err>

Whether the remote endpoint closed the channel.

Returns Ok(false) if the send was successful and Ok(true) if the send failed for the above reason. The original error is returned if the failure had another cause.

§Example

In the following example the client counts upwards and the server closes the connections once it receives the number 5. The client stops counting as soon as sending fails. Note that the panic error path in the client would only be triggered by another error, such as a serialization error.

use remoc::prelude::*;

// This would be run on the client.
async fn client(mut tx: rch::base::Sender<u32>) {
    let mut n = 0;

    loop {
        if tx.send(n).await.into_closed().unwrap() {
            break;
        }
        n += 1;
    }

    assert!(n > 5);
}

// This would be run on the server.
async fn server(mut rx: rch::base::Receiver<u32>) {
    while let Some(n) = rx.recv().await.unwrap() {
        if n == 5 {
            rx.close().await;
        }
    }
}
source

fn into_disconnected(self) -> Result<bool, Self::Err>

Whether the remote endpoint closed the channel, was dropped or the connection failed.

Returns Ok(false) if the send was successful and Ok(true) if the send failed for the above reasons. The original error is returned if the failure had another cause.

§Example

In the following example the client counts upwards and the server drops the connections once it receives the number 5. The client stops counting as soon as sending fails. Note that the panic error path in the client would only be triggered by another error, such as a serialization error.

use remoc::prelude::*;

// This would be run on the client.
async fn client(mut tx: rch::base::Sender<u32>) {
    let mut n = 0;

    loop {
        if tx.send(n).await.into_disconnected().unwrap() {
            break;
        }
        n += 1;
    }

    assert!(n > 5);
}

// This would be run on the server.
async fn server(mut rx: rch::base::Receiver<u32>) {
    while let Some(n) = rx.recv().await.unwrap() {
        if n == 5 {
            break;
        }
    }
}

Implementations on Foreign Types§

source§

impl<E> SendResultExt for Result<(), E>
where E: SendErrorExt,

Implementors§