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///
13/// # Cancel safety
14///
15/// Transfer safely stops if the future is dropped.
16pub async fn async_transfer<T, As: AsyncConsumer<Item = T>, Ad: AsyncProducer<Item = T>>(
17 src: &mut As,
18 dst: &mut Ad,
19 count: Option<usize>,
20) -> usize {
21 let mut actual_count = 0;
22 // TODO: Transfer multiple items at once.
23 loop {
24 if count.as_ref().is_some_and(|n| actual_count == *n) {
25 break;
26 }
27 match dst
28 .push(match src.pop().await {
29 Some(item) => item,
30 None => break,
31 })
32 .await
33 {
34 Ok(()) => actual_count += 1,
35 Err(_item) => break,
36 };
37 }
38 actual_count
39}