1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use tokio::sync::mpsc::{self, error::SendError};

/// Forwards items from a channel to another channel, applying a map function to each item
///
/// Returns an error if the sending channel is full
pub async fn forward_map_chan<T, R>(
    mut from: mpsc::Receiver<T>,
    to: mpsc::Sender<R>,
    map: impl Fn(T) -> R,
) -> Result<(), SendError<R>> {
    while let Some(item) = from.recv().await {
        let item = map(item);
        to.send(item).await?;
    }
    Ok(())
}