segsource/sources/
vec_source.rs1#[cfg(feature = "async")]
2use crate::sync::async_u8_vec_from_file;
3use crate::{Endidness, Result, Segment, Source, U8Source};
4#[cfg(not(feature = "std"))]
5use alloc::vec::Vec;
6#[cfg(feature = "with_bytes")]
7use bytes::Bytes;
8#[cfg(feature = "std")]
9use std::{fs, io::Read as _, path::Path};
10
11#[cfg(feature = "async")]
12use async_trait::async_trait;
13
14pub struct VecSource<I: Sync + Send> {
16 initial_offset: usize,
17 data: Vec<I>,
18 endidness: Endidness,
19}
20
21impl<I: Sync + Send> VecSource<I> {
22 #[inline]
23 fn new(data: Vec<I>, initial_offset: usize, endidness: Endidness) -> Self {
24 Self {
25 initial_offset,
26 data,
27 endidness,
28 }
29 }
30}
31
32impl<I: Sync + Send> Source for VecSource<I> {
33 type Item = I;
34
35 #[inline]
36 fn initial_offset(&self) -> usize {
37 self.initial_offset
38 }
39
40 #[inline]
41 fn size(&self) -> usize {
42 self.data.len() as usize
43 }
44
45 #[inline]
46 fn from_vec_with_offset(items: Vec<Self::Item>, initial_offset: usize) -> Result<Self> {
47 Ok(Self {
48 initial_offset,
49 data: items,
50 endidness: Endidness::default(),
51 })
52 }
53
54 fn segment(&self, start: usize, end: usize) -> Result<Segment<I>> {
55 self.validate_offset(start)?;
56 self.validate_offset(end)?;
57 Ok(Segment::with_offset(
58 &self.data
59 [(start - self.initial_offset) as usize..(end - self.initial_offset) as usize],
60 start,
61 ))
62 }
63}
64
65#[cfg_attr(feature = "async", async_trait)]
66impl U8Source for VecSource<u8> {
67 #[inline]
68 fn endidness(&self) -> Endidness {
69 self.endidness
70 }
71
72 #[inline]
73 fn change_endidness(&mut self, endidness: Endidness) {
74 self.endidness = endidness;
75 }
76
77 #[cfg(feature = "async")]
78 async fn from_file_with_offset_async<P>(
79 path: P,
80 initial_offset: usize,
81 endidness: Endidness,
82 ) -> Result<Self>
83 where
84 P: AsRef<Path> + Sync + Send,
85 {
86 Ok(Self::new(
87 async_u8_vec_from_file(path).await?,
88 initial_offset,
89 endidness,
90 ))
91 }
92
93 #[cfg(feature = "std")]
94 #[inline]
95 fn from_file_with_offset<P: AsRef<Path>>(
96 path: P,
97 initial_offset: usize,
98 endidness: Endidness,
99 ) -> Result<Self> {
100 let md = path.as_ref().metadata()?;
101 let mut data = Vec::with_capacity(md.len() as usize);
102 {
103 let mut file = fs::File::open(path)?;
104 file.read_to_end(&mut data)?;
105 }
106 Ok(Self::new(data, initial_offset, endidness))
107 }
108
109 #[inline]
110 #[cfg(feature = "with_bytes")]
111 fn from_bytes_with_offset(
112 bytes: Bytes,
113 initial_offset: usize,
114 endidness: Endidness,
115 ) -> Result<Self> {
116 Ok(Self::new(
117 bytes.into_iter().collect(),
118 initial_offset,
119 endidness,
120 ))
121 }
122
123 #[inline]
124 fn from_u8_vec_with_offset(
125 items: Vec<u8>,
126 initial_offset: usize,
127 endidness: Endidness,
128 ) -> Result<Self> {
129 Ok(Self::new(items, initial_offset, endidness))
130 }
131
132 fn from_u8_slice_with_offset(
133 items: &[u8],
134 initial_offset: usize,
135 endidness: Endidness,
136 ) -> Result<Self> {
137 Ok(Self::new(Vec::from(items), initial_offset, endidness))
138 }
139
140 }