1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#[cfg(feature = "async")]
use crate::sync::async_u8_vec_from_file;
use crate::{Endidness, Result, Segment, Source, U8Source};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[cfg(feature = "with_bytes")]
use bytes::Bytes;
#[cfg(feature = "std")]
use std::{fs, io::Read as _, path::Path};

#[cfg(feature = "async")]
use async_trait::async_trait;

/// A [`Source`] that uses a `Vec` to store its data.
pub struct VecSource<I: Sync + Send> {
    initial_offset: usize,
    data: Vec<I>,
    endidness: Endidness,
}

impl<I: Sync + Send> VecSource<I> {
    #[inline]
    fn new(data: Vec<I>, initial_offset: usize, endidness: Endidness) -> Self {
        Self {
            initial_offset,
            data,
            endidness,
        }
    }
}

impl<I: Sync + Send> Source for VecSource<I> {
    type Item = I;

    #[inline]
    fn initial_offset(&self) -> usize {
        self.initial_offset
    }

    #[inline]
    fn size(&self) -> usize {
        self.data.len() as usize
    }

    #[inline]
    fn from_vec_with_offset(items: Vec<Self::Item>, initial_offset: usize) -> Result<Self> {
        Ok(Self {
            initial_offset,
            data: items,
            endidness: Endidness::default(),
        })
    }

    fn segment(&self, start: usize, end: usize) -> Result<Segment<I>> {
        self.validate_offset(start)?;
        self.validate_offset(end)?;
        Ok(Segment::with_offset(
            &self.data
                [(start - self.initial_offset) as usize..(end - self.initial_offset) as usize],
            start,
        ))
    }
}

#[cfg_attr(feature = "async", async_trait)]
impl U8Source for VecSource<u8> {
    #[inline]
    fn endidness(&self) -> Endidness {
        self.endidness
    }

    #[inline]
    fn change_endidness(&mut self, endidness: Endidness) {
        self.endidness = endidness;
    }

    #[cfg(feature = "async")]
    async fn from_file_with_offset_async<P>(
        path: P,
        initial_offset: usize,
        endidness: Endidness,
    ) -> Result<Self>
    where
        P: AsRef<Path> + Sync + Send,
    {
        Ok(Self::new(
            async_u8_vec_from_file(path).await?,
            initial_offset,
            endidness,
        ))
    }

    #[cfg(feature = "std")]
    #[inline]
    fn from_file_with_offset<P: AsRef<Path>>(
        path: P,
        initial_offset: usize,
        endidness: Endidness,
    ) -> Result<Self> {
        let md = path.as_ref().metadata()?;
        let mut data = Vec::with_capacity(md.len() as usize);
        {
            let mut file = fs::File::open(path)?;
            file.read_to_end(&mut data)?;
        }
        Ok(Self::new(data, initial_offset, endidness))
    }

    #[inline]
    #[cfg(feature = "with_bytes")]
    fn from_bytes_with_offset(
        bytes: Bytes,
        initial_offset: usize,
        endidness: Endidness,
    ) -> Result<Self> {
        Ok(Self::new(
            bytes.into_iter().collect(),
            initial_offset,
            endidness,
        ))
    }

    #[inline]
    fn from_u8_vec_with_offset(
        items: Vec<u8>,
        initial_offset: usize,
        endidness: Endidness,
    ) -> Result<Self> {
        Ok(Self::new(items, initial_offset, endidness))
    }

    fn from_u8_slice_with_offset(
        items: &[u8],
        initial_offset: usize,
        endidness: Endidness,
    ) -> Result<Self> {
        Ok(Self::new(Vec::from(items), initial_offset, endidness))
    }

    //fn u8_segment(&self, start: usize, end: usize) -> Result<Segment<u8>> {
    //self.validate_offset(start)?;
    //self.validate_offset(end)?;
    //Ok(Segment::with_offset_and_endidness(
    //&self.data
    //[(start - self.initial_offset) as usize..(end - self.initial_offset) as usize],
    //start,
    //self.endidness,
    //))
    //}
}