variable_len_reader/impls/
std.rs1use std::io::{Error, ErrorKind, Read, Write};
2use crate::synchronous::reader::VariableReader;
3use crate::synchronous::{VariableReadable, VariableWritable};
4use crate::synchronous::writer::VariableWriter;
5
6impl<R: Read> VariableReadable for R {
7 type Error = Error;
8
9 #[inline]
10 fn read_single(&mut self) -> Result<u8, Self::Error> {
11 let mut buf = [0];
12 R::read_exact(self, &mut buf)?;
13 Ok(buf[0])
14 }
15
16 #[inline]
17 fn read_more(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
18 R::read_exact(self, buf)
19 }
20}
21
22impl<R: Read> VariableReader for R {
23 #[inline]
24 fn read_bool_error(func_name: &'static str, byte: u8) -> Self::Error {
25 Error::new(ErrorKind::InvalidData, format!("Invalid bool. value {} at {}.", byte, func_name))
26 }
27
28 #[cfg(feature = "sync_bools")]
29 #[inline]
30 fn read_bools_error(func_name: &'static str, byte: u8) -> Self::Error {
31 Error::new(ErrorKind::InvalidData, format!("Invalid bools. value {} at {}.", byte, func_name))
32 }
33
34 #[cfg(feature = "sync_varint")]
35 #[inline]
36 fn read_varint_error(func_name: &'static str, value: u128) -> Self::Error {
37 Error::new(ErrorKind::InvalidData, format!("Too long varint value. {} at {}.", value, func_name))
38 }
39
40 #[cfg(feature = "sync_string")]
41 #[inline]
42 fn read_string_error(_func_name: &'static str, error: alloc::string::FromUtf8Error) -> Self::Error {
43 Error::new(ErrorKind::InvalidData, error)
44 }
45}
46
47impl<W: Write> VariableWritable for W {
48 type Error = Error;
49
50 #[inline]
51 fn write_single(&mut self, byte: u8) -> Result<(), Self::Error> {
52 W::write_all(self, &[byte])
53 }
54
55 #[inline]
56 fn write_more(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
57 W::write_all(self, buf)
58 }
59}
60
61impl<W: Write> VariableWriter for W { }