brotli_decompressor/
io_wrappers.rs

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
#[cfg(feature="std")]
use std::io::{self, ErrorKind, Read, Write};

/// this trait does not allow for transient errors: they must be retried in the underlying layer
pub trait CustomWrite<ErrType> {
  fn write(self: &mut Self, data: &[u8]) -> Result<usize, ErrType>;
  fn flush(self: &mut Self) -> Result<(), ErrType>;
}
/// this trait does not allow for transient errors: they must be retried in the underlying layer
pub trait CustomRead<ErrType> {
  fn read(self: &mut Self, data: &mut [u8]) -> Result<usize, ErrType>;
}

#[allow(dead_code)] // prefer to replace 2 inlines in BrotliDecompressCustomIo once traits work
pub fn write_all<ErrType, OutputType>(w: &mut OutputType, buf: &[u8]) -> Result<(), ErrType>
  where OutputType: CustomWrite<ErrType>
{
  let mut total_written: usize = 0;
  while total_written < buf.len() {
    match w.write(&buf[total_written..]) {
      Err(e) => return Result::Err(e),
      // CustomResult::Transient(e) => continue,
      Ok(cur_written) => {
        assert_eq!(cur_written == 0, false); // not allowed by the contract
        total_written += cur_written;
      }
    }
  }
  Ok(())
}

#[cfg(feature="std")]
pub struct IntoIoReader<InputType: Read>(pub InputType);

#[cfg(feature="std")]
pub struct IntoIoWriter<InputType: Write>(pub InputType);


#[cfg(feature="std")]
pub struct IoWriterWrapper<'a, OutputType: Write + 'a>(pub &'a mut OutputType);


#[cfg(feature="std")]
pub struct IoReaderWrapper<'a, OutputType: Read + 'a>(pub &'a mut OutputType);

#[cfg(feature="std")]
impl<'a, OutputType: Write> CustomWrite<io::Error> for IoWriterWrapper<'a, OutputType> {
  fn write(self: &mut Self, buf: &[u8]) -> Result<usize, io::Error> {
    loop {
      match self.0.write(buf) {
        Err(e) => {
          match e.kind() {
            ErrorKind::Interrupted => continue,
            _ => return Err(e),
          }
        }
        Ok(cur_written) => return Ok(cur_written),
      }
    }
  }
  fn flush(self: &mut Self) -> Result<(), io::Error> {
    loop {
      match self.0.flush() {
        Err(e) => {
          match e.kind() {
            ErrorKind::Interrupted => continue,
            _ => return Err(e),
          }
        }
        Ok(_) => return Ok(()),
      }
    }
  }
}


#[cfg(feature="std")]
impl<'a, InputType: Read> CustomRead<io::Error> for IoReaderWrapper<'a, InputType> {
  fn read(self: &mut Self, buf: &mut [u8]) -> Result<usize, io::Error> {
    loop {
      match self.0.read(buf) {
        Err(e) => {
          match e.kind() {
            ErrorKind::Interrupted => continue,
            _ => return Err(e),
          }
        }
        Ok(cur_read) => return Ok(cur_read),
      }
    }
  }
}

#[cfg(feature="std")]
impl<InputType: Read> CustomRead<io::Error> for IntoIoReader<InputType> {
  fn read(self: &mut Self, buf: &mut [u8]) -> Result<usize, io::Error> {
    loop {
      match self.0.read(buf) {
        Err(e) => {
          match e.kind() {
            ErrorKind::Interrupted => continue,
            _ => return Err(e),
          }
        }
        Ok(cur_read) => return Ok(cur_read),
      }
    }
  }
}

#[cfg(feature="std")]
impl<InputType: Write> CustomWrite<io::Error> for IntoIoWriter<InputType> {
  fn flush(self: &mut Self) -> Result<(), io::Error> {
    loop {
      match self.0.flush() {
        Err(e) => {
          match e.kind() {
            ErrorKind::Interrupted => continue,
            _ => return Err(e),
          }
        }
        Ok(_) => return Ok(()),
      }
    }     
  }
  fn write(self: &mut Self, buf: &[u8]) -> Result<usize, io::Error> {
    loop {
      match self.0.write(buf) {
        Err(e) => {
          match e.kind() {
            ErrorKind::Interrupted => continue,
            _ => return Err(e),
          }
        }
        Ok(cur_written) => return Ok(cur_written),
      }
    }
  }
}