FallibleExt

Trait FallibleExt 

Source
pub trait FallibleExt<T> {
    // Required methods
    fn send_lossy(&self, msg: T) -> bool;
    fn request<R, F>(
        &self,
        make_msg: F,
    ) -> impl Future<Output = Option<R>> + Send
       where R: Send,
             F: FnOnce(Sender<R>) -> T + Send;
    fn request_or<R, F>(
        &self,
        make_msg: F,
        default: R,
    ) -> impl Future<Output = R> + Send
       where R: Send,
             F: FnOnce(Sender<R>) -> T + Send;
    fn request_or_default<R, F>(
        &self,
        make_msg: F,
    ) -> impl Future<Output = R> + Send
       where R: Default + Send,
             F: FnOnce(Sender<R>) -> T + Send;
}
Expand description

Extension trait for channel operations that may fail due to disconnection.

Use these methods when the receiver may be dropped during shutdown and you want to handle that gracefully rather than panicking.

Required Methods§

Source

fn send_lossy(&self, msg: T) -> bool

Send a message, returning true if successful.

Use this for fire-and-forget messages where the receiver may have been dropped during shutdown. The return value can be ignored if the caller doesn’t need to know whether the send succeeded.

Source

fn request<R, F>(&self, make_msg: F) -> impl Future<Output = Option<R>> + Send
where R: Send, F: FnOnce(Sender<R>) -> T + Send,

Send a request message containing a oneshot responder and await the response.

Returns None if:

  • The receiver has been dropped (send fails)
  • The responder is dropped without sending (receive fails)
§Example
let dialable: Option<Vec<PublicKey>> = sender
    .request(|tx| Message::Dialable { responder: tx })
    .await;
Source

fn request_or<R, F>( &self, make_msg: F, default: R, ) -> impl Future<Output = R> + Send
where R: Send, F: FnOnce(Sender<R>) -> T + Send,

Send a request and return the provided default on failure.

This is a convenience wrapper around request for cases where you have a sensible default value.

Source

fn request_or_default<R, F>( &self, make_msg: F, ) -> impl Future<Output = R> + Send
where R: Default + Send, F: FnOnce(Sender<R>) -> T + Send,

Send a request and return R::default() on failure.

This is a convenience wrapper around request for types that implement Default.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: Send> FallibleExt<T> for UnboundedSender<T>

Source§

fn send_lossy(&self, msg: T) -> bool

Source§

async fn request<R, F>(&self, make_msg: F) -> Option<R>
where R: Send, F: FnOnce(Sender<R>) -> T + Send,

Source§

async fn request_or<R, F>(&self, make_msg: F, default: R) -> R
where R: Send, F: FnOnce(Sender<R>) -> T + Send,

Source§

async fn request_or_default<R, F>(&self, make_msg: F) -> R
where R: Default + Send, F: FnOnce(Sender<R>) -> T + Send,

Implementors§