Skip to main content

fast_pull/core/
mock.rs

1extern crate alloc;
2use crate::{ProgressEntry, PullResult, PullStream, Puller};
3use alloc::{sync::Arc, vec::Vec};
4use bytes::Bytes;
5use futures::stream;
6
7pub fn build_mock_data(size: usize) -> Vec<u8> {
8    (0..size).map(|i| (i % 256) as u8).collect()
9}
10
11#[derive(Clone)]
12pub struct MockPuller(pub Arc<[u8]>);
13impl MockPuller {
14    pub fn new(data: &[u8]) -> Self {
15        Self(Arc::from(data))
16    }
17}
18impl Puller for MockPuller {
19    type Error = ();
20    async fn pull(
21        &mut self,
22        range: Option<&ProgressEntry>,
23    ) -> PullResult<impl PullStream<Self::Error>, Self::Error> {
24        let data = match range {
25            Some(r) => &self.0[r.start as usize..r.end as usize],
26            None => &self.0,
27        };
28        Ok(stream::iter(
29            data.chunks(2)
30                .map(|c| Ok(Bytes::from_iter(c.iter().cloned()))),
31        ))
32    }
33}