async_ringbuf/
transfer.rs

1use crate::{consumer::AsyncConsumer, producer::AsyncProducer};
2
3/// Tranfer data from one ring buffer to another.
4///
5/// `count` is the number of items to transfer.
6/// The number of actually transfered items is returned.
7///
8/// If `count` is `Some(n)` then exactly `n` items will be transfered on completion
9/// except the situation when `src` or `dst` is closed or the future is dropped before completion.
10///
11/// If `count` is `None` then transfer will be performed until the one or another ring buffer is closed.
12/// Transfer also safely stopped if the future is dropped.
13pub async fn async_transfer<T, As: AsyncConsumer<Item = T>, Ad: AsyncProducer<Item = T>>(
14    src: &mut As,
15    dst: &mut Ad,
16    count: Option<usize>,
17) -> usize {
18    let mut actual_count = 0;
19    // TODO: Transfer multiple items at once.
20    loop {
21        if count.as_ref().map_or(false, |n| actual_count == *n) {
22            break;
23        }
24        actual_count += 1;
25
26        match dst
27            .push(match src.pop().await {
28                Some(item) => item,
29                None => break,
30            })
31            .await
32        {
33            Ok(()) => (),
34            Err(_item) => break,
35        };
36    }
37    actual_count
38}