LCOV - code coverage report
Current view: top level - libs/utils/src/sync/duplex - mpsc.rs (source / functions) Coverage Total Hit
Test: aca806cab4756d7eb6a304846130f4a73a5d5393.info Lines: 86.7 % 15 13
Test Date: 2025-04-24 20:31:15 Functions: 62.5 % 16 10

            Line data    Source code
       1              : use tokio::sync::mpsc;
       2              : 
       3              : /// A bi-directional channel.
       4              : pub struct Duplex<S, R> {
       5              :     pub tx: mpsc::Sender<S>,
       6              :     pub rx: mpsc::Receiver<R>,
       7              : }
       8              : 
       9              : /// Creates a bi-directional channel.
      10              : ///
      11              : /// The channel will buffer up to the provided number of messages. Once the buffer is full,
      12              : /// attempts to send new messages will wait until a message is received from the channel.
      13              : /// The provided buffer capacity must be at least 1.
      14        20832 : pub fn channel<A: Send, B: Send>(buffer: usize) -> (Duplex<A, B>, Duplex<B, A>) {
      15        20832 :     let (tx_a, rx_a) = mpsc::channel::<A>(buffer);
      16        20832 :     let (tx_b, rx_b) = mpsc::channel::<B>(buffer);
      17        20832 : 
      18        20832 :     (Duplex { tx: tx_a, rx: rx_b }, Duplex { tx: tx_b, rx: rx_a })
      19        20832 : }
      20              : 
      21              : impl<S: Send, R: Send> Duplex<S, R> {
      22              :     /// Sends a value, waiting until there is capacity.
      23              :     ///
      24              :     /// A successful send occurs when it is determined that the other end of the channel has not hung up already.
      25       294552 :     pub async fn send(&self, x: S) -> Result<(), mpsc::error::SendError<S>> {
      26       294552 :         self.tx.send(x).await
      27            0 :     }
      28              : 
      29        20832 :     pub fn try_send(&self, x: S) -> Result<(), mpsc::error::TrySendError<S>> {
      30        20832 :         self.tx.try_send(x)
      31        20832 :     }
      32              : 
      33              :     /// Receives the next value for this receiver.
      34              :     ///
      35              :     /// This method returns `None` if the channel has been closed and there are
      36              :     /// no remaining messages in the channel's buffer.
      37       315306 :     pub async fn recv(&mut self) -> Option<R> {
      38       315306 :         self.rx.recv().await
      39            0 :     }
      40              : }
        

Generated by: LCOV version 2.1-beta