non_empty_slice/
io.rs

1#[cfg(not(feature = "std"))]
2compile_error!("expected `std` to be enabled");
3
4use core::fmt;
5
6use std::io::{IoSlice, Result, Write};
7
8use crate::{slice::NonEmptyBytes, vec::NonEmptyByteVec};
9
10type Bytes = [u8];
11type ByteSlices<'a> = [IoSlice<'a>];
12
13impl Write for &mut NonEmptyBytes {
14    fn write(&mut self, buffer: &Bytes) -> Result<usize> {
15        self.as_mut_slice().write(buffer)
16    }
17
18    fn write_vectored(&mut self, buffers: &ByteSlices<'_>) -> Result<usize> {
19        self.as_mut_slice().write_vectored(buffers)
20    }
21
22    fn write_all(&mut self, buffer: &Bytes) -> Result<()> {
23        self.as_mut_slice().write_all(buffer)
24    }
25
26    fn write_fmt(&mut self, arguments: fmt::Arguments<'_>) -> Result<()> {
27        self.as_mut_slice().write_fmt(arguments)
28    }
29
30    fn flush(&mut self) -> Result<()> {
31        self.as_mut_slice().flush()
32    }
33}
34
35impl Write for NonEmptyByteVec {
36    fn write(&mut self, buffer: &Bytes) -> Result<usize> {
37        // SAFETY: writing can not make the vector empty
38        unsafe { self.as_mut_vec().write(buffer) }
39    }
40
41    fn write_vectored(&mut self, buffers: &ByteSlices<'_>) -> Result<usize> {
42        // SAFETY: writing can not make the vector empty
43        unsafe { self.as_mut_vec().write_vectored(buffers) }
44    }
45
46    fn write_all(&mut self, buffer: &Bytes) -> Result<()> {
47        // SAFETY: writing can not make the vector empty
48        unsafe { self.as_mut_vec().write_all(buffer) }
49    }
50
51    fn write_fmt(&mut self, arguments: fmt::Arguments<'_>) -> Result<()> {
52        // SAFETY: writing can not make the vector empty
53        unsafe { self.as_mut_vec().write_fmt(arguments) }
54    }
55
56    fn flush(&mut self) -> Result<()> {
57        // SAFETY: flushing can not make the vector empty
58        unsafe { self.as_mut_vec().flush() }
59    }
60}