Skip to main content

fast_pull/core/
mock.rs

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