io_arrays/
slice.rs

1use crate::borrow_streamer::BorrowStreamer;
2use crate::{Advice, Array, Metadata, ReadAt, WriteAt};
3#[cfg(feature = "io-streams")]
4use io_streams::StreamReader;
5use std::cmp::min;
6use std::io::{self, IoSlice, IoSliceMut, Read};
7
8impl Array for [u8] {
9    #[inline]
10    fn metadata(&self) -> io::Result<Metadata> {
11        Ok(Metadata {
12            len: self.len().try_into().unwrap(),
13            // An array in memory doesn't have a natural "block size" in the
14            // way that filesystems do, so currently this is an arbitrarily
15            // chosen value. In the future this could be guided by performance
16            // measurements.
17            blksize: 4096,
18        })
19    }
20
21    #[inline]
22    fn advise(&self, _offset: u64, _len: u64, _advice: Advice) -> io::Result<()> {
23        Ok(())
24    }
25}
26
27impl Array for &mut [u8] {
28    #[inline]
29    fn metadata(&self) -> io::Result<Metadata> {
30        Ok(Metadata {
31            len: self.len().try_into().unwrap(),
32            // An array in memory doesn't have a natural "block size" in the
33            // way that filesystems do, so currently this is an arbitrarily
34            // chosen value. In the future this could be guided by performance
35            // measurements.
36            blksize: 4096,
37        })
38    }
39
40    #[inline]
41    fn advise(&self, _offset: u64, _len: u64, _advice: Advice) -> io::Result<()> {
42        Ok(())
43    }
44}
45
46impl ReadAt for [u8] {
47    #[inline]
48    fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
49        self.read_exact_at(buf, offset)?;
50        Ok(buf.len())
51    }
52
53    fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> {
54        let offset = offset
55            .try_into()
56            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
57        let at: &[u8] = self.get(offset..).unwrap_or(&[]);
58        let len = min(at.len(), buf.len());
59        buf[..len].copy_from_slice(&at[..len]);
60        Ok(())
61    }
62
63    fn read_vectored_at(&self, bufs: &mut [IoSliceMut], offset: u64) -> io::Result<usize> {
64        let initial_offset = offset
65            .try_into()
66            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
67        let mut running_offset = initial_offset;
68        for buf in bufs {
69            let at = self.get(running_offset..).unwrap_or(&[]);
70            let len = min(at.len(), buf.len());
71            buf.copy_from_slice(&at[..len]);
72            running_offset += len;
73        }
74        Ok(running_offset - initial_offset)
75    }
76
77    fn read_exact_vectored_at(&self, bufs: &mut [IoSliceMut], offset: u64) -> io::Result<()> {
78        let mut running_offset = offset
79            .try_into()
80            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
81        for buf in bufs {
82            let at = self.get(running_offset..).unwrap_or(&[]);
83            if at.len() < buf.len() {
84                return Err(io::Error::new(
85                    io::ErrorKind::UnexpectedEof,
86                    "failed to fill whole buffer",
87                ));
88            }
89            let len = buf.len();
90            buf.copy_from_slice(&at[..len]);
91            running_offset += len;
92        }
93        Ok(())
94    }
95
96    #[inline]
97    fn is_read_vectored_at(&self) -> bool {
98        true
99    }
100
101    #[cfg(feature = "io-streams")]
102    fn read_via_stream_at(&self, _offset: u64) -> io::Result<StreamReader> {
103        todo!("slice::read_via_stream_at")
104    }
105}
106
107impl WriteAt for [u8] {
108    #[inline]
109    fn write_at(&mut self, buf: &[u8], offset: u64) -> io::Result<usize> {
110        self.write_all_at(buf, offset)?;
111        Ok(buf.len())
112    }
113
114    fn write_all_at(&mut self, buf: &[u8], offset: u64) -> io::Result<()> {
115        let offset = offset
116            .try_into()
117            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
118        let at = self.get_mut(offset..).unwrap_or(&mut []);
119        let len = min(at.len(), buf.len());
120        at[..len].copy_from_slice(&buf[..len]);
121        Ok(())
122    }
123
124    fn write_vectored_at(&mut self, bufs: &[IoSlice], offset: u64) -> io::Result<usize> {
125        let initial_offset = offset
126            .try_into()
127            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
128        let mut running_offset = initial_offset;
129        for buf in bufs {
130            let at = self.get_mut(running_offset..).unwrap_or(&mut []);
131            let len = min(at.len(), buf.len());
132            at[..len].copy_from_slice(buf);
133            running_offset += len;
134        }
135        Ok(running_offset - initial_offset)
136    }
137
138    fn write_all_vectored_at(&mut self, bufs: &mut [IoSlice], offset: u64) -> io::Result<()> {
139        let mut running_offset = offset
140            .try_into()
141            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
142        for buf in bufs {
143            let at = self.get_mut(running_offset..).unwrap_or(&mut []);
144            if at.len() < buf.len() {
145                return Err(io::Error::new(
146                    io::ErrorKind::UnexpectedEof,
147                    "failed to fill whole buffer",
148                ));
149            }
150            let len = buf.len();
151            at[..len].copy_from_slice(buf);
152            running_offset += len;
153        }
154        Ok(())
155    }
156
157    #[inline]
158    fn is_write_vectored_at(&self) -> bool {
159        true
160    }
161
162    #[inline]
163    fn copy_from<R: ReadAt>(
164        &mut self,
165        _offset: u64,
166        input: &R,
167        input_offset: u64,
168        len: u64,
169    ) -> io::Result<u64> {
170        let _todo = BorrowStreamer::new(input, input_offset).take(len);
171        todo!("slice::copy_from")
172    }
173
174    #[inline]
175    fn set_len(&mut self, _len: u64) -> io::Result<()> {
176        Err(io::Error::new(
177            io::ErrorKind::PermissionDenied,
178            "cannot set_len on a slice",
179        ))
180    }
181}
182
183impl WriteAt for &mut [u8] {
184    #[inline]
185    fn write_at(&mut self, buf: &[u8], offset: u64) -> io::Result<usize> {
186        self.write_all_at(buf, offset)?;
187        Ok(buf.len())
188    }
189
190    fn write_all_at(&mut self, buf: &[u8], offset: u64) -> io::Result<()> {
191        let offset = offset
192            .try_into()
193            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
194        let at = self.get_mut(offset..).unwrap_or(&mut []);
195        let len = min(at.len(), buf.len());
196        at[..len].copy_from_slice(&buf[..len]);
197        Ok(())
198    }
199
200    fn write_vectored_at(&mut self, bufs: &[IoSlice], offset: u64) -> io::Result<usize> {
201        let initial_offset = offset
202            .try_into()
203            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
204        let mut running_offset = initial_offset;
205        for buf in bufs {
206            let at = self.get_mut(running_offset..).unwrap_or(&mut []);
207            let len = min(at.len(), buf.len());
208            at[..len].copy_from_slice(buf);
209            running_offset += len;
210        }
211        Ok(running_offset - initial_offset)
212    }
213
214    fn write_all_vectored_at(&mut self, bufs: &mut [IoSlice], offset: u64) -> io::Result<()> {
215        let mut running_offset = offset
216            .try_into()
217            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
218        for buf in bufs {
219            let at = self.get_mut(running_offset..).unwrap_or(&mut []);
220            if at.len() < buf.len() {
221                return Err(io::Error::new(
222                    io::ErrorKind::UnexpectedEof,
223                    "failed to fill whole buffer",
224                ));
225            }
226            let len = buf.len();
227            at[..len].copy_from_slice(buf);
228            running_offset += len;
229        }
230        Ok(())
231    }
232
233    #[inline]
234    fn is_write_vectored_at(&self) -> bool {
235        true
236    }
237
238    #[inline]
239    fn copy_from<R: ReadAt>(
240        &mut self,
241        _offset: u64,
242        input: &R,
243        input_offset: u64,
244        len: u64,
245    ) -> io::Result<u64> {
246        let _todo = BorrowStreamer::new(input, input_offset).take(len);
247        todo!("slice::copy_from")
248    }
249
250    #[inline]
251    fn set_len(&mut self, _len: u64) -> io::Result<()> {
252        Err(io::Error::new(
253            io::ErrorKind::PermissionDenied,
254            "cannot set_len on a slice",
255        ))
256    }
257}
258
259impl Array for Vec<u8> {
260    #[inline]
261    fn metadata(&self) -> io::Result<Metadata> {
262        self.as_slice().metadata()
263    }
264
265    #[inline]
266    fn advise(&self, offset: u64, len: u64, advice: Advice) -> io::Result<()> {
267        self.as_slice().advise(offset, len, advice)
268    }
269}
270
271impl Array for &mut Vec<u8> {
272    #[inline]
273    fn metadata(&self) -> io::Result<Metadata> {
274        self.as_slice().metadata()
275    }
276
277    #[inline]
278    fn advise(&self, offset: u64, len: u64, advice: Advice) -> io::Result<()> {
279        self.as_slice().advise(offset, len, advice)
280    }
281}
282
283impl ReadAt for Vec<u8> {
284    #[inline]
285    fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
286        self.as_slice().read_at(buf, offset)
287    }
288
289    #[inline]
290    fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> {
291        self.as_slice().read_exact_at(buf, offset)
292    }
293
294    #[inline]
295    fn read_vectored_at(&self, bufs: &mut [IoSliceMut], offset: u64) -> io::Result<usize> {
296        self.as_slice().read_vectored_at(bufs, offset)
297    }
298
299    #[inline]
300    fn read_exact_vectored_at(&self, bufs: &mut [IoSliceMut], offset: u64) -> io::Result<()> {
301        self.as_slice().read_exact_vectored_at(bufs, offset)
302    }
303
304    #[inline]
305    fn is_read_vectored_at(&self) -> bool {
306        self.as_slice().is_read_vectored_at()
307    }
308
309    #[cfg(feature = "io-streams")]
310    fn read_via_stream_at(&self, _offset: u64) -> io::Result<StreamReader> {
311        todo!("slice::read_via_stream_at")
312    }
313}
314
315impl WriteAt for Vec<u8> {
316    #[inline]
317    fn write_at(&mut self, buf: &[u8], offset: u64) -> io::Result<usize> {
318        self.as_mut_slice().write_at(buf, offset)
319    }
320
321    #[inline]
322    fn write_all_at(&mut self, buf: &[u8], offset: u64) -> io::Result<()> {
323        self.as_mut_slice().write_all_at(buf, offset)
324    }
325
326    #[inline]
327    fn write_vectored_at(&mut self, bufs: &[IoSlice], offset: u64) -> io::Result<usize> {
328        self.as_mut_slice().write_vectored_at(bufs, offset)
329    }
330
331    #[inline]
332    fn write_all_vectored_at(&mut self, bufs: &mut [IoSlice], offset: u64) -> io::Result<()> {
333        self.as_mut_slice().write_all_vectored_at(bufs, offset)
334    }
335
336    #[inline]
337    fn is_write_vectored_at(&self) -> bool {
338        self.as_slice().is_write_vectored_at()
339    }
340
341    #[inline]
342    fn copy_from<R: ReadAt>(
343        &mut self,
344        offset: u64,
345        input: &R,
346        input_offset: u64,
347        len: u64,
348    ) -> io::Result<u64> {
349        self.as_mut_slice()
350            .copy_from(offset, input, input_offset, len)
351    }
352
353    #[inline]
354    fn set_len(&mut self, len: u64) -> io::Result<()> {
355        self.resize(
356            len.try_into()
357                .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
358            0,
359        );
360        Ok(())
361    }
362}
363
364impl WriteAt for &mut Vec<u8> {
365    #[inline]
366    fn write_at(&mut self, buf: &[u8], offset: u64) -> io::Result<usize> {
367        self.as_mut_slice().write_at(buf, offset)
368    }
369
370    #[inline]
371    fn write_all_at(&mut self, buf: &[u8], offset: u64) -> io::Result<()> {
372        self.as_mut_slice().write_all_at(buf, offset)
373    }
374
375    #[inline]
376    fn write_vectored_at(&mut self, bufs: &[IoSlice], offset: u64) -> io::Result<usize> {
377        self.as_mut_slice().write_vectored_at(bufs, offset)
378    }
379
380    #[inline]
381    fn write_all_vectored_at(&mut self, bufs: &mut [IoSlice], offset: u64) -> io::Result<()> {
382        self.as_mut_slice().write_all_vectored_at(bufs, offset)
383    }
384
385    #[inline]
386    fn is_write_vectored_at(&self) -> bool {
387        self.as_slice().is_write_vectored_at()
388    }
389
390    #[inline]
391    fn copy_from<R: ReadAt>(
392        &mut self,
393        offset: u64,
394        input: &R,
395        input_offset: u64,
396        len: u64,
397    ) -> io::Result<u64> {
398        self.as_mut_slice()
399            .copy_from(offset, input, input_offset, len)
400    }
401
402    #[inline]
403    fn set_len(&mut self, len: u64) -> io::Result<()> {
404        self.resize(
405            len.try_into()
406                .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
407            0,
408        );
409        Ok(())
410    }
411}