| 1083 | |
| 1084 | impl Write for BoundedBytes { |
| 1085 | fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> { |
| 1086 | if bytes.len() > self.max_bytes.saturating_sub(self.bytes.len()) { |
| 1087 | return Err(self.reject()); |
| 1088 | } |
| 1089 | let required = self.bytes.len() + bytes.len(); |
| 1090 | if required > self.bytes.capacity() { |
| 1091 | let capacity = self |
| 1092 | .bytes |
| 1093 | .capacity() |
| 1094 | .saturating_mul(2) |
| 1095 | .max(required) |
| 1096 | .min(self.max_bytes); |
| 1097 | self.bytes.reserve_exact(capacity - self.bytes.len()); |
| 1098 | } |
| 1099 | self.bytes.extend_from_slice(bytes); |
| 1100 | Ok(bytes.len()) |
| 1101 | } |
| 1102 | |
| 1103 | fn flush(&mut self) -> std::io::Result<()> { |
| 1104 | Ok(()) |