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 actual_count += 1;
28
29 match dst
30 .push(match src.pop().await {
31 Some(item) => item,
32 None => break,
33 })
34 .await
35 {
36 Ok(()) => (),
37 Err(_item) => break,
38 };
39 }
40 actual_count
41}