A better Rust ATProto crate
102
fork

Configure Feed

Select the types of activity you want to include in your feed.

at pretty-codegen 305 lines 7.7 kB view raw
1use crate::io::{self, BufRead, Error, ErrorKind, Initializer, Read, Seek, SeekFrom, Write}; 2use core::{cmp, fmt, mem}; 3 4#[cfg(feature = "alloc")] 5use crate::alloc::{boxed::Box, string::String, vec::Vec}; 6 7// ============================================================================= 8// Forwarding implementations 9impl<R: Read + ?Sized> Read for &mut R { 10 #[inline] 11 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { 12 (**self).read(buf) 13 } 14 15 #[inline] 16 unsafe fn initializer(&self) -> Initializer { 17 unsafe { (**self).initializer() } 18 } 19 20 #[inline] 21 #[cfg(feature = "alloc")] 22 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { 23 (**self).read_to_end(buf) 24 } 25 26 #[inline] 27 #[cfg(feature = "alloc")] 28 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { 29 (**self).read_to_string(buf) 30 } 31 32 #[inline] 33 #[cfg(feature = "alloc")] 34 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { 35 (**self).read_exact(buf) 36 } 37} 38 39impl<W: Write + ?Sized> Write for &mut W { 40 #[inline] 41 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { 42 (**self).write(buf) 43 } 44 45 #[inline] 46 fn flush(&mut self) -> io::Result<()> { 47 (**self).flush() 48 } 49 50 #[inline] 51 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { 52 (**self).write_all(buf) 53 } 54 55 #[inline] 56 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { 57 (**self).write_fmt(fmt) 58 } 59} 60 61impl<S: Seek + ?Sized> Seek for &mut S { 62 #[inline] 63 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { 64 (**self).seek(pos) 65 } 66} 67 68impl<B: BufRead + ?Sized> BufRead for &mut B { 69 #[inline] 70 fn fill_buf(&mut self) -> io::Result<&[u8]> { 71 (**self).fill_buf() 72 } 73 74 #[inline] 75 fn consume(&mut self, amt: usize) { 76 (**self).consume(amt) 77 } 78 79 #[inline] 80 #[cfg(feature = "alloc")] 81 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> { 82 (**self).read_until(byte, buf) 83 } 84 85 #[inline] 86 #[cfg(feature = "alloc")] 87 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> { 88 (**self).read_line(buf) 89 } 90} 91 92#[cfg(feature = "alloc")] 93impl<R: Read + ?Sized> Read for Box<R> { 94 #[inline] 95 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { 96 (**self).read(buf) 97 } 98 99 #[inline] 100 unsafe fn initializer(&self) -> Initializer { 101 unsafe { (**self).initializer() } 102 } 103 104 #[inline] 105 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { 106 (**self).read_to_end(buf) 107 } 108 109 #[inline] 110 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { 111 (**self).read_to_string(buf) 112 } 113 114 #[inline] 115 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { 116 (**self).read_exact(buf) 117 } 118} 119 120#[cfg(feature = "alloc")] 121impl<W: Write + ?Sized> Write for Box<W> { 122 #[inline] 123 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { 124 (**self).write(buf) 125 } 126 127 #[inline] 128 fn flush(&mut self) -> io::Result<()> { 129 (**self).flush() 130 } 131 132 #[inline] 133 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { 134 (**self).write_all(buf) 135 } 136 137 #[inline] 138 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { 139 (**self).write_fmt(fmt) 140 } 141} 142 143#[cfg(feature = "alloc")] 144impl<S: Seek + ?Sized> Seek for Box<S> { 145 #[inline] 146 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { 147 (**self).seek(pos) 148 } 149} 150 151#[cfg(feature = "alloc")] 152impl<B: BufRead + ?Sized> BufRead for Box<B> { 153 #[inline] 154 fn fill_buf(&mut self) -> io::Result<&[u8]> { 155 (**self).fill_buf() 156 } 157 158 #[inline] 159 fn consume(&mut self, amt: usize) { 160 (**self).consume(amt) 161 } 162 163 #[inline] 164 #[cfg(feature = "alloc")] 165 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> { 166 (**self).read_until(byte, buf) 167 } 168 169 #[inline] 170 #[cfg(feature = "alloc")] 171 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> { 172 (**self).read_line(buf) 173 } 174} 175 176// ============================================================================= 177// In-memory buffer implementations 178 179/// Read is implemented for `&[u8]` by copying from the slice. 180/// 181/// Note that reading updates the slice to point to the yet unread part. 182/// The slice will be empty when EOF is reached. 183impl Read for &[u8] { 184 #[inline] 185 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { 186 let amt = cmp::min(buf.len(), self.len()); 187 let (a, b) = self.split_at(amt); 188 189 // First check if the amount of bytes we want to read is small: 190 // `copy_from_slice` will generally expand to a call to `memcpy`, and 191 // for a single byte the overhead is significant. 192 if amt == 1 { 193 buf[0] = a[0]; 194 } else { 195 buf[..amt].copy_from_slice(a); 196 } 197 198 *self = b; 199 Ok(amt) 200 } 201 202 #[inline] 203 unsafe fn initializer(&self) -> Initializer { 204 unsafe { Initializer::nop() } 205 } 206 207 #[inline] 208 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { 209 if buf.len() > self.len() { 210 return Err(Error::new( 211 ErrorKind::UnexpectedEof, 212 "failed to fill whole buffer".into(), 213 )); 214 } 215 let (a, b) = self.split_at(buf.len()); 216 217 // First check if the amount of bytes we want to read is small: 218 // `copy_from_slice` will generally expand to a call to `memcpy`, and 219 // for a single byte the overhead is significant. 220 if buf.len() == 1 { 221 buf[0] = a[0]; 222 } else { 223 buf.copy_from_slice(a); 224 } 225 226 *self = b; 227 Ok(()) 228 } 229 230 #[inline] 231 #[cfg(feature = "alloc")] 232 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { 233 buf.extend_from_slice(self); 234 let len = self.len(); 235 *self = &self[len..]; 236 Ok(len) 237 } 238} 239 240impl BufRead for &[u8] { 241 #[inline] 242 fn fill_buf(&mut self) -> io::Result<&[u8]> { 243 Ok(*self) 244 } 245 246 #[inline] 247 fn consume(&mut self, amt: usize) { 248 *self = &self[amt..]; 249 } 250} 251 252/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting 253/// its data. 254/// 255/// Note that writing updates the slice to point to the yet unwritten part. 256/// The slice will be empty when it has been completely overwritten. 257impl Write for &mut [u8] { 258 #[inline] 259 fn write(&mut self, data: &[u8]) -> io::Result<usize> { 260 let amt = cmp::min(data.len(), self.len()); 261 let (a, b) = mem::take(self).split_at_mut(amt); 262 a.copy_from_slice(&data[..amt]); 263 *self = b; 264 Ok(amt) 265 } 266 267 #[inline] 268 fn write_all(&mut self, data: &[u8]) -> io::Result<()> { 269 if self.write(data)? == data.len() { 270 Ok(()) 271 } else { 272 Err(Error::new( 273 ErrorKind::WriteZero, 274 "failed to write whole buffer".into(), 275 )) 276 } 277 } 278 279 #[inline] 280 fn flush(&mut self) -> io::Result<()> { 281 Ok(()) 282 } 283} 284 285/// Write is implemented for `Vec<u8>` by appending to the vector. 286/// The vector will grow as needed. 287#[cfg(feature = "alloc")] 288impl Write for Vec<u8> { 289 #[inline] 290 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { 291 self.extend_from_slice(buf); 292 Ok(buf.len()) 293 } 294 295 #[inline] 296 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { 297 self.extend_from_slice(buf); 298 Ok(()) 299 } 300 301 #[inline] 302 fn flush(&mut self) -> io::Result<()> { 303 Ok(()) 304 } 305}