| 1 |
#[cfg(feature = "std")] |
| 2 |
use crate::buf::{reader, Reader}; |
| 3 |
use crate::buf::{take, Chain, Take}; |
| 4 |
#[cfg(feature = "std")] |
| 5 |
use crate::{min_u64_usize, saturating_sub_usize_u64}; |
| 6 |
use crate::{panic_advance, panic_does_not_fit, TryGetError}; |
| 7 |
|
| 8 |
#[cfg(feature = "std")] |
| 9 |
use std::io::IoSlice; |
| 10 |
|
| 11 |
use alloc::boxed::Box; |
| 12 |
|
| 13 |
macro_rules! buf_try_get_impl { |
| 14 |
($this:ident, $typ:tt::$conv:tt) => {{ |
| 15 |
const SIZE: usize = core::mem::size_of::<$typ>(); |
| 16 |
|
| 17 |
if $this.remaining() < SIZE { |
| 18 |
return Err(TryGetError { |
| 19 |
requested: SIZE, |
| 20 |
available: $this.remaining(), |
| 21 |
}); |
| 22 |
} |
| 23 |
|
| 24 |
// try to convert directly from the bytes |
| 25 |
// this Option<ret> trick is to avoid keeping a borrow on self |
| 26 |
// when advance() is called (mut borrow) and to call bytes() only once |
| 27 |
let ret = $this |
| 28 |
.chunk() |
| 29 |
.get(..SIZE) |
| 30 |
.map(|src| unsafe { $typ::$conv(*(src as *const _ as *const [_; SIZE])) }); |
| 31 |
|
| 32 |
if let Some(ret) = ret { |
| 33 |
// if the direct conversion was possible, advance and return |
| 34 |
$this.advance(SIZE); |
| 35 |
return Ok(ret); |
| 36 |
} else { |
| 37 |
// if not we copy the bytes in a temp buffer then convert |
| 38 |
let mut buf = [0; SIZE]; |
| 39 |
$this.copy_to_slice(&mut buf); // (do the advance) |
| 40 |
return Ok($typ::$conv(buf)); |
| 41 |
} |
| 42 |
}}; |
| 43 |
(le => $this:ident, $typ:tt, $len_to_read:expr) => {{ |
| 44 |
const SIZE: usize = core::mem::size_of::<$typ>(); |
| 45 |
|
| 46 |
// The same trick as above does not improve the best case speed. |
| 47 |
// It seems to be linked to the way the method is optimised by the compiler |
| 48 |
let mut buf = [0; SIZE]; |
| 49 |
|
| 50 |
let subslice = match buf.get_mut(..$len_to_read) { |
| 51 |
Some(subslice) => subslice, |
| 52 |
None => panic_does_not_fit(SIZE, $len_to_read), |
| 53 |
}; |
| 54 |
|
| 55 |
$this.try_copy_to_slice(subslice)?; |
| 56 |
return Ok($typ::from_le_bytes(buf)); |
| 57 |
}}; |
| 58 |
(be => $this:ident, $typ:tt, $len_to_read:expr) => {{ |
| 59 |
const SIZE: usize = core::mem::size_of::<$typ>(); |
| 60 |
|
| 61 |
let slice_at = match SIZE.checked_sub($len_to_read) { |
| 62 |
Some(slice_at) => slice_at, |
| 63 |
None => panic_does_not_fit(SIZE, $len_to_read), |
| 64 |
}; |
| 65 |
|
| 66 |
let mut buf = [0; SIZE]; |
| 67 |
$this.try_copy_to_slice(&mut buf[slice_at..])?; |
| 68 |
return Ok($typ::from_be_bytes(buf)); |
| 69 |
}}; |
| 70 |
} |
| 71 |
|
| 72 |
macro_rules! buf_get_impl { |
| 73 |
($this:ident, $typ:tt::$conv:tt) => {{ |
| 74 |
return (|| buf_try_get_impl!($this, $typ::$conv))() |
| 75 |
.unwrap_or_else(|error| panic_advance(&error)); |
| 76 |
}}; |
| 77 |
(le => $this:ident, $typ:tt, $len_to_read:expr) => {{ |
| 78 |
return (|| buf_try_get_impl!(le => $this, $typ, $len_to_read))() |
| 79 |
.unwrap_or_else(|error| panic_advance(&error)); |
| 80 |
}}; |
| 81 |
(be => $this:ident, $typ:tt, $len_to_read:expr) => {{ |
| 82 |
return (|| buf_try_get_impl!(be => $this, $typ, $len_to_read))() |
| 83 |
.unwrap_or_else(|error| panic_advance(&error)); |
| 84 |
}}; |
| 85 |
} |
| 86 |
|
| 87 |
// https://en.wikipedia.org/wiki/Sign_extension |
| 88 |
fn sign_extend(val: u64, nbytes: usize) -> i64 { |
| 89 |
let shift = (8 - nbytes) * 8; |
| 90 |
(val << shift) as i64 >> shift |
| 91 |
} |
| 92 |
|
| 93 |
/// Read bytes from a buffer. |
| 94 |
/// |
| 95 |
/// A buffer stores bytes in memory such that read operations are infallible. |
| 96 |
/// The underlying storage may or may not be in contiguous memory. A `Buf` value |
| 97 |
/// is a cursor into the buffer. Reading from `Buf` advances the cursor |
| 98 |
/// position. It can be thought of as an efficient `Iterator` for collections of |
| 99 |
/// bytes. |
| 100 |
/// |
| 101 |
/// The simplest `Buf` is a `&[u8]`. |
| 102 |
/// |
| 103 |
/// ``` |
| 104 |
/// use bytes::Buf; |
| 105 |
/// |
| 106 |
/// let mut buf = &b"hello world"[..]; |
| 107 |
/// |
| 108 |
/// assert_eq!(b'h', buf.get_u8()); |
| 109 |
/// assert_eq!(b'e', buf.get_u8()); |
| 110 |
/// assert_eq!(b'l', buf.get_u8()); |
| 111 |
/// |
| 112 |
/// let mut rest = [0; 8]; |
| 113 |
/// buf.copy_to_slice(&mut rest); |
| 114 |
/// |
| 115 |
/// assert_eq!(&rest[..], &b"lo world"[..]); |
| 116 |
/// ``` |
| 117 |
pub trait Buf { |
| 118 |
/// Returns the number of bytes between the current position and the end of |
| 119 |
/// the buffer. |
| 120 |
/// |
| 121 |
/// This value is greater than or equal to the length of the slice returned |
| 122 |
/// by `chunk()`. |
| 123 |
/// |
| 124 |
/// # Examples |
| 125 |
/// |
| 126 |
/// ``` |
| 127 |
/// use bytes::Buf; |
| 128 |
/// |
| 129 |
/// let mut buf = &b"hello world"[..]; |
| 130 |
/// |
| 131 |
/// assert_eq!(buf.remaining(), 11); |
| 132 |
/// |
| 133 |
/// buf.get_u8(); |
| 134 |
/// |
| 135 |
/// assert_eq!(buf.remaining(), 10); |
| 136 |
/// ``` |
| 137 |
/// |
| 138 |
/// # Implementer notes |
| 139 |
/// |
| 140 |
/// Implementations of `remaining` should ensure that the return value does |
| 141 |
/// not change unless a call is made to `advance` or any other function that |
| 142 |
/// is documented to change the `Buf`'s current position. |
| 143 |
fn remaining(&self) -> usize; |
| 144 |
|
| 145 |
/// Returns a slice starting at the current position and of length between 0 |
| 146 |
/// and `Buf::remaining()`. Note that this *can* return a shorter slice (this |
| 147 |
/// allows non-continuous internal representation). |
| 148 |
/// |
| 149 |
/// This is a lower level function. Most operations are done with other |
| 150 |
/// functions. |
| 151 |
/// |
| 152 |
/// # Examples |
| 153 |
/// |
| 154 |
/// ``` |
| 155 |
/// use bytes::Buf; |
| 156 |
/// |
| 157 |
/// let mut buf = &b"hello world"[..]; |
| 158 |
/// |
| 159 |
/// assert_eq!(buf.chunk(), &b"hello world"[..]); |
| 160 |
/// |
| 161 |
/// buf.advance(6); |
| 162 |
/// |
| 163 |
/// assert_eq!(buf.chunk(), &b"world"[..]); |
| 164 |
/// ``` |
| 165 |
/// |
| 166 |
/// # Implementer notes |
| 167 |
/// |
| 168 |
/// This function should never panic. `chunk()` should return an empty |
| 169 |
/// slice **if and only if** `remaining()` returns 0. In other words, |
| 170 |
/// `chunk()` returning an empty slice implies that `remaining()` will |
| 171 |
/// return 0 and `remaining()` returning 0 implies that `chunk()` will |
| 172 |
/// return an empty slice. |
| 173 |
// The `chunk` method was previously called `bytes`. This alias makes the rename |
| 174 |
// more easily discoverable. |
| 175 |
#[cfg_attr(docsrs, doc(alias = "bytes"))] |
| 176 |
fn chunk(&self) -> &[u8]; |
| 177 |
|
| 178 |
/// Fills `dst` with potentially multiple slices starting at `self`'s |
| 179 |
/// current position. |
| 180 |
/// |
| 181 |
/// If the `Buf` is backed by disjoint slices of bytes, `chunk_vectored` enables |
| 182 |
/// fetching more than one slice at once. `dst` is a slice of `IoSlice` |
| 183 |
/// references, enabling the slice to be directly used with [`writev`] |
| 184 |
/// without any further conversion. The sum of the lengths of all the |
| 185 |
/// buffers written to `dst` will be less than or equal to `Buf::remaining()`. |
| 186 |
/// |
| 187 |
/// The entries in `dst` will be overwritten, but the data **contained** by |
| 188 |
/// the slices **will not** be modified. The return value is the number of |
| 189 |
/// slices written to `dst`. If `Buf::remaining()` is non-zero, then this |
| 190 |
/// writes at least one non-empty slice to `dst`. |
| 191 |
/// |
| 192 |
/// This is a lower level function. Most operations are done with other |
| 193 |
/// functions. |
| 194 |
/// |
| 195 |
/// # Implementer notes |
| 196 |
/// |
| 197 |
/// This function should never panic. Once the end of the buffer is reached, |
| 198 |
/// i.e., `Buf::remaining` returns 0, calls to `chunk_vectored` must return 0 |
| 199 |
/// without mutating `dst`. |
| 200 |
/// |
| 201 |
/// Implementations should also take care to properly handle being called |
| 202 |
/// with `dst` being a zero length slice. |
| 203 |
/// |
| 204 |
/// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html |
| 205 |
#[cfg(feature = "std")] |
| 206 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 207 |
fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize { |
| 208 |
if dst.is_empty() { |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
if self.has_remaining() { |
| 213 |
dst[0] = IoSlice::new(self.chunk()); |
| 214 |
1 |
| 215 |
} else { |
| 216 |
0 |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/// Advance the internal cursor of the Buf |
| 221 |
/// |
| 222 |
/// The next call to `chunk()` will return a slice starting `cnt` bytes |
| 223 |
/// further into the underlying buffer. |
| 224 |
/// |
| 225 |
/// # Examples |
| 226 |
/// |
| 227 |
/// ``` |
| 228 |
/// use bytes::Buf; |
| 229 |
/// |
| 230 |
/// let mut buf = &b"hello world"[..]; |
| 231 |
/// |
| 232 |
/// assert_eq!(buf.chunk(), &b"hello world"[..]); |
| 233 |
/// |
| 234 |
/// buf.advance(6); |
| 235 |
/// |
| 236 |
/// assert_eq!(buf.chunk(), &b"world"[..]); |
| 237 |
/// ``` |
| 238 |
/// |
| 239 |
/// # Panics |
| 240 |
/// |
| 241 |
/// This function **may** panic if `cnt > self.remaining()`. |
| 242 |
/// |
| 243 |
/// # Implementer notes |
| 244 |
/// |
| 245 |
/// It is recommended for implementations of `advance` to panic if `cnt > |
| 246 |
/// self.remaining()`. If the implementation does not panic, the call must |
| 247 |
/// behave as if `cnt == self.remaining()`. |
| 248 |
/// |
| 249 |
/// A call with `cnt == 0` should never panic and be a no-op. |
| 250 |
fn advance(&mut self, cnt: usize); |
| 251 |
|
| 252 |
/// Returns true if there are any more bytes to consume |
| 253 |
/// |
| 254 |
/// This is equivalent to `self.remaining() != 0`. |
| 255 |
/// |
| 256 |
/// # Examples |
| 257 |
/// |
| 258 |
/// ``` |
| 259 |
/// use bytes::Buf; |
| 260 |
/// |
| 261 |
/// let mut buf = &b"a"[..]; |
| 262 |
/// |
| 263 |
/// assert!(buf.has_remaining()); |
| 264 |
/// |
| 265 |
/// buf.get_u8(); |
| 266 |
/// |
| 267 |
/// assert!(!buf.has_remaining()); |
| 268 |
/// ``` |
| 269 |
fn has_remaining(&self) -> bool { |
| 270 |
self.remaining() > 0 |
| 271 |
} |
| 272 |
|
| 273 |
/// Copies bytes from `self` into `dst`. |
| 274 |
/// |
| 275 |
/// The cursor is advanced by the number of bytes copied. `self` must have |
| 276 |
/// enough remaining bytes to fill `dst`. |
| 277 |
/// |
| 278 |
/// # Examples |
| 279 |
/// |
| 280 |
/// ``` |
| 281 |
/// use bytes::Buf; |
| 282 |
/// |
| 283 |
/// let mut buf = &b"hello world"[..]; |
| 284 |
/// let mut dst = [0; 5]; |
| 285 |
/// |
| 286 |
/// buf.copy_to_slice(&mut dst); |
| 287 |
/// assert_eq!(&b"hello"[..], &dst); |
| 288 |
/// assert_eq!(6, buf.remaining()); |
| 289 |
/// ``` |
| 290 |
/// |
| 291 |
/// # Panics |
| 292 |
/// |
| 293 |
/// This function panics if `self.remaining() < dst.len()`. |
| 294 |
fn copy_to_slice(&mut self, dst: &mut [u8]) { |
| 295 |
self.try_copy_to_slice(dst) |
| 296 |
.unwrap_or_else(|error| panic_advance(&error)); |
| 297 |
} |
| 298 |
|
| 299 |
/// Gets an unsigned 8 bit integer from `self`. |
| 300 |
/// |
| 301 |
/// The current position is advanced by 1. |
| 302 |
/// |
| 303 |
/// # Examples |
| 304 |
/// |
| 305 |
/// ``` |
| 306 |
/// use bytes::Buf; |
| 307 |
/// |
| 308 |
/// let mut buf = &b"\x08 hello"[..]; |
| 309 |
/// assert_eq!(8, buf.get_u8()); |
| 310 |
/// ``` |
| 311 |
/// |
| 312 |
/// # Panics |
| 313 |
/// |
| 314 |
/// This function panics if there is no more remaining data in `self`. |
| 315 |
fn get_u8(&mut self) -> u8 { |
| 316 |
if self.remaining() < 1 { |
| 317 |
panic_advance(&TryGetError { |
| 318 |
requested: 1, |
| 319 |
available: 0, |
| 320 |
}) |
| 321 |
} |
| 322 |
let ret = self.chunk()[0]; |
| 323 |
self.advance(1); |
| 324 |
ret |
| 325 |
} |
| 326 |
|
| 327 |
/// Gets a signed 8 bit integer from `self`. |
| 328 |
/// |
| 329 |
/// The current position is advanced by 1. |
| 330 |
/// |
| 331 |
/// # Examples |
| 332 |
/// |
| 333 |
/// ``` |
| 334 |
/// use bytes::Buf; |
| 335 |
/// |
| 336 |
/// let mut buf = &b"\x08 hello"[..]; |
| 337 |
/// assert_eq!(8, buf.get_i8()); |
| 338 |
/// ``` |
| 339 |
/// |
| 340 |
/// # Panics |
| 341 |
/// |
| 342 |
/// This function panics if there is no more remaining data in `self`. |
| 343 |
fn get_i8(&mut self) -> i8 { |
| 344 |
if self.remaining() < 1 { |
| 345 |
panic_advance(&TryGetError { |
| 346 |
requested: 1, |
| 347 |
available: 0, |
| 348 |
}); |
| 349 |
} |
| 350 |
let ret = self.chunk()[0] as i8; |
| 351 |
self.advance(1); |
| 352 |
ret |
| 353 |
} |
| 354 |
|
| 355 |
/// Gets an unsigned 16 bit integer from `self` in big-endian byte order. |
| 356 |
/// |
| 357 |
/// The current position is advanced by 2. |
| 358 |
/// |
| 359 |
/// # Examples |
| 360 |
/// |
| 361 |
/// ``` |
| 362 |
/// use bytes::Buf; |
| 363 |
/// |
| 364 |
/// let mut buf = &b"\x08\x09 hello"[..]; |
| 365 |
/// assert_eq!(0x0809, buf.get_u16()); |
| 366 |
/// ``` |
| 367 |
/// |
| 368 |
/// # Panics |
| 369 |
/// |
| 370 |
/// This function panics if there is not enough remaining data in `self`. |
| 371 |
fn get_u16(&mut self) -> u16 { |
| 372 |
buf_get_impl!(self, u16::from_be_bytes); |
| 373 |
} |
| 374 |
|
| 375 |
/// Gets an unsigned 16 bit integer from `self` in little-endian byte order. |
| 376 |
/// |
| 377 |
/// The current position is advanced by 2. |
| 378 |
/// |
| 379 |
/// # Examples |
| 380 |
/// |
| 381 |
/// ``` |
| 382 |
/// use bytes::Buf; |
| 383 |
/// |
| 384 |
/// let mut buf = &b"\x09\x08 hello"[..]; |
| 385 |
/// assert_eq!(0x0809, buf.get_u16_le()); |
| 386 |
/// ``` |
| 387 |
/// |
| 388 |
/// # Panics |
| 389 |
/// |
| 390 |
/// This function panics if there is not enough remaining data in `self`. |
| 391 |
fn get_u16_le(&mut self) -> u16 { |
| 392 |
buf_get_impl!(self, u16::from_le_bytes); |
| 393 |
} |
| 394 |
|
| 395 |
/// Gets an unsigned 16 bit integer from `self` in native-endian byte order. |
| 396 |
/// |
| 397 |
/// The current position is advanced by 2. |
| 398 |
/// |
| 399 |
/// # Examples |
| 400 |
/// |
| 401 |
/// ``` |
| 402 |
/// use bytes::Buf; |
| 403 |
/// |
| 404 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 405 |
/// true => b"\x08\x09 hello", |
| 406 |
/// false => b"\x09\x08 hello", |
| 407 |
/// }; |
| 408 |
/// assert_eq!(0x0809, buf.get_u16_ne()); |
| 409 |
/// ``` |
| 410 |
/// |
| 411 |
/// # Panics |
| 412 |
/// |
| 413 |
/// This function panics if there is not enough remaining data in `self`. |
| 414 |
fn get_u16_ne(&mut self) -> u16 { |
| 415 |
buf_get_impl!(self, u16::from_ne_bytes); |
| 416 |
} |
| 417 |
|
| 418 |
/// Gets a signed 16 bit integer from `self` in big-endian byte order. |
| 419 |
/// |
| 420 |
/// The current position is advanced by 2. |
| 421 |
/// |
| 422 |
/// # Examples |
| 423 |
/// |
| 424 |
/// ``` |
| 425 |
/// use bytes::Buf; |
| 426 |
/// |
| 427 |
/// let mut buf = &b"\x08\x09 hello"[..]; |
| 428 |
/// assert_eq!(0x0809, buf.get_i16()); |
| 429 |
/// ``` |
| 430 |
/// |
| 431 |
/// # Panics |
| 432 |
/// |
| 433 |
/// This function panics if there is not enough remaining data in `self`. |
| 434 |
fn get_i16(&mut self) -> i16 { |
| 435 |
buf_get_impl!(self, i16::from_be_bytes); |
| 436 |
} |
| 437 |
|
| 438 |
/// Gets a signed 16 bit integer from `self` in little-endian byte order. |
| 439 |
/// |
| 440 |
/// The current position is advanced by 2. |
| 441 |
/// |
| 442 |
/// # Examples |
| 443 |
/// |
| 444 |
/// ``` |
| 445 |
/// use bytes::Buf; |
| 446 |
/// |
| 447 |
/// let mut buf = &b"\x09\x08 hello"[..]; |
| 448 |
/// assert_eq!(0x0809, buf.get_i16_le()); |
| 449 |
/// ``` |
| 450 |
/// |
| 451 |
/// # Panics |
| 452 |
/// |
| 453 |
/// This function panics if there is not enough remaining data in `self`. |
| 454 |
fn get_i16_le(&mut self) -> i16 { |
| 455 |
buf_get_impl!(self, i16::from_le_bytes); |
| 456 |
} |
| 457 |
|
| 458 |
/// Gets a signed 16 bit integer from `self` in native-endian byte order. |
| 459 |
/// |
| 460 |
/// The current position is advanced by 2. |
| 461 |
/// |
| 462 |
/// # Examples |
| 463 |
/// |
| 464 |
/// ``` |
| 465 |
/// use bytes::Buf; |
| 466 |
/// |
| 467 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 468 |
/// true => b"\x08\x09 hello", |
| 469 |
/// false => b"\x09\x08 hello", |
| 470 |
/// }; |
| 471 |
/// assert_eq!(0x0809, buf.get_i16_ne()); |
| 472 |
/// ``` |
| 473 |
/// |
| 474 |
/// # Panics |
| 475 |
/// |
| 476 |
/// This function panics if there is not enough remaining data in `self`. |
| 477 |
fn get_i16_ne(&mut self) -> i16 { |
| 478 |
buf_get_impl!(self, i16::from_ne_bytes); |
| 479 |
} |
| 480 |
|
| 481 |
/// Gets an unsigned 32 bit integer from `self` in the big-endian byte order. |
| 482 |
/// |
| 483 |
/// The current position is advanced by 4. |
| 484 |
/// |
| 485 |
/// # Examples |
| 486 |
/// |
| 487 |
/// ``` |
| 488 |
/// use bytes::Buf; |
| 489 |
/// |
| 490 |
/// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; |
| 491 |
/// assert_eq!(0x0809A0A1, buf.get_u32()); |
| 492 |
/// ``` |
| 493 |
/// |
| 494 |
/// # Panics |
| 495 |
/// |
| 496 |
/// This function panics if there is not enough remaining data in `self`. |
| 497 |
fn get_u32(&mut self) -> u32 { |
| 498 |
buf_get_impl!(self, u32::from_be_bytes); |
| 499 |
} |
| 500 |
|
| 501 |
/// Gets an unsigned 32 bit integer from `self` in the little-endian byte order. |
| 502 |
/// |
| 503 |
/// The current position is advanced by 4. |
| 504 |
/// |
| 505 |
/// # Examples |
| 506 |
/// |
| 507 |
/// ``` |
| 508 |
/// use bytes::Buf; |
| 509 |
/// |
| 510 |
/// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; |
| 511 |
/// assert_eq!(0x0809A0A1, buf.get_u32_le()); |
| 512 |
/// ``` |
| 513 |
/// |
| 514 |
/// # Panics |
| 515 |
/// |
| 516 |
/// This function panics if there is not enough remaining data in `self`. |
| 517 |
fn get_u32_le(&mut self) -> u32 { |
| 518 |
buf_get_impl!(self, u32::from_le_bytes); |
| 519 |
} |
| 520 |
|
| 521 |
/// Gets an unsigned 32 bit integer from `self` in native-endian byte order. |
| 522 |
/// |
| 523 |
/// The current position is advanced by 4. |
| 524 |
/// |
| 525 |
/// # Examples |
| 526 |
/// |
| 527 |
/// ``` |
| 528 |
/// use bytes::Buf; |
| 529 |
/// |
| 530 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 531 |
/// true => b"\x08\x09\xA0\xA1 hello", |
| 532 |
/// false => b"\xA1\xA0\x09\x08 hello", |
| 533 |
/// }; |
| 534 |
/// assert_eq!(0x0809A0A1, buf.get_u32_ne()); |
| 535 |
/// ``` |
| 536 |
/// |
| 537 |
/// # Panics |
| 538 |
/// |
| 539 |
/// This function panics if there is not enough remaining data in `self`. |
| 540 |
fn get_u32_ne(&mut self) -> u32 { |
| 541 |
buf_get_impl!(self, u32::from_ne_bytes); |
| 542 |
} |
| 543 |
|
| 544 |
/// Gets a signed 32 bit integer from `self` in big-endian byte order. |
| 545 |
/// |
| 546 |
/// The current position is advanced by 4. |
| 547 |
/// |
| 548 |
/// # Examples |
| 549 |
/// |
| 550 |
/// ``` |
| 551 |
/// use bytes::Buf; |
| 552 |
/// |
| 553 |
/// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; |
| 554 |
/// assert_eq!(0x0809A0A1, buf.get_i32()); |
| 555 |
/// ``` |
| 556 |
/// |
| 557 |
/// # Panics |
| 558 |
/// |
| 559 |
/// This function panics if there is not enough remaining data in `self`. |
| 560 |
fn get_i32(&mut self) -> i32 { |
| 561 |
buf_get_impl!(self, i32::from_be_bytes); |
| 562 |
} |
| 563 |
|
| 564 |
/// Gets a signed 32 bit integer from `self` in little-endian byte order. |
| 565 |
/// |
| 566 |
/// The current position is advanced by 4. |
| 567 |
/// |
| 568 |
/// # Examples |
| 569 |
/// |
| 570 |
/// ``` |
| 571 |
/// use bytes::Buf; |
| 572 |
/// |
| 573 |
/// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; |
| 574 |
/// assert_eq!(0x0809A0A1, buf.get_i32_le()); |
| 575 |
/// ``` |
| 576 |
/// |
| 577 |
/// # Panics |
| 578 |
/// |
| 579 |
/// This function panics if there is not enough remaining data in `self`. |
| 580 |
fn get_i32_le(&mut self) -> i32 { |
| 581 |
buf_get_impl!(self, i32::from_le_bytes); |
| 582 |
} |
| 583 |
|
| 584 |
/// Gets a signed 32 bit integer from `self` in native-endian byte order. |
| 585 |
/// |
| 586 |
/// The current position is advanced by 4. |
| 587 |
/// |
| 588 |
/// # Examples |
| 589 |
/// |
| 590 |
/// ``` |
| 591 |
/// use bytes::Buf; |
| 592 |
/// |
| 593 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 594 |
/// true => b"\x08\x09\xA0\xA1 hello", |
| 595 |
/// false => b"\xA1\xA0\x09\x08 hello", |
| 596 |
/// }; |
| 597 |
/// assert_eq!(0x0809A0A1, buf.get_i32_ne()); |
| 598 |
/// ``` |
| 599 |
/// |
| 600 |
/// # Panics |
| 601 |
/// |
| 602 |
/// This function panics if there is not enough remaining data in `self`. |
| 603 |
fn get_i32_ne(&mut self) -> i32 { |
| 604 |
buf_get_impl!(self, i32::from_ne_bytes); |
| 605 |
} |
| 606 |
|
| 607 |
/// Gets an unsigned 64 bit integer from `self` in big-endian byte order. |
| 608 |
/// |
| 609 |
/// The current position is advanced by 8. |
| 610 |
/// |
| 611 |
/// # Examples |
| 612 |
/// |
| 613 |
/// ``` |
| 614 |
/// use bytes::Buf; |
| 615 |
/// |
| 616 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; |
| 617 |
/// assert_eq!(0x0102030405060708, buf.get_u64()); |
| 618 |
/// ``` |
| 619 |
/// |
| 620 |
/// # Panics |
| 621 |
/// |
| 622 |
/// This function panics if there is not enough remaining data in `self`. |
| 623 |
fn get_u64(&mut self) -> u64 { |
| 624 |
buf_get_impl!(self, u64::from_be_bytes); |
| 625 |
} |
| 626 |
|
| 627 |
/// Gets an unsigned 64 bit integer from `self` in little-endian byte order. |
| 628 |
/// |
| 629 |
/// The current position is advanced by 8. |
| 630 |
/// |
| 631 |
/// # Examples |
| 632 |
/// |
| 633 |
/// ``` |
| 634 |
/// use bytes::Buf; |
| 635 |
/// |
| 636 |
/// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; |
| 637 |
/// assert_eq!(0x0102030405060708, buf.get_u64_le()); |
| 638 |
/// ``` |
| 639 |
/// |
| 640 |
/// # Panics |
| 641 |
/// |
| 642 |
/// This function panics if there is not enough remaining data in `self`. |
| 643 |
fn get_u64_le(&mut self) -> u64 { |
| 644 |
buf_get_impl!(self, u64::from_le_bytes); |
| 645 |
} |
| 646 |
|
| 647 |
/// Gets an unsigned 64 bit integer from `self` in native-endian byte order. |
| 648 |
/// |
| 649 |
/// The current position is advanced by 8. |
| 650 |
/// |
| 651 |
/// # Examples |
| 652 |
/// |
| 653 |
/// ``` |
| 654 |
/// use bytes::Buf; |
| 655 |
/// |
| 656 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 657 |
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello", |
| 658 |
/// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello", |
| 659 |
/// }; |
| 660 |
/// assert_eq!(0x0102030405060708, buf.get_u64_ne()); |
| 661 |
/// ``` |
| 662 |
/// |
| 663 |
/// # Panics |
| 664 |
/// |
| 665 |
/// This function panics if there is not enough remaining data in `self`. |
| 666 |
fn get_u64_ne(&mut self) -> u64 { |
| 667 |
buf_get_impl!(self, u64::from_ne_bytes); |
| 668 |
} |
| 669 |
|
| 670 |
/// Gets a signed 64 bit integer from `self` in big-endian byte order. |
| 671 |
/// |
| 672 |
/// The current position is advanced by 8. |
| 673 |
/// |
| 674 |
/// # Examples |
| 675 |
/// |
| 676 |
/// ``` |
| 677 |
/// use bytes::Buf; |
| 678 |
/// |
| 679 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; |
| 680 |
/// assert_eq!(0x0102030405060708, buf.get_i64()); |
| 681 |
/// ``` |
| 682 |
/// |
| 683 |
/// # Panics |
| 684 |
/// |
| 685 |
/// This function panics if there is not enough remaining data in `self`. |
| 686 |
fn get_i64(&mut self) -> i64 { |
| 687 |
buf_get_impl!(self, i64::from_be_bytes); |
| 688 |
} |
| 689 |
|
| 690 |
/// Gets a signed 64 bit integer from `self` in little-endian byte order. |
| 691 |
/// |
| 692 |
/// The current position is advanced by 8. |
| 693 |
/// |
| 694 |
/// # Examples |
| 695 |
/// |
| 696 |
/// ``` |
| 697 |
/// use bytes::Buf; |
| 698 |
/// |
| 699 |
/// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; |
| 700 |
/// assert_eq!(0x0102030405060708, buf.get_i64_le()); |
| 701 |
/// ``` |
| 702 |
/// |
| 703 |
/// # Panics |
| 704 |
/// |
| 705 |
/// This function panics if there is not enough remaining data in `self`. |
| 706 |
fn get_i64_le(&mut self) -> i64 { |
| 707 |
buf_get_impl!(self, i64::from_le_bytes); |
| 708 |
} |
| 709 |
|
| 710 |
/// Gets a signed 64 bit integer from `self` in native-endian byte order. |
| 711 |
/// |
| 712 |
/// The current position is advanced by 8. |
| 713 |
/// |
| 714 |
/// # Examples |
| 715 |
/// |
| 716 |
/// ``` |
| 717 |
/// use bytes::Buf; |
| 718 |
/// |
| 719 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 720 |
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello", |
| 721 |
/// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello", |
| 722 |
/// }; |
| 723 |
/// assert_eq!(0x0102030405060708, buf.get_i64_ne()); |
| 724 |
/// ``` |
| 725 |
/// |
| 726 |
/// # Panics |
| 727 |
/// |
| 728 |
/// This function panics if there is not enough remaining data in `self`. |
| 729 |
fn get_i64_ne(&mut self) -> i64 { |
| 730 |
buf_get_impl!(self, i64::from_ne_bytes); |
| 731 |
} |
| 732 |
|
| 733 |
/// Gets an unsigned 128 bit integer from `self` in big-endian byte order. |
| 734 |
/// |
| 735 |
/// The current position is advanced by 16. |
| 736 |
/// |
| 737 |
/// # Examples |
| 738 |
/// |
| 739 |
/// ``` |
| 740 |
/// use bytes::Buf; |
| 741 |
/// |
| 742 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; |
| 743 |
/// assert_eq!(0x01020304050607080910111213141516, buf.get_u128()); |
| 744 |
/// ``` |
| 745 |
/// |
| 746 |
/// # Panics |
| 747 |
/// |
| 748 |
/// This function panics if there is not enough remaining data in `self`. |
| 749 |
fn get_u128(&mut self) -> u128 { |
| 750 |
buf_get_impl!(self, u128::from_be_bytes); |
| 751 |
} |
| 752 |
|
| 753 |
/// Gets an unsigned 128 bit integer from `self` in little-endian byte order. |
| 754 |
/// |
| 755 |
/// The current position is advanced by 16. |
| 756 |
/// |
| 757 |
/// # Examples |
| 758 |
/// |
| 759 |
/// ``` |
| 760 |
/// use bytes::Buf; |
| 761 |
/// |
| 762 |
/// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; |
| 763 |
/// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_le()); |
| 764 |
/// ``` |
| 765 |
/// |
| 766 |
/// # Panics |
| 767 |
/// |
| 768 |
/// This function panics if there is not enough remaining data in `self`. |
| 769 |
fn get_u128_le(&mut self) -> u128 { |
| 770 |
buf_get_impl!(self, u128::from_le_bytes); |
| 771 |
} |
| 772 |
|
| 773 |
/// Gets an unsigned 128 bit integer from `self` in native-endian byte order. |
| 774 |
/// |
| 775 |
/// The current position is advanced by 16. |
| 776 |
/// |
| 777 |
/// # Examples |
| 778 |
/// |
| 779 |
/// ``` |
| 780 |
/// use bytes::Buf; |
| 781 |
/// |
| 782 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 783 |
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello", |
| 784 |
/// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello", |
| 785 |
/// }; |
| 786 |
/// assert_eq!(0x01020304050607080910111213141516, buf.get_u128_ne()); |
| 787 |
/// ``` |
| 788 |
/// |
| 789 |
/// # Panics |
| 790 |
/// |
| 791 |
/// This function panics if there is not enough remaining data in `self`. |
| 792 |
fn get_u128_ne(&mut self) -> u128 { |
| 793 |
buf_get_impl!(self, u128::from_ne_bytes); |
| 794 |
} |
| 795 |
|
| 796 |
/// Gets a signed 128 bit integer from `self` in big-endian byte order. |
| 797 |
/// |
| 798 |
/// The current position is advanced by 16. |
| 799 |
/// |
| 800 |
/// # Examples |
| 801 |
/// |
| 802 |
/// ``` |
| 803 |
/// use bytes::Buf; |
| 804 |
/// |
| 805 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; |
| 806 |
/// assert_eq!(0x01020304050607080910111213141516, buf.get_i128()); |
| 807 |
/// ``` |
| 808 |
/// |
| 809 |
/// # Panics |
| 810 |
/// |
| 811 |
/// This function panics if there is not enough remaining data in `self`. |
| 812 |
fn get_i128(&mut self) -> i128 { |
| 813 |
buf_get_impl!(self, i128::from_be_bytes); |
| 814 |
} |
| 815 |
|
| 816 |
/// Gets a signed 128 bit integer from `self` in little-endian byte order. |
| 817 |
/// |
| 818 |
/// The current position is advanced by 16. |
| 819 |
/// |
| 820 |
/// # Examples |
| 821 |
/// |
| 822 |
/// ``` |
| 823 |
/// use bytes::Buf; |
| 824 |
/// |
| 825 |
/// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; |
| 826 |
/// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_le()); |
| 827 |
/// ``` |
| 828 |
/// |
| 829 |
/// # Panics |
| 830 |
/// |
| 831 |
/// This function panics if there is not enough remaining data in `self`. |
| 832 |
fn get_i128_le(&mut self) -> i128 { |
| 833 |
buf_get_impl!(self, i128::from_le_bytes); |
| 834 |
} |
| 835 |
|
| 836 |
/// Gets a signed 128 bit integer from `self` in native-endian byte order. |
| 837 |
/// |
| 838 |
/// The current position is advanced by 16. |
| 839 |
/// |
| 840 |
/// # Examples |
| 841 |
/// |
| 842 |
/// ``` |
| 843 |
/// use bytes::Buf; |
| 844 |
/// |
| 845 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 846 |
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello", |
| 847 |
/// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello", |
| 848 |
/// }; |
| 849 |
/// assert_eq!(0x01020304050607080910111213141516, buf.get_i128_ne()); |
| 850 |
/// ``` |
| 851 |
/// |
| 852 |
/// # Panics |
| 853 |
/// |
| 854 |
/// This function panics if there is not enough remaining data in `self`. |
| 855 |
fn get_i128_ne(&mut self) -> i128 { |
| 856 |
buf_get_impl!(self, i128::from_ne_bytes); |
| 857 |
} |
| 858 |
|
| 859 |
/// Gets an unsigned n-byte integer from `self` in big-endian byte order. |
| 860 |
/// |
| 861 |
/// The current position is advanced by `nbytes`. |
| 862 |
/// |
| 863 |
/// # Examples |
| 864 |
/// |
| 865 |
/// ``` |
| 866 |
/// use bytes::Buf; |
| 867 |
/// |
| 868 |
/// let mut buf = &b"\x01\x02\x03 hello"[..]; |
| 869 |
/// assert_eq!(0x010203, buf.get_uint(3)); |
| 870 |
/// ``` |
| 871 |
/// |
| 872 |
/// # Panics |
| 873 |
/// |
| 874 |
/// This function panics if there is not enough remaining data in `self`, or |
| 875 |
/// if `nbytes` is greater than 8. |
| 876 |
fn get_uint(&mut self, nbytes: usize) -> u64 { |
| 877 |
buf_get_impl!(be => self, u64, nbytes); |
| 878 |
} |
| 879 |
|
| 880 |
/// Gets an unsigned n-byte integer from `self` in little-endian byte order. |
| 881 |
/// |
| 882 |
/// The current position is advanced by `nbytes`. |
| 883 |
/// |
| 884 |
/// # Examples |
| 885 |
/// |
| 886 |
/// ``` |
| 887 |
/// use bytes::Buf; |
| 888 |
/// |
| 889 |
/// let mut buf = &b"\x03\x02\x01 hello"[..]; |
| 890 |
/// assert_eq!(0x010203, buf.get_uint_le(3)); |
| 891 |
/// ``` |
| 892 |
/// |
| 893 |
/// # Panics |
| 894 |
/// |
| 895 |
/// This function panics if there is not enough remaining data in `self`, or |
| 896 |
/// if `nbytes` is greater than 8. |
| 897 |
fn get_uint_le(&mut self, nbytes: usize) -> u64 { |
| 898 |
buf_get_impl!(le => self, u64, nbytes); |
| 899 |
} |
| 900 |
|
| 901 |
/// Gets an unsigned n-byte integer from `self` in native-endian byte order. |
| 902 |
/// |
| 903 |
/// The current position is advanced by `nbytes`. |
| 904 |
/// |
| 905 |
/// # Examples |
| 906 |
/// |
| 907 |
/// ``` |
| 908 |
/// use bytes::Buf; |
| 909 |
/// |
| 910 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 911 |
/// true => b"\x01\x02\x03 hello", |
| 912 |
/// false => b"\x03\x02\x01 hello", |
| 913 |
/// }; |
| 914 |
/// assert_eq!(0x010203, buf.get_uint_ne(3)); |
| 915 |
/// ``` |
| 916 |
/// |
| 917 |
/// # Panics |
| 918 |
/// |
| 919 |
/// This function panics if there is not enough remaining data in `self`, or |
| 920 |
/// if `nbytes` is greater than 8. |
| 921 |
fn get_uint_ne(&mut self, nbytes: usize) -> u64 { |
| 922 |
if cfg!(target_endian = "big") { |
| 923 |
self.get_uint(nbytes) |
| 924 |
} else { |
| 925 |
self.get_uint_le(nbytes) |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
/// Gets a signed n-byte integer from `self` in big-endian byte order. |
| 930 |
/// |
| 931 |
/// The current position is advanced by `nbytes`. |
| 932 |
/// |
| 933 |
/// # Examples |
| 934 |
/// |
| 935 |
/// ``` |
| 936 |
/// use bytes::Buf; |
| 937 |
/// |
| 938 |
/// let mut buf = &b"\x01\x02\x03 hello"[..]; |
| 939 |
/// assert_eq!(0x010203, buf.get_int(3)); |
| 940 |
/// ``` |
| 941 |
/// |
| 942 |
/// # Panics |
| 943 |
/// |
| 944 |
/// This function panics if there is not enough remaining data in `self`, or |
| 945 |
/// if `nbytes` is greater than 8. |
| 946 |
fn get_int(&mut self, nbytes: usize) -> i64 { |
| 947 |
sign_extend(self.get_uint(nbytes), nbytes) |
| 948 |
} |
| 949 |
|
| 950 |
/// Gets a signed n-byte integer from `self` in little-endian byte order. |
| 951 |
/// |
| 952 |
/// The current position is advanced by `nbytes`. |
| 953 |
/// |
| 954 |
/// # Examples |
| 955 |
/// |
| 956 |
/// ``` |
| 957 |
/// use bytes::Buf; |
| 958 |
/// |
| 959 |
/// let mut buf = &b"\x03\x02\x01 hello"[..]; |
| 960 |
/// assert_eq!(0x010203, buf.get_int_le(3)); |
| 961 |
/// ``` |
| 962 |
/// |
| 963 |
/// # Panics |
| 964 |
/// |
| 965 |
/// This function panics if there is not enough remaining data in `self`, or |
| 966 |
/// if `nbytes` is greater than 8. |
| 967 |
fn get_int_le(&mut self, nbytes: usize) -> i64 { |
| 968 |
sign_extend(self.get_uint_le(nbytes), nbytes) |
| 969 |
} |
| 970 |
|
| 971 |
/// Gets a signed n-byte integer from `self` in native-endian byte order. |
| 972 |
/// |
| 973 |
/// The current position is advanced by `nbytes`. |
| 974 |
/// |
| 975 |
/// # Examples |
| 976 |
/// |
| 977 |
/// ``` |
| 978 |
/// use bytes::Buf; |
| 979 |
/// |
| 980 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 981 |
/// true => b"\x01\x02\x03 hello", |
| 982 |
/// false => b"\x03\x02\x01 hello", |
| 983 |
/// }; |
| 984 |
/// assert_eq!(0x010203, buf.get_int_ne(3)); |
| 985 |
/// ``` |
| 986 |
/// |
| 987 |
/// # Panics |
| 988 |
/// |
| 989 |
/// This function panics if there is not enough remaining data in `self`, or |
| 990 |
/// if `nbytes` is greater than 8. |
| 991 |
fn get_int_ne(&mut self, nbytes: usize) -> i64 { |
| 992 |
if cfg!(target_endian = "big") { |
| 993 |
self.get_int(nbytes) |
| 994 |
} else { |
| 995 |
self.get_int_le(nbytes) |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
/// Gets an IEEE754 single-precision (4 bytes) floating point number from |
| 1000 |
/// `self` in big-endian byte order. |
| 1001 |
/// |
| 1002 |
/// The current position is advanced by 4. |
| 1003 |
/// |
| 1004 |
/// # Examples |
| 1005 |
/// |
| 1006 |
/// ``` |
| 1007 |
/// use bytes::Buf; |
| 1008 |
/// |
| 1009 |
/// let mut buf = &b"\x3F\x99\x99\x9A hello"[..]; |
| 1010 |
/// assert_eq!(1.2f32, buf.get_f32()); |
| 1011 |
/// ``` |
| 1012 |
/// |
| 1013 |
/// # Panics |
| 1014 |
/// |
| 1015 |
/// This function panics if there is not enough remaining data in `self`. |
| 1016 |
fn get_f32(&mut self) -> f32 { |
| 1017 |
f32::from_bits(self.get_u32()) |
| 1018 |
} |
| 1019 |
|
| 1020 |
/// Gets an IEEE754 single-precision (4 bytes) floating point number from |
| 1021 |
/// `self` in little-endian byte order. |
| 1022 |
/// |
| 1023 |
/// The current position is advanced by 4. |
| 1024 |
/// |
| 1025 |
/// # Examples |
| 1026 |
/// |
| 1027 |
/// ``` |
| 1028 |
/// use bytes::Buf; |
| 1029 |
/// |
| 1030 |
/// let mut buf = &b"\x9A\x99\x99\x3F hello"[..]; |
| 1031 |
/// assert_eq!(1.2f32, buf.get_f32_le()); |
| 1032 |
/// ``` |
| 1033 |
/// |
| 1034 |
/// # Panics |
| 1035 |
/// |
| 1036 |
/// This function panics if there is not enough remaining data in `self`. |
| 1037 |
fn get_f32_le(&mut self) -> f32 { |
| 1038 |
f32::from_bits(self.get_u32_le()) |
| 1039 |
} |
| 1040 |
|
| 1041 |
/// Gets an IEEE754 single-precision (4 bytes) floating point number from |
| 1042 |
/// `self` in native-endian byte order. |
| 1043 |
/// |
| 1044 |
/// The current position is advanced by 4. |
| 1045 |
/// |
| 1046 |
/// # Examples |
| 1047 |
/// |
| 1048 |
/// ``` |
| 1049 |
/// use bytes::Buf; |
| 1050 |
/// |
| 1051 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 1052 |
/// true => b"\x3F\x99\x99\x9A hello", |
| 1053 |
/// false => b"\x9A\x99\x99\x3F hello", |
| 1054 |
/// }; |
| 1055 |
/// assert_eq!(1.2f32, buf.get_f32_ne()); |
| 1056 |
/// ``` |
| 1057 |
/// |
| 1058 |
/// # Panics |
| 1059 |
/// |
| 1060 |
/// This function panics if there is not enough remaining data in `self`. |
| 1061 |
fn get_f32_ne(&mut self) -> f32 { |
| 1062 |
f32::from_bits(self.get_u32_ne()) |
| 1063 |
} |
| 1064 |
|
| 1065 |
/// Gets an IEEE754 double-precision (8 bytes) floating point number from |
| 1066 |
/// `self` in big-endian byte order. |
| 1067 |
/// |
| 1068 |
/// The current position is advanced by 8. |
| 1069 |
/// |
| 1070 |
/// # Examples |
| 1071 |
/// |
| 1072 |
/// ``` |
| 1073 |
/// use bytes::Buf; |
| 1074 |
/// |
| 1075 |
/// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"[..]; |
| 1076 |
/// assert_eq!(1.2f64, buf.get_f64()); |
| 1077 |
/// ``` |
| 1078 |
/// |
| 1079 |
/// # Panics |
| 1080 |
/// |
| 1081 |
/// This function panics if there is not enough remaining data in `self`. |
| 1082 |
fn get_f64(&mut self) -> f64 { |
| 1083 |
f64::from_bits(self.get_u64()) |
| 1084 |
} |
| 1085 |
|
| 1086 |
/// Gets an IEEE754 double-precision (8 bytes) floating point number from |
| 1087 |
/// `self` in little-endian byte order. |
| 1088 |
/// |
| 1089 |
/// The current position is advanced by 8. |
| 1090 |
/// |
| 1091 |
/// # Examples |
| 1092 |
/// |
| 1093 |
/// ``` |
| 1094 |
/// use bytes::Buf; |
| 1095 |
/// |
| 1096 |
/// let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"[..]; |
| 1097 |
/// assert_eq!(1.2f64, buf.get_f64_le()); |
| 1098 |
/// ``` |
| 1099 |
/// |
| 1100 |
/// # Panics |
| 1101 |
/// |
| 1102 |
/// This function panics if there is not enough remaining data in `self`. |
| 1103 |
fn get_f64_le(&mut self) -> f64 { |
| 1104 |
f64::from_bits(self.get_u64_le()) |
| 1105 |
} |
| 1106 |
|
| 1107 |
/// Gets an IEEE754 double-precision (8 bytes) floating point number from |
| 1108 |
/// `self` in native-endian byte order. |
| 1109 |
/// |
| 1110 |
/// The current position is advanced by 8. |
| 1111 |
/// |
| 1112 |
/// # Examples |
| 1113 |
/// |
| 1114 |
/// ``` |
| 1115 |
/// use bytes::Buf; |
| 1116 |
/// |
| 1117 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 1118 |
/// true => b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello", |
| 1119 |
/// false => b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello", |
| 1120 |
/// }; |
| 1121 |
/// assert_eq!(1.2f64, buf.get_f64_ne()); |
| 1122 |
/// ``` |
| 1123 |
/// |
| 1124 |
/// # Panics |
| 1125 |
/// |
| 1126 |
/// This function panics if there is not enough remaining data in `self`. |
| 1127 |
fn get_f64_ne(&mut self) -> f64 { |
| 1128 |
f64::from_bits(self.get_u64_ne()) |
| 1129 |
} |
| 1130 |
|
| 1131 |
/// Copies bytes from `self` into `dst`. |
| 1132 |
/// |
| 1133 |
/// The cursor is advanced by the number of bytes copied. `self` must have |
| 1134 |
/// enough remaining bytes to fill `dst`. |
| 1135 |
/// |
| 1136 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1137 |
/// remaining bytes to read the value. |
| 1138 |
/// |
| 1139 |
/// # Examples |
| 1140 |
/// |
| 1141 |
/// ``` |
| 1142 |
/// use bytes::Buf; |
| 1143 |
/// |
| 1144 |
/// let mut buf = &b"hello world"[..]; |
| 1145 |
/// let mut dst = [0; 5]; |
| 1146 |
/// |
| 1147 |
/// assert_eq!(Ok(()), buf.try_copy_to_slice(&mut dst)); |
| 1148 |
/// assert_eq!(&b"hello"[..], &dst); |
| 1149 |
/// assert_eq!(6, buf.remaining()); |
| 1150 |
/// ``` |
| 1151 |
/// |
| 1152 |
/// ``` |
| 1153 |
/// use bytes::{Buf, TryGetError}; |
| 1154 |
/// |
| 1155 |
/// let mut buf = &b"hello world"[..]; |
| 1156 |
/// let mut dst = [0; 12]; |
| 1157 |
/// |
| 1158 |
/// assert_eq!(Err(TryGetError{requested: 12, available: 11}), buf.try_copy_to_slice(&mut dst)); |
| 1159 |
/// assert_eq!(11, buf.remaining()); |
| 1160 |
/// ``` |
| 1161 |
fn try_copy_to_slice(&mut self, mut dst: &mut [u8]) -> Result<(), TryGetError> { |
| 1162 |
if self.remaining() < dst.len() { |
| 1163 |
return Err(TryGetError { |
| 1164 |
requested: dst.len(), |
| 1165 |
available: self.remaining(), |
| 1166 |
}); |
| 1167 |
} |
| 1168 |
|
| 1169 |
while !dst.is_empty() { |
| 1170 |
let src = self.chunk(); |
| 1171 |
let cnt = usize::min(src.len(), dst.len()); |
| 1172 |
|
| 1173 |
dst[..cnt].copy_from_slice(&src[..cnt]); |
| 1174 |
dst = &mut dst[cnt..]; |
| 1175 |
|
| 1176 |
self.advance(cnt); |
| 1177 |
} |
| 1178 |
Ok(()) |
| 1179 |
} |
| 1180 |
|
| 1181 |
/// Gets an unsigned 8 bit integer from `self`. |
| 1182 |
/// |
| 1183 |
/// The current position is advanced by 1. |
| 1184 |
/// |
| 1185 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1186 |
/// remaining bytes to read the value. |
| 1187 |
/// |
| 1188 |
/// # Examples |
| 1189 |
/// |
| 1190 |
/// ``` |
| 1191 |
/// use bytes::Buf; |
| 1192 |
/// |
| 1193 |
/// let mut buf = &b"\x08 hello"[..]; |
| 1194 |
/// assert_eq!(Ok(0x08_u8), buf.try_get_u8()); |
| 1195 |
/// assert_eq!(6, buf.remaining()); |
| 1196 |
/// ``` |
| 1197 |
/// |
| 1198 |
/// ``` |
| 1199 |
/// use bytes::{Buf, TryGetError}; |
| 1200 |
/// |
| 1201 |
/// let mut buf = &b""[..]; |
| 1202 |
/// assert_eq!(Err(TryGetError{requested: 1, available: 0}), buf.try_get_u8()); |
| 1203 |
/// ``` |
| 1204 |
fn try_get_u8(&mut self) -> Result<u8, TryGetError> { |
| 1205 |
if self.remaining() < 1 { |
| 1206 |
return Err(TryGetError { |
| 1207 |
requested: 1, |
| 1208 |
available: self.remaining(), |
| 1209 |
}); |
| 1210 |
} |
| 1211 |
let ret = self.chunk()[0]; |
| 1212 |
self.advance(1); |
| 1213 |
Ok(ret) |
| 1214 |
} |
| 1215 |
|
| 1216 |
/// Gets a signed 8 bit integer from `self`. |
| 1217 |
/// |
| 1218 |
/// The current position is advanced by 1. |
| 1219 |
/// |
| 1220 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1221 |
/// remaining bytes to read the value. |
| 1222 |
/// |
| 1223 |
/// # Examples |
| 1224 |
/// |
| 1225 |
/// ``` |
| 1226 |
/// use bytes::Buf; |
| 1227 |
/// |
| 1228 |
/// let mut buf = &b"\x08 hello"[..]; |
| 1229 |
/// assert_eq!(Ok(0x08_i8), buf.try_get_i8()); |
| 1230 |
/// assert_eq!(6, buf.remaining()); |
| 1231 |
/// ``` |
| 1232 |
/// |
| 1233 |
/// ``` |
| 1234 |
/// use bytes::{Buf, TryGetError}; |
| 1235 |
/// |
| 1236 |
/// let mut buf = &b""[..]; |
| 1237 |
/// assert_eq!(Err(TryGetError{requested: 1, available: 0}), buf.try_get_i8()); |
| 1238 |
/// ``` |
| 1239 |
fn try_get_i8(&mut self) -> Result<i8, TryGetError> { |
| 1240 |
if self.remaining() < 1 { |
| 1241 |
return Err(TryGetError { |
| 1242 |
requested: 1, |
| 1243 |
available: self.remaining(), |
| 1244 |
}); |
| 1245 |
} |
| 1246 |
let ret = self.chunk()[0] as i8; |
| 1247 |
self.advance(1); |
| 1248 |
Ok(ret) |
| 1249 |
} |
| 1250 |
|
| 1251 |
/// Gets an unsigned 16 bit integer from `self` in big-endian byte order. |
| 1252 |
/// |
| 1253 |
/// The current position is advanced by 2. |
| 1254 |
/// |
| 1255 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1256 |
/// remaining bytes to read the value. |
| 1257 |
/// |
| 1258 |
/// # Examples |
| 1259 |
/// |
| 1260 |
/// ``` |
| 1261 |
/// use bytes::Buf; |
| 1262 |
/// |
| 1263 |
/// let mut buf = &b"\x08\x09 hello"[..]; |
| 1264 |
/// assert_eq!(Ok(0x0809_u16), buf.try_get_u16()); |
| 1265 |
/// assert_eq!(6, buf.remaining()); |
| 1266 |
/// ``` |
| 1267 |
/// |
| 1268 |
/// ``` |
| 1269 |
/// use bytes::{Buf, TryGetError}; |
| 1270 |
/// |
| 1271 |
/// let mut buf = &b"\x08"[..]; |
| 1272 |
/// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_u16()); |
| 1273 |
/// assert_eq!(1, buf.remaining()); |
| 1274 |
/// ``` |
| 1275 |
fn try_get_u16(&mut self) -> Result<u16, TryGetError> { |
| 1276 |
buf_try_get_impl!(self, u16::from_be_bytes) |
| 1277 |
} |
| 1278 |
|
| 1279 |
/// Gets an unsigned 16 bit integer from `self` in little-endian byte order. |
| 1280 |
/// |
| 1281 |
/// The current position is advanced by 2. |
| 1282 |
/// |
| 1283 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1284 |
/// remaining bytes to read the value. |
| 1285 |
/// |
| 1286 |
/// # Examples |
| 1287 |
/// |
| 1288 |
/// ``` |
| 1289 |
/// use bytes::Buf; |
| 1290 |
/// |
| 1291 |
/// let mut buf = &b"\x09\x08 hello"[..]; |
| 1292 |
/// assert_eq!(Ok(0x0809_u16), buf.try_get_u16_le()); |
| 1293 |
/// assert_eq!(6, buf.remaining()); |
| 1294 |
/// ``` |
| 1295 |
/// |
| 1296 |
/// ``` |
| 1297 |
/// use bytes::{Buf, TryGetError}; |
| 1298 |
/// |
| 1299 |
/// let mut buf = &b"\x08"[..]; |
| 1300 |
/// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_u16_le()); |
| 1301 |
/// assert_eq!(1, buf.remaining()); |
| 1302 |
/// ``` |
| 1303 |
fn try_get_u16_le(&mut self) -> Result<u16, TryGetError> { |
| 1304 |
buf_try_get_impl!(self, u16::from_le_bytes) |
| 1305 |
} |
| 1306 |
|
| 1307 |
/// Gets an unsigned 16 bit integer from `self` in native-endian byte order. |
| 1308 |
/// |
| 1309 |
/// The current position is advanced by 2. |
| 1310 |
/// |
| 1311 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1312 |
/// remaining bytes to read the value. |
| 1313 |
/// |
| 1314 |
/// # Examples |
| 1315 |
/// |
| 1316 |
/// ``` |
| 1317 |
/// use bytes::Buf; |
| 1318 |
/// |
| 1319 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 1320 |
/// true => b"\x08\x09 hello", |
| 1321 |
/// false => b"\x09\x08 hello", |
| 1322 |
/// }; |
| 1323 |
/// assert_eq!(Ok(0x0809_u16), buf.try_get_u16_ne()); |
| 1324 |
/// assert_eq!(6, buf.remaining()); |
| 1325 |
/// ``` |
| 1326 |
/// |
| 1327 |
/// ``` |
| 1328 |
/// use bytes::{Buf, TryGetError}; |
| 1329 |
/// |
| 1330 |
/// let mut buf = &b"\x08"[..]; |
| 1331 |
/// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_u16_ne()); |
| 1332 |
/// assert_eq!(1, buf.remaining()); |
| 1333 |
/// ``` |
| 1334 |
fn try_get_u16_ne(&mut self) -> Result<u16, TryGetError> { |
| 1335 |
buf_try_get_impl!(self, u16::from_ne_bytes) |
| 1336 |
} |
| 1337 |
|
| 1338 |
/// Gets a signed 16 bit integer from `self` in big-endian byte order. |
| 1339 |
/// |
| 1340 |
/// The current position is advanced by 2. |
| 1341 |
/// |
| 1342 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1343 |
/// remaining bytes to read the value. |
| 1344 |
/// |
| 1345 |
/// # Examples |
| 1346 |
/// |
| 1347 |
/// ``` |
| 1348 |
/// use bytes::Buf; |
| 1349 |
/// |
| 1350 |
/// let mut buf = &b"\x08\x09 hello"[..]; |
| 1351 |
/// assert_eq!(Ok(0x0809_i16), buf.try_get_i16()); |
| 1352 |
/// assert_eq!(6, buf.remaining()); |
| 1353 |
/// ``` |
| 1354 |
/// |
| 1355 |
/// ``` |
| 1356 |
/// use bytes::{Buf, TryGetError}; |
| 1357 |
/// |
| 1358 |
/// let mut buf = &b"\x08"[..]; |
| 1359 |
/// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_i16()); |
| 1360 |
/// assert_eq!(1, buf.remaining()); |
| 1361 |
/// ``` |
| 1362 |
fn try_get_i16(&mut self) -> Result<i16, TryGetError> { |
| 1363 |
buf_try_get_impl!(self, i16::from_be_bytes) |
| 1364 |
} |
| 1365 |
|
| 1366 |
/// Gets an signed 16 bit integer from `self` in little-endian byte order. |
| 1367 |
/// |
| 1368 |
/// The current position is advanced by 2. |
| 1369 |
/// |
| 1370 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1371 |
/// remaining bytes to read the value. |
| 1372 |
/// |
| 1373 |
/// # Examples |
| 1374 |
/// |
| 1375 |
/// ``` |
| 1376 |
/// use bytes::Buf; |
| 1377 |
/// |
| 1378 |
/// let mut buf = &b"\x09\x08 hello"[..]; |
| 1379 |
/// assert_eq!(Ok(0x0809_i16), buf.try_get_i16_le()); |
| 1380 |
/// assert_eq!(6, buf.remaining()); |
| 1381 |
/// ``` |
| 1382 |
/// |
| 1383 |
/// ``` |
| 1384 |
/// use bytes::{Buf, TryGetError}; |
| 1385 |
/// |
| 1386 |
/// let mut buf = &b"\x08"[..]; |
| 1387 |
/// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_i16_le()); |
| 1388 |
/// assert_eq!(1, buf.remaining()); |
| 1389 |
/// ``` |
| 1390 |
fn try_get_i16_le(&mut self) -> Result<i16, TryGetError> { |
| 1391 |
buf_try_get_impl!(self, i16::from_le_bytes) |
| 1392 |
} |
| 1393 |
|
| 1394 |
/// Gets a signed 16 bit integer from `self` in native-endian byte order. |
| 1395 |
/// |
| 1396 |
/// The current position is advanced by 2. |
| 1397 |
/// |
| 1398 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1399 |
/// remaining bytes to read the value. |
| 1400 |
/// |
| 1401 |
/// # Examples |
| 1402 |
/// |
| 1403 |
/// ``` |
| 1404 |
/// use bytes::Buf; |
| 1405 |
/// |
| 1406 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 1407 |
/// true => b"\x08\x09 hello", |
| 1408 |
/// false => b"\x09\x08 hello", |
| 1409 |
/// }; |
| 1410 |
/// assert_eq!(Ok(0x0809_i16), buf.try_get_i16_ne()); |
| 1411 |
/// assert_eq!(6, buf.remaining()); |
| 1412 |
/// ``` |
| 1413 |
/// |
| 1414 |
/// ``` |
| 1415 |
/// use bytes::{Buf, TryGetError}; |
| 1416 |
/// |
| 1417 |
/// let mut buf = &b"\x08"[..]; |
| 1418 |
/// assert_eq!(Err(TryGetError{requested: 2, available: 1}), buf.try_get_i16_ne()); |
| 1419 |
/// assert_eq!(1, buf.remaining()); |
| 1420 |
/// ``` |
| 1421 |
fn try_get_i16_ne(&mut self) -> Result<i16, TryGetError> { |
| 1422 |
buf_try_get_impl!(self, i16::from_ne_bytes) |
| 1423 |
} |
| 1424 |
|
| 1425 |
/// Gets an unsigned 32 bit integer from `self` in big-endian byte order. |
| 1426 |
/// |
| 1427 |
/// The current position is advanced by 4. |
| 1428 |
/// |
| 1429 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1430 |
/// remaining bytes to read the value. |
| 1431 |
/// |
| 1432 |
/// # Examples |
| 1433 |
/// |
| 1434 |
/// ``` |
| 1435 |
/// use bytes::Buf; |
| 1436 |
/// |
| 1437 |
/// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; |
| 1438 |
/// assert_eq!(Ok(0x0809A0A1), buf.try_get_u32()); |
| 1439 |
/// assert_eq!(6, buf.remaining()); |
| 1440 |
/// ``` |
| 1441 |
/// |
| 1442 |
/// ``` |
| 1443 |
/// use bytes::{Buf, TryGetError}; |
| 1444 |
/// |
| 1445 |
/// let mut buf = &b"\x01\x02\x03"[..]; |
| 1446 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_u32()); |
| 1447 |
/// assert_eq!(3, buf.remaining()); |
| 1448 |
/// ``` |
| 1449 |
fn try_get_u32(&mut self) -> Result<u32, TryGetError> { |
| 1450 |
buf_try_get_impl!(self, u32::from_be_bytes) |
| 1451 |
} |
| 1452 |
|
| 1453 |
/// Gets an unsigned 32 bit integer from `self` in little-endian byte order. |
| 1454 |
/// |
| 1455 |
/// The current position is advanced by 4. |
| 1456 |
/// |
| 1457 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1458 |
/// remaining bytes to read the value. |
| 1459 |
/// |
| 1460 |
/// # Examples |
| 1461 |
/// |
| 1462 |
/// ``` |
| 1463 |
/// use bytes::Buf; |
| 1464 |
/// |
| 1465 |
/// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; |
| 1466 |
/// assert_eq!(Ok(0x0809A0A1_u32), buf.try_get_u32_le()); |
| 1467 |
/// assert_eq!(6, buf.remaining()); |
| 1468 |
/// ``` |
| 1469 |
/// |
| 1470 |
/// ``` |
| 1471 |
/// use bytes::{Buf, TryGetError}; |
| 1472 |
/// |
| 1473 |
/// let mut buf = &b"\x08\x09\xA0"[..]; |
| 1474 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_u32_le()); |
| 1475 |
/// assert_eq!(3, buf.remaining()); |
| 1476 |
/// ``` |
| 1477 |
fn try_get_u32_le(&mut self) -> Result<u32, TryGetError> { |
| 1478 |
buf_try_get_impl!(self, u32::from_le_bytes) |
| 1479 |
} |
| 1480 |
|
| 1481 |
/// Gets an unsigned 32 bit integer from `self` in native-endian byte order. |
| 1482 |
/// |
| 1483 |
/// The current position is advanced by 4. |
| 1484 |
/// |
| 1485 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1486 |
/// remaining bytes to read the value. |
| 1487 |
/// |
| 1488 |
/// # Examples |
| 1489 |
/// |
| 1490 |
/// ``` |
| 1491 |
/// use bytes::Buf; |
| 1492 |
/// |
| 1493 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 1494 |
/// true => b"\x08\x09\xA0\xA1 hello", |
| 1495 |
/// false => b"\xA1\xA0\x09\x08 hello", |
| 1496 |
/// }; |
| 1497 |
/// assert_eq!(Ok(0x0809A0A1_u32), buf.try_get_u32_ne()); |
| 1498 |
/// assert_eq!(6, buf.remaining()); |
| 1499 |
/// ``` |
| 1500 |
/// |
| 1501 |
/// ``` |
| 1502 |
/// use bytes::{Buf, TryGetError}; |
| 1503 |
/// |
| 1504 |
/// let mut buf = &b"\x08\x09\xA0"[..]; |
| 1505 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_u32_ne()); |
| 1506 |
/// assert_eq!(3, buf.remaining()); |
| 1507 |
/// ``` |
| 1508 |
fn try_get_u32_ne(&mut self) -> Result<u32, TryGetError> { |
| 1509 |
buf_try_get_impl!(self, u32::from_ne_bytes) |
| 1510 |
} |
| 1511 |
|
| 1512 |
/// Gets a signed 32 bit integer from `self` in big-endian byte order. |
| 1513 |
/// |
| 1514 |
/// The current position is advanced by 4. |
| 1515 |
/// |
| 1516 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1517 |
/// remaining bytes to read the value. |
| 1518 |
/// |
| 1519 |
/// # Examples |
| 1520 |
/// |
| 1521 |
/// ``` |
| 1522 |
/// use bytes::Buf; |
| 1523 |
/// |
| 1524 |
/// let mut buf = &b"\x08\x09\xA0\xA1 hello"[..]; |
| 1525 |
/// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32()); |
| 1526 |
/// assert_eq!(6, buf.remaining()); |
| 1527 |
/// ``` |
| 1528 |
/// |
| 1529 |
/// ``` |
| 1530 |
/// use bytes::{Buf, TryGetError}; |
| 1531 |
/// |
| 1532 |
/// let mut buf = &b"\x01\x02\x03"[..]; |
| 1533 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_i32()); |
| 1534 |
/// assert_eq!(3, buf.remaining()); |
| 1535 |
/// ``` |
| 1536 |
fn try_get_i32(&mut self) -> Result<i32, TryGetError> { |
| 1537 |
buf_try_get_impl!(self, i32::from_be_bytes) |
| 1538 |
} |
| 1539 |
|
| 1540 |
/// Gets a signed 32 bit integer from `self` in little-endian byte order. |
| 1541 |
/// |
| 1542 |
/// The current position is advanced by 4. |
| 1543 |
/// |
| 1544 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1545 |
/// remaining bytes to read the value. |
| 1546 |
/// |
| 1547 |
/// # Examples |
| 1548 |
/// |
| 1549 |
/// ``` |
| 1550 |
/// use bytes::Buf; |
| 1551 |
/// |
| 1552 |
/// let mut buf = &b"\xA1\xA0\x09\x08 hello"[..]; |
| 1553 |
/// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32_le()); |
| 1554 |
/// assert_eq!(6, buf.remaining()); |
| 1555 |
/// ``` |
| 1556 |
/// |
| 1557 |
/// ``` |
| 1558 |
/// use bytes::{Buf, TryGetError}; |
| 1559 |
/// |
| 1560 |
/// let mut buf = &b"\x08\x09\xA0"[..]; |
| 1561 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_i32_le()); |
| 1562 |
/// assert_eq!(3, buf.remaining()); |
| 1563 |
/// ``` |
| 1564 |
fn try_get_i32_le(&mut self) -> Result<i32, TryGetError> { |
| 1565 |
buf_try_get_impl!(self, i32::from_le_bytes) |
| 1566 |
} |
| 1567 |
|
| 1568 |
/// Gets a signed 32 bit integer from `self` in native-endian byte order. |
| 1569 |
/// |
| 1570 |
/// The current position is advanced by 4. |
| 1571 |
/// |
| 1572 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1573 |
/// remaining bytes to read the value. |
| 1574 |
/// |
| 1575 |
/// # Examples |
| 1576 |
/// |
| 1577 |
/// ``` |
| 1578 |
/// use bytes::Buf; |
| 1579 |
/// |
| 1580 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 1581 |
/// true => b"\x08\x09\xA0\xA1 hello", |
| 1582 |
/// false => b"\xA1\xA0\x09\x08 hello", |
| 1583 |
/// }; |
| 1584 |
/// assert_eq!(Ok(0x0809A0A1_i32), buf.try_get_i32_ne()); |
| 1585 |
/// assert_eq!(6, buf.remaining()); |
| 1586 |
/// ``` |
| 1587 |
/// |
| 1588 |
/// ``` |
| 1589 |
/// use bytes::{Buf, TryGetError}; |
| 1590 |
/// |
| 1591 |
/// let mut buf = &b"\x08\x09\xA0"[..]; |
| 1592 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_i32_ne()); |
| 1593 |
/// assert_eq!(3, buf.remaining()); |
| 1594 |
/// ``` |
| 1595 |
fn try_get_i32_ne(&mut self) -> Result<i32, TryGetError> { |
| 1596 |
buf_try_get_impl!(self, i32::from_ne_bytes) |
| 1597 |
} |
| 1598 |
|
| 1599 |
/// Gets an unsigned 64 bit integer from `self` in big-endian byte order. |
| 1600 |
/// |
| 1601 |
/// The current position is advanced by 8. |
| 1602 |
/// |
| 1603 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1604 |
/// remaining bytes to read the value. |
| 1605 |
/// |
| 1606 |
/// # Examples |
| 1607 |
/// |
| 1608 |
/// ``` |
| 1609 |
/// use bytes::Buf; |
| 1610 |
/// |
| 1611 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; |
| 1612 |
/// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64()); |
| 1613 |
/// assert_eq!(6, buf.remaining()); |
| 1614 |
/// ``` |
| 1615 |
/// |
| 1616 |
/// ``` |
| 1617 |
/// use bytes::{Buf, TryGetError}; |
| 1618 |
/// |
| 1619 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; |
| 1620 |
/// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_u64()); |
| 1621 |
/// assert_eq!(7, buf.remaining()); |
| 1622 |
/// ``` |
| 1623 |
fn try_get_u64(&mut self) -> Result<u64, TryGetError> { |
| 1624 |
buf_try_get_impl!(self, u64::from_be_bytes) |
| 1625 |
} |
| 1626 |
|
| 1627 |
/// Gets an unsigned 64 bit integer from `self` in little-endian byte order. |
| 1628 |
/// |
| 1629 |
/// The current position is advanced by 8. |
| 1630 |
/// |
| 1631 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1632 |
/// remaining bytes to read the value. |
| 1633 |
/// |
| 1634 |
/// # Examples |
| 1635 |
/// |
| 1636 |
/// ``` |
| 1637 |
/// use bytes::Buf; |
| 1638 |
/// |
| 1639 |
/// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; |
| 1640 |
/// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64_le()); |
| 1641 |
/// assert_eq!(6, buf.remaining()); |
| 1642 |
/// ``` |
| 1643 |
/// |
| 1644 |
/// ``` |
| 1645 |
/// use bytes::{Buf, TryGetError}; |
| 1646 |
/// |
| 1647 |
/// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; |
| 1648 |
/// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_u64_le()); |
| 1649 |
/// assert_eq!(7, buf.remaining()); |
| 1650 |
/// ``` |
| 1651 |
fn try_get_u64_le(&mut self) -> Result<u64, TryGetError> { |
| 1652 |
buf_try_get_impl!(self, u64::from_le_bytes) |
| 1653 |
} |
| 1654 |
|
| 1655 |
/// Gets an unsigned 64 bit integer from `self` in native-endian byte order. |
| 1656 |
/// |
| 1657 |
/// The current position is advanced by 8. |
| 1658 |
/// |
| 1659 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1660 |
/// remaining bytes to read the value. |
| 1661 |
/// |
| 1662 |
/// # Examples |
| 1663 |
/// |
| 1664 |
/// ``` |
| 1665 |
/// use bytes::Buf; |
| 1666 |
/// |
| 1667 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 1668 |
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello", |
| 1669 |
/// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello", |
| 1670 |
/// }; |
| 1671 |
/// assert_eq!(Ok(0x0102030405060708_u64), buf.try_get_u64_ne()); |
| 1672 |
/// assert_eq!(6, buf.remaining()); |
| 1673 |
/// ``` |
| 1674 |
/// |
| 1675 |
/// ``` |
| 1676 |
/// use bytes::{Buf, TryGetError}; |
| 1677 |
/// |
| 1678 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; |
| 1679 |
/// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_u64_ne()); |
| 1680 |
/// assert_eq!(7, buf.remaining()); |
| 1681 |
/// ``` |
| 1682 |
fn try_get_u64_ne(&mut self) -> Result<u64, TryGetError> { |
| 1683 |
buf_try_get_impl!(self, u64::from_ne_bytes) |
| 1684 |
} |
| 1685 |
|
| 1686 |
/// Gets a signed 64 bit integer from `self` in big-endian byte order. |
| 1687 |
/// |
| 1688 |
/// The current position is advanced by 8. |
| 1689 |
/// |
| 1690 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1691 |
/// remaining bytes to read the value. |
| 1692 |
/// |
| 1693 |
/// # Examples |
| 1694 |
/// |
| 1695 |
/// ``` |
| 1696 |
/// use bytes::Buf; |
| 1697 |
/// |
| 1698 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08 hello"[..]; |
| 1699 |
/// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64()); |
| 1700 |
/// assert_eq!(6, buf.remaining()); |
| 1701 |
/// ``` |
| 1702 |
/// |
| 1703 |
/// ``` |
| 1704 |
/// use bytes::{Buf, TryGetError}; |
| 1705 |
/// |
| 1706 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; |
| 1707 |
/// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_i64()); |
| 1708 |
/// assert_eq!(7, buf.remaining()); |
| 1709 |
/// ``` |
| 1710 |
fn try_get_i64(&mut self) -> Result<i64, TryGetError> { |
| 1711 |
buf_try_get_impl!(self, i64::from_be_bytes) |
| 1712 |
} |
| 1713 |
|
| 1714 |
/// Gets a signed 64 bit integer from `self` in little-endian byte order. |
| 1715 |
/// |
| 1716 |
/// The current position is advanced by 8. |
| 1717 |
/// |
| 1718 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1719 |
/// remaining bytes to read the value. |
| 1720 |
/// |
| 1721 |
/// # Examples |
| 1722 |
/// |
| 1723 |
/// ``` |
| 1724 |
/// use bytes::Buf; |
| 1725 |
/// |
| 1726 |
/// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; |
| 1727 |
/// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64_le()); |
| 1728 |
/// assert_eq!(6, buf.remaining()); |
| 1729 |
/// ``` |
| 1730 |
/// |
| 1731 |
/// ``` |
| 1732 |
/// use bytes::{Buf, TryGetError}; |
| 1733 |
/// |
| 1734 |
/// let mut buf = &b"\x08\x07\x06\x05\x04\x03\x02"[..]; |
| 1735 |
/// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_i64_le()); |
| 1736 |
/// assert_eq!(7, buf.remaining()); |
| 1737 |
/// ``` |
| 1738 |
fn try_get_i64_le(&mut self) -> Result<i64, TryGetError> { |
| 1739 |
buf_try_get_impl!(self, i64::from_le_bytes) |
| 1740 |
} |
| 1741 |
|
| 1742 |
/// Gets a signed 64 bit integer from `self` in native-endian byte order. |
| 1743 |
/// |
| 1744 |
/// The current position is advanced by 8. |
| 1745 |
/// |
| 1746 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1747 |
/// remaining bytes to read the value. |
| 1748 |
/// |
| 1749 |
/// # Examples |
| 1750 |
/// |
| 1751 |
/// ``` |
| 1752 |
/// use bytes::Buf; |
| 1753 |
/// |
| 1754 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 1755 |
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08 hello", |
| 1756 |
/// false => b"\x08\x07\x06\x05\x04\x03\x02\x01 hello", |
| 1757 |
/// }; |
| 1758 |
/// assert_eq!(Ok(0x0102030405060708_i64), buf.try_get_i64_ne()); |
| 1759 |
/// assert_eq!(6, buf.remaining()); |
| 1760 |
/// ``` |
| 1761 |
/// |
| 1762 |
/// ``` |
| 1763 |
/// use bytes::{Buf, TryGetError}; |
| 1764 |
/// |
| 1765 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07"[..]; |
| 1766 |
/// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_i64_ne()); |
| 1767 |
/// assert_eq!(7, buf.remaining()); |
| 1768 |
/// ``` |
| 1769 |
fn try_get_i64_ne(&mut self) -> Result<i64, TryGetError> { |
| 1770 |
buf_try_get_impl!(self, i64::from_ne_bytes) |
| 1771 |
} |
| 1772 |
|
| 1773 |
/// Gets an unsigned 128 bit integer from `self` in big-endian byte order. |
| 1774 |
/// |
| 1775 |
/// The current position is advanced by 16. |
| 1776 |
/// |
| 1777 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1778 |
/// remaining bytes to read the value. |
| 1779 |
/// |
| 1780 |
/// # Examples |
| 1781 |
/// |
| 1782 |
/// ``` |
| 1783 |
/// use bytes::Buf; |
| 1784 |
/// |
| 1785 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; |
| 1786 |
/// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128()); |
| 1787 |
/// assert_eq!(6, buf.remaining()); |
| 1788 |
/// ``` |
| 1789 |
/// |
| 1790 |
/// ``` |
| 1791 |
/// use bytes::{Buf, TryGetError}; |
| 1792 |
/// |
| 1793 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; |
| 1794 |
/// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_u128()); |
| 1795 |
/// assert_eq!(15, buf.remaining()); |
| 1796 |
/// ``` |
| 1797 |
fn try_get_u128(&mut self) -> Result<u128, TryGetError> { |
| 1798 |
buf_try_get_impl!(self, u128::from_be_bytes) |
| 1799 |
} |
| 1800 |
|
| 1801 |
/// Gets an unsigned 128 bit integer from `self` in little-endian byte order. |
| 1802 |
/// |
| 1803 |
/// The current position is advanced by 16. |
| 1804 |
/// |
| 1805 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1806 |
/// remaining bytes to read the value. |
| 1807 |
/// |
| 1808 |
/// # Examples |
| 1809 |
/// |
| 1810 |
/// ``` |
| 1811 |
/// use bytes::Buf; |
| 1812 |
/// |
| 1813 |
/// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; |
| 1814 |
/// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128_le()); |
| 1815 |
/// assert_eq!(6, buf.remaining()); |
| 1816 |
/// ``` |
| 1817 |
/// |
| 1818 |
/// ``` |
| 1819 |
/// use bytes::{Buf, TryGetError}; |
| 1820 |
/// |
| 1821 |
/// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; |
| 1822 |
/// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_u128_le()); |
| 1823 |
/// assert_eq!(15, buf.remaining()); |
| 1824 |
/// ``` |
| 1825 |
fn try_get_u128_le(&mut self) -> Result<u128, TryGetError> { |
| 1826 |
buf_try_get_impl!(self, u128::from_le_bytes) |
| 1827 |
} |
| 1828 |
|
| 1829 |
/// Gets an unsigned 128 bit integer from `self` in native-endian byte order. |
| 1830 |
/// |
| 1831 |
/// The current position is advanced by 16. |
| 1832 |
/// |
| 1833 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1834 |
/// remaining bytes to read the value. |
| 1835 |
/// |
| 1836 |
/// # Examples |
| 1837 |
/// |
| 1838 |
/// ``` |
| 1839 |
/// use bytes::Buf; |
| 1840 |
/// |
| 1841 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 1842 |
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello", |
| 1843 |
/// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello", |
| 1844 |
/// }; |
| 1845 |
/// assert_eq!(Ok(0x01020304050607080910111213141516_u128), buf.try_get_u128_ne()); |
| 1846 |
/// assert_eq!(6, buf.remaining()); |
| 1847 |
/// ``` |
| 1848 |
/// |
| 1849 |
/// ``` |
| 1850 |
/// use bytes::{Buf, TryGetError}; |
| 1851 |
/// |
| 1852 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; |
| 1853 |
/// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_u128_ne()); |
| 1854 |
/// assert_eq!(15, buf.remaining()); |
| 1855 |
/// ``` |
| 1856 |
fn try_get_u128_ne(&mut self) -> Result<u128, TryGetError> { |
| 1857 |
buf_try_get_impl!(self, u128::from_ne_bytes) |
| 1858 |
} |
| 1859 |
|
| 1860 |
/// Gets a signed 128 bit integer from `self` in big-endian byte order. |
| 1861 |
/// |
| 1862 |
/// The current position is advanced by 16. |
| 1863 |
/// |
| 1864 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1865 |
/// remaining bytes to read the value. |
| 1866 |
/// |
| 1867 |
/// # Examples |
| 1868 |
/// |
| 1869 |
/// ``` |
| 1870 |
/// use bytes::Buf; |
| 1871 |
/// |
| 1872 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello"[..]; |
| 1873 |
/// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128()); |
| 1874 |
/// assert_eq!(6, buf.remaining()); |
| 1875 |
/// ``` |
| 1876 |
/// |
| 1877 |
/// ``` |
| 1878 |
/// use bytes::{Buf, TryGetError}; |
| 1879 |
/// |
| 1880 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; |
| 1881 |
/// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_i128()); |
| 1882 |
/// assert_eq!(15, buf.remaining()); |
| 1883 |
/// ``` |
| 1884 |
fn try_get_i128(&mut self) -> Result<i128, TryGetError> { |
| 1885 |
buf_try_get_impl!(self, i128::from_be_bytes) |
| 1886 |
} |
| 1887 |
|
| 1888 |
/// Gets a signed 128 bit integer from `self` in little-endian byte order. |
| 1889 |
/// |
| 1890 |
/// The current position is advanced by 16. |
| 1891 |
/// |
| 1892 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1893 |
/// remaining bytes to read the value. |
| 1894 |
/// |
| 1895 |
/// # Examples |
| 1896 |
/// |
| 1897 |
/// ``` |
| 1898 |
/// use bytes::Buf; |
| 1899 |
/// |
| 1900 |
/// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello"[..]; |
| 1901 |
/// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128_le()); |
| 1902 |
/// assert_eq!(6, buf.remaining()); |
| 1903 |
/// ``` |
| 1904 |
/// |
| 1905 |
/// ``` |
| 1906 |
/// use bytes::{Buf, TryGetError}; |
| 1907 |
/// |
| 1908 |
/// let mut buf = &b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02"[..]; |
| 1909 |
/// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_i128_le()); |
| 1910 |
/// assert_eq!(15, buf.remaining()); |
| 1911 |
/// ``` |
| 1912 |
fn try_get_i128_le(&mut self) -> Result<i128, TryGetError> { |
| 1913 |
buf_try_get_impl!(self, i128::from_le_bytes) |
| 1914 |
} |
| 1915 |
|
| 1916 |
/// Gets a signed 128 bit integer from `self` in native-endian byte order. |
| 1917 |
/// |
| 1918 |
/// The current position is advanced by 16. |
| 1919 |
/// |
| 1920 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1921 |
/// remaining bytes to read the value. |
| 1922 |
/// |
| 1923 |
/// # Examples |
| 1924 |
/// |
| 1925 |
/// ``` |
| 1926 |
/// use bytes::Buf; |
| 1927 |
/// |
| 1928 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 1929 |
/// true => b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16 hello", |
| 1930 |
/// false => b"\x16\x15\x14\x13\x12\x11\x10\x09\x08\x07\x06\x05\x04\x03\x02\x01 hello", |
| 1931 |
/// }; |
| 1932 |
/// assert_eq!(Ok(0x01020304050607080910111213141516_i128), buf.try_get_i128_ne()); |
| 1933 |
/// assert_eq!(6, buf.remaining()); |
| 1934 |
/// ``` |
| 1935 |
/// |
| 1936 |
/// ``` |
| 1937 |
/// use bytes::{Buf, TryGetError}; |
| 1938 |
/// |
| 1939 |
/// let mut buf = &b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15"[..]; |
| 1940 |
/// assert_eq!(Err(TryGetError{requested: 16, available: 15}), buf.try_get_i128_ne()); |
| 1941 |
/// assert_eq!(15, buf.remaining()); |
| 1942 |
/// ``` |
| 1943 |
fn try_get_i128_ne(&mut self) -> Result<i128, TryGetError> { |
| 1944 |
buf_try_get_impl!(self, i128::from_ne_bytes) |
| 1945 |
} |
| 1946 |
|
| 1947 |
/// Gets an unsigned n-byte integer from `self` in big-endian byte order. |
| 1948 |
/// |
| 1949 |
/// The current position is advanced by `nbytes`. |
| 1950 |
/// |
| 1951 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1952 |
/// remaining bytes to read the value. |
| 1953 |
/// |
| 1954 |
/// # Examples |
| 1955 |
/// |
| 1956 |
/// ``` |
| 1957 |
/// use bytes::Buf; |
| 1958 |
/// |
| 1959 |
/// let mut buf = &b"\x01\x02\x03 hello"[..]; |
| 1960 |
/// assert_eq!(Ok(0x010203_u64), buf.try_get_uint(3)); |
| 1961 |
/// assert_eq!(6, buf.remaining()); |
| 1962 |
/// ``` |
| 1963 |
/// |
| 1964 |
/// ``` |
| 1965 |
/// use bytes::{Buf, TryGetError}; |
| 1966 |
/// |
| 1967 |
/// let mut buf = &b"\x01\x02\x03"[..]; |
| 1968 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_uint(4)); |
| 1969 |
/// assert_eq!(3, buf.remaining()); |
| 1970 |
/// ``` |
| 1971 |
/// |
| 1972 |
/// # Panics |
| 1973 |
/// |
| 1974 |
/// This function panics if `nbytes` > 8. |
| 1975 |
fn try_get_uint(&mut self, nbytes: usize) -> Result<u64, TryGetError> { |
| 1976 |
buf_try_get_impl!(be => self, u64, nbytes); |
| 1977 |
} |
| 1978 |
|
| 1979 |
/// Gets an unsigned n-byte integer from `self` in little-endian byte order. |
| 1980 |
/// |
| 1981 |
/// The current position is advanced by `nbytes`. |
| 1982 |
/// |
| 1983 |
/// Returns `Err(TryGetError)` when there are not enough |
| 1984 |
/// remaining bytes to read the value. |
| 1985 |
/// |
| 1986 |
/// # Examples |
| 1987 |
/// |
| 1988 |
/// ``` |
| 1989 |
/// use bytes::Buf; |
| 1990 |
/// |
| 1991 |
/// let mut buf = &b"\x03\x02\x01 hello"[..]; |
| 1992 |
/// assert_eq!(Ok(0x010203_u64), buf.try_get_uint_le(3)); |
| 1993 |
/// assert_eq!(6, buf.remaining()); |
| 1994 |
/// ``` |
| 1995 |
/// |
| 1996 |
/// ``` |
| 1997 |
/// use bytes::{Buf, TryGetError}; |
| 1998 |
/// |
| 1999 |
/// let mut buf = &b"\x01\x02\x03"[..]; |
| 2000 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_uint_le(4)); |
| 2001 |
/// assert_eq!(3, buf.remaining()); |
| 2002 |
/// ``` |
| 2003 |
/// |
| 2004 |
/// # Panics |
| 2005 |
/// |
| 2006 |
/// This function panics if `nbytes` > 8. |
| 2007 |
fn try_get_uint_le(&mut self, nbytes: usize) -> Result<u64, TryGetError> { |
| 2008 |
buf_try_get_impl!(le => self, u64, nbytes); |
| 2009 |
} |
| 2010 |
|
| 2011 |
/// Gets an unsigned n-byte integer from `self` in native-endian byte order. |
| 2012 |
/// |
| 2013 |
/// The current position is advanced by `nbytes`. |
| 2014 |
/// |
| 2015 |
/// Returns `Err(TryGetError)` when there are not enough |
| 2016 |
/// remaining bytes to read the value. |
| 2017 |
/// |
| 2018 |
/// # Examples |
| 2019 |
/// |
| 2020 |
/// ``` |
| 2021 |
/// use bytes::Buf; |
| 2022 |
/// |
| 2023 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 2024 |
/// true => b"\x01\x02\x03 hello", |
| 2025 |
/// false => b"\x03\x02\x01 hello", |
| 2026 |
/// }; |
| 2027 |
/// assert_eq!(Ok(0x010203_u64), buf.try_get_uint_ne(3)); |
| 2028 |
/// assert_eq!(6, buf.remaining()); |
| 2029 |
/// ``` |
| 2030 |
/// |
| 2031 |
/// ``` |
| 2032 |
/// use bytes::{Buf, TryGetError}; |
| 2033 |
/// |
| 2034 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 2035 |
/// true => b"\x01\x02\x03", |
| 2036 |
/// false => b"\x03\x02\x01", |
| 2037 |
/// }; |
| 2038 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_uint_ne(4)); |
| 2039 |
/// assert_eq!(3, buf.remaining()); |
| 2040 |
/// ``` |
| 2041 |
/// |
| 2042 |
/// # Panics |
| 2043 |
/// |
| 2044 |
/// This function panics if `nbytes` is greater than 8. |
| 2045 |
fn try_get_uint_ne(&mut self, nbytes: usize) -> Result<u64, TryGetError> { |
| 2046 |
if cfg!(target_endian = "big") { |
| 2047 |
self.try_get_uint(nbytes) |
| 2048 |
} else { |
| 2049 |
self.try_get_uint_le(nbytes) |
| 2050 |
} |
| 2051 |
} |
| 2052 |
|
| 2053 |
/// Gets a signed n-byte integer from `self` in big-endian byte order. |
| 2054 |
/// |
| 2055 |
/// The current position is advanced by `nbytes`. |
| 2056 |
/// |
| 2057 |
/// Returns `Err(TryGetError)` when there are not enough |
| 2058 |
/// remaining bytes to read the value. |
| 2059 |
/// |
| 2060 |
/// # Examples |
| 2061 |
/// |
| 2062 |
/// ``` |
| 2063 |
/// use bytes::Buf; |
| 2064 |
/// |
| 2065 |
/// let mut buf = &b"\x01\x02\x03 hello"[..]; |
| 2066 |
/// assert_eq!(Ok(0x010203_i64), buf.try_get_int(3)); |
| 2067 |
/// assert_eq!(6, buf.remaining()); |
| 2068 |
/// ``` |
| 2069 |
/// |
| 2070 |
/// ``` |
| 2071 |
/// use bytes::{Buf, TryGetError}; |
| 2072 |
/// |
| 2073 |
/// let mut buf = &b"\x01\x02\x03"[..]; |
| 2074 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_int(4)); |
| 2075 |
/// assert_eq!(3, buf.remaining()); |
| 2076 |
/// ``` |
| 2077 |
/// |
| 2078 |
/// # Panics |
| 2079 |
/// |
| 2080 |
/// This function panics if `nbytes` is greater than 8. |
| 2081 |
fn try_get_int(&mut self, nbytes: usize) -> Result<i64, TryGetError> { |
| 2082 |
buf_try_get_impl!(be => self, i64, nbytes); |
| 2083 |
} |
| 2084 |
|
| 2085 |
/// Gets a signed n-byte integer from `self` in little-endian byte order. |
| 2086 |
/// |
| 2087 |
/// The current position is advanced by `nbytes`. |
| 2088 |
/// |
| 2089 |
/// Returns `Err(TryGetError)` when there are not enough |
| 2090 |
/// remaining bytes to read the value. |
| 2091 |
/// |
| 2092 |
/// # Examples |
| 2093 |
/// |
| 2094 |
/// ``` |
| 2095 |
/// use bytes::Buf; |
| 2096 |
/// |
| 2097 |
/// let mut buf = &b"\x03\x02\x01 hello"[..]; |
| 2098 |
/// assert_eq!(Ok(0x010203_i64), buf.try_get_int_le(3)); |
| 2099 |
/// assert_eq!(6, buf.remaining()); |
| 2100 |
/// ``` |
| 2101 |
/// |
| 2102 |
/// ``` |
| 2103 |
/// use bytes::{Buf, TryGetError}; |
| 2104 |
/// |
| 2105 |
/// let mut buf = &b"\x01\x02\x03"[..]; |
| 2106 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_int_le(4)); |
| 2107 |
/// assert_eq!(3, buf.remaining()); |
| 2108 |
/// ``` |
| 2109 |
/// |
| 2110 |
/// # Panics |
| 2111 |
/// |
| 2112 |
/// This function panics if `nbytes` is greater than 8. |
| 2113 |
fn try_get_int_le(&mut self, nbytes: usize) -> Result<i64, TryGetError> { |
| 2114 |
buf_try_get_impl!(le => self, i64, nbytes); |
| 2115 |
} |
| 2116 |
|
| 2117 |
/// Gets a signed n-byte integer from `self` in native-endian byte order. |
| 2118 |
/// |
| 2119 |
/// The current position is advanced by `nbytes`. |
| 2120 |
/// |
| 2121 |
/// Returns `Err(TryGetError)` when there are not enough |
| 2122 |
/// remaining bytes to read the value. |
| 2123 |
/// |
| 2124 |
/// # Examples |
| 2125 |
/// |
| 2126 |
/// ``` |
| 2127 |
/// use bytes::Buf; |
| 2128 |
/// |
| 2129 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 2130 |
/// true => b"\x01\x02\x03 hello", |
| 2131 |
/// false => b"\x03\x02\x01 hello", |
| 2132 |
/// }; |
| 2133 |
/// assert_eq!(Ok(0x010203_i64), buf.try_get_int_ne(3)); |
| 2134 |
/// assert_eq!(6, buf.remaining()); |
| 2135 |
/// ``` |
| 2136 |
/// |
| 2137 |
/// ``` |
| 2138 |
/// use bytes::{Buf, TryGetError}; |
| 2139 |
/// |
| 2140 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 2141 |
/// true => b"\x01\x02\x03", |
| 2142 |
/// false => b"\x03\x02\x01", |
| 2143 |
/// }; |
| 2144 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_int_ne(4)); |
| 2145 |
/// assert_eq!(3, buf.remaining()); |
| 2146 |
/// ``` |
| 2147 |
/// |
| 2148 |
/// # Panics |
| 2149 |
/// |
| 2150 |
/// This function panics if `nbytes` is greater than 8. |
| 2151 |
fn try_get_int_ne(&mut self, nbytes: usize) -> Result<i64, TryGetError> { |
| 2152 |
if cfg!(target_endian = "big") { |
| 2153 |
self.try_get_int(nbytes) |
| 2154 |
} else { |
| 2155 |
self.try_get_int_le(nbytes) |
| 2156 |
} |
| 2157 |
} |
| 2158 |
|
| 2159 |
/// Gets an IEEE754 single-precision (4 bytes) floating point number from |
| 2160 |
/// `self` in big-endian byte order. |
| 2161 |
/// |
| 2162 |
/// The current position is advanced by 4. |
| 2163 |
/// |
| 2164 |
/// Returns `Err(TryGetError)` when there are not enough |
| 2165 |
/// remaining bytes to read the value. |
| 2166 |
/// |
| 2167 |
/// # Examples |
| 2168 |
/// |
| 2169 |
/// ``` |
| 2170 |
/// use bytes::Buf; |
| 2171 |
/// |
| 2172 |
/// let mut buf = &b"\x3F\x99\x99\x9A hello"[..]; |
| 2173 |
/// assert_eq!(1.2f32, buf.get_f32()); |
| 2174 |
/// assert_eq!(6, buf.remaining()); |
| 2175 |
/// ``` |
| 2176 |
/// |
| 2177 |
/// ``` |
| 2178 |
/// use bytes::{Buf, TryGetError}; |
| 2179 |
/// |
| 2180 |
/// let mut buf = &b"\x3F\x99\x99"[..]; |
| 2181 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_f32()); |
| 2182 |
/// assert_eq!(3, buf.remaining()); |
| 2183 |
/// ``` |
| 2184 |
fn try_get_f32(&mut self) -> Result<f32, TryGetError> { |
| 2185 |
Ok(f32::from_bits(self.try_get_u32()?)) |
| 2186 |
} |
| 2187 |
|
| 2188 |
/// Gets an IEEE754 single-precision (4 bytes) floating point number from |
| 2189 |
/// `self` in little-endian byte order. |
| 2190 |
/// |
| 2191 |
/// The current position is advanced by 4. |
| 2192 |
/// |
| 2193 |
/// Returns `Err(TryGetError)` when there are not enough |
| 2194 |
/// remaining bytes to read the value. |
| 2195 |
/// |
| 2196 |
/// # Examples |
| 2197 |
/// |
| 2198 |
/// ``` |
| 2199 |
/// use bytes::Buf; |
| 2200 |
/// |
| 2201 |
/// let mut buf = &b"\x9A\x99\x99\x3F hello"[..]; |
| 2202 |
/// assert_eq!(1.2f32, buf.get_f32_le()); |
| 2203 |
/// assert_eq!(6, buf.remaining()); |
| 2204 |
/// ``` |
| 2205 |
/// |
| 2206 |
/// ``` |
| 2207 |
/// use bytes::{Buf, TryGetError}; |
| 2208 |
/// |
| 2209 |
/// let mut buf = &b"\x3F\x99\x99"[..]; |
| 2210 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_f32_le()); |
| 2211 |
/// assert_eq!(3, buf.remaining()); |
| 2212 |
/// ``` |
| 2213 |
fn try_get_f32_le(&mut self) -> Result<f32, TryGetError> { |
| 2214 |
Ok(f32::from_bits(self.try_get_u32_le()?)) |
| 2215 |
} |
| 2216 |
|
| 2217 |
/// Gets an IEEE754 single-precision (4 bytes) floating point number from |
| 2218 |
/// `self` in native-endian byte order. |
| 2219 |
/// |
| 2220 |
/// The current position is advanced by 4. |
| 2221 |
/// |
| 2222 |
/// Returns `Err(TryGetError)` when there are not enough |
| 2223 |
/// remaining bytes to read the value. |
| 2224 |
/// |
| 2225 |
/// # Examples |
| 2226 |
/// |
| 2227 |
/// ``` |
| 2228 |
/// use bytes::Buf; |
| 2229 |
/// |
| 2230 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 2231 |
/// true => b"\x3F\x99\x99\x9A hello", |
| 2232 |
/// false => b"\x9A\x99\x99\x3F hello", |
| 2233 |
/// }; |
| 2234 |
/// assert_eq!(1.2f32, buf.get_f32_ne()); |
| 2235 |
/// assert_eq!(6, buf.remaining()); |
| 2236 |
/// ``` |
| 2237 |
/// |
| 2238 |
/// ``` |
| 2239 |
/// use bytes::{Buf, TryGetError}; |
| 2240 |
/// |
| 2241 |
/// let mut buf = &b"\x3F\x99\x99"[..]; |
| 2242 |
/// assert_eq!(Err(TryGetError{requested: 4, available: 3}), buf.try_get_f32_ne()); |
| 2243 |
/// assert_eq!(3, buf.remaining()); |
| 2244 |
/// ``` |
| 2245 |
fn try_get_f32_ne(&mut self) -> Result<f32, TryGetError> { |
| 2246 |
Ok(f32::from_bits(self.try_get_u32_ne()?)) |
| 2247 |
} |
| 2248 |
|
| 2249 |
/// Gets an IEEE754 double-precision (8 bytes) floating point number from |
| 2250 |
/// `self` in big-endian byte order. |
| 2251 |
/// |
| 2252 |
/// The current position is advanced by 8. |
| 2253 |
/// |
| 2254 |
/// Returns `Err(TryGetError)` when there are not enough |
| 2255 |
/// remaining bytes to read the value. |
| 2256 |
/// |
| 2257 |
/// # Examples |
| 2258 |
/// |
| 2259 |
/// ``` |
| 2260 |
/// use bytes::Buf; |
| 2261 |
/// |
| 2262 |
/// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello"[..]; |
| 2263 |
/// assert_eq!(1.2f64, buf.get_f64()); |
| 2264 |
/// assert_eq!(6, buf.remaining()); |
| 2265 |
/// ``` |
| 2266 |
/// |
| 2267 |
/// ``` |
| 2268 |
/// use bytes::{Buf, TryGetError}; |
| 2269 |
/// |
| 2270 |
/// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; |
| 2271 |
/// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_f64()); |
| 2272 |
/// assert_eq!(7, buf.remaining()); |
| 2273 |
/// ``` |
| 2274 |
fn try_get_f64(&mut self) -> Result<f64, TryGetError> { |
| 2275 |
Ok(f64::from_bits(self.try_get_u64()?)) |
| 2276 |
} |
| 2277 |
|
| 2278 |
/// Gets an IEEE754 double-precision (8 bytes) floating point number from |
| 2279 |
/// `self` in little-endian byte order. |
| 2280 |
/// |
| 2281 |
/// The current position is advanced by 8. |
| 2282 |
/// |
| 2283 |
/// Returns `Err(TryGetError)` when there are not enough |
| 2284 |
/// remaining bytes to read the value. |
| 2285 |
/// |
| 2286 |
/// # Examples |
| 2287 |
/// |
| 2288 |
/// ``` |
| 2289 |
/// use bytes::Buf; |
| 2290 |
/// |
| 2291 |
/// let mut buf = &b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello"[..]; |
| 2292 |
/// assert_eq!(1.2f64, buf.get_f64_le()); |
| 2293 |
/// assert_eq!(6, buf.remaining()); |
| 2294 |
/// ``` |
| 2295 |
/// |
| 2296 |
/// ``` |
| 2297 |
/// use bytes::{Buf, TryGetError}; |
| 2298 |
/// |
| 2299 |
/// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; |
| 2300 |
/// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_f64_le()); |
| 2301 |
/// assert_eq!(7, buf.remaining()); |
| 2302 |
/// ``` |
| 2303 |
fn try_get_f64_le(&mut self) -> Result<f64, TryGetError> { |
| 2304 |
Ok(f64::from_bits(self.try_get_u64_le()?)) |
| 2305 |
} |
| 2306 |
|
| 2307 |
/// Gets an IEEE754 double-precision (8 bytes) floating point number from |
| 2308 |
/// `self` in native-endian byte order. |
| 2309 |
/// |
| 2310 |
/// The current position is advanced by 8. |
| 2311 |
/// |
| 2312 |
/// Returns `Err(TryGetError)` when there are not enough |
| 2313 |
/// remaining bytes to read the value. |
| 2314 |
/// |
| 2315 |
/// # Examples |
| 2316 |
/// |
| 2317 |
/// ``` |
| 2318 |
/// use bytes::Buf; |
| 2319 |
/// |
| 2320 |
/// let mut buf: &[u8] = match cfg!(target_endian = "big") { |
| 2321 |
/// true => b"\x3F\xF3\x33\x33\x33\x33\x33\x33 hello", |
| 2322 |
/// false => b"\x33\x33\x33\x33\x33\x33\xF3\x3F hello", |
| 2323 |
/// }; |
| 2324 |
/// assert_eq!(1.2f64, buf.get_f64_ne()); |
| 2325 |
/// assert_eq!(6, buf.remaining()); |
| 2326 |
/// ``` |
| 2327 |
/// |
| 2328 |
/// ``` |
| 2329 |
/// use bytes::{Buf, TryGetError}; |
| 2330 |
/// |
| 2331 |
/// let mut buf = &b"\x3F\xF3\x33\x33\x33\x33\x33"[..]; |
| 2332 |
/// assert_eq!(Err(TryGetError{requested: 8, available: 7}), buf.try_get_f64_ne()); |
| 2333 |
/// assert_eq!(7, buf.remaining()); |
| 2334 |
/// ``` |
| 2335 |
fn try_get_f64_ne(&mut self) -> Result<f64, TryGetError> { |
| 2336 |
Ok(f64::from_bits(self.try_get_u64_ne()?)) |
| 2337 |
} |
| 2338 |
|
| 2339 |
/// Consumes `len` bytes inside self and returns new instance of `Bytes` |
| 2340 |
/// with this data. |
| 2341 |
/// |
| 2342 |
/// This function may be optimized by the underlying type to avoid actual |
| 2343 |
/// copies. For example, `Bytes` implementation will do a shallow copy |
| 2344 |
/// (ref-count increment). |
| 2345 |
/// |
| 2346 |
/// # Examples |
| 2347 |
/// |
| 2348 |
/// ``` |
| 2349 |
/// use bytes::Buf; |
| 2350 |
/// |
| 2351 |
/// let bytes = (&b"hello world"[..]).copy_to_bytes(5); |
| 2352 |
/// assert_eq!(&bytes[..], &b"hello"[..]); |
| 2353 |
/// ``` |
| 2354 |
/// |
| 2355 |
/// # Panics |
| 2356 |
/// |
| 2357 |
/// This function panics if `len > self.remaining()`. |
| 2358 |
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { |
| 2359 |
use super::BufMut; |
| 2360 |
|
| 2361 |
if self.remaining() < len { |
| 2362 |
panic_advance(&TryGetError { |
| 2363 |
requested: len, |
| 2364 |
available: self.remaining(), |
| 2365 |
}); |
| 2366 |
} |
| 2367 |
|
| 2368 |
let mut ret = crate::BytesMut::with_capacity(len); |
| 2369 |
ret.put(self.take(len)); |
| 2370 |
ret.freeze() |
| 2371 |
} |
| 2372 |
|
| 2373 |
/// Creates an adaptor which will read at most `limit` bytes from `self`. |
| 2374 |
/// |
| 2375 |
/// This function returns a new instance of `Buf` which will read at most |
| 2376 |
/// `limit` bytes. |
| 2377 |
/// |
| 2378 |
/// # Examples |
| 2379 |
/// |
| 2380 |
/// ``` |
| 2381 |
/// use bytes::{Buf, BufMut}; |
| 2382 |
/// |
| 2383 |
/// let mut buf = b"hello world"[..].take(5); |
| 2384 |
/// let mut dst = vec![]; |
| 2385 |
/// |
| 2386 |
/// dst.put(&mut buf); |
| 2387 |
/// assert_eq!(dst, b"hello"); |
| 2388 |
/// |
| 2389 |
/// let mut buf = buf.into_inner(); |
| 2390 |
/// dst.clear(); |
| 2391 |
/// dst.put(&mut buf); |
| 2392 |
/// assert_eq!(dst, b" world"); |
| 2393 |
/// ``` |
| 2394 |
fn take(self, limit: usize) -> Take<Self> |
| 2395 |
where |
| 2396 |
Self: Sized, |
| 2397 |
{ |
| 2398 |
take::new(self, limit) |
| 2399 |
} |
| 2400 |
|
| 2401 |
/// Creates an adaptor which will chain this buffer with another. |
| 2402 |
/// |
| 2403 |
/// The returned `Buf` instance will first consume all bytes from `self`. |
| 2404 |
/// Afterwards the output is equivalent to the output of next. |
| 2405 |
/// |
| 2406 |
/// # Examples |
| 2407 |
/// |
| 2408 |
/// ``` |
| 2409 |
/// use bytes::Buf; |
| 2410 |
/// |
| 2411 |
/// let mut chain = b"hello "[..].chain(&b"world"[..]); |
| 2412 |
/// |
| 2413 |
/// let full = chain.copy_to_bytes(11); |
| 2414 |
/// assert_eq!(full.chunk(), b"hello world"); |
| 2415 |
/// ``` |
| 2416 |
fn chain<U: Buf>(self, next: U) -> Chain<Self, U> |
| 2417 |
where |
| 2418 |
Self: Sized, |
| 2419 |
{ |
| 2420 |
Chain::new(self, next) |
| 2421 |
} |
| 2422 |
|
| 2423 |
/// Creates an adaptor which implements the `Read` trait for `self`. |
| 2424 |
/// |
| 2425 |
/// This function returns a new value which implements `Read` by adapting |
| 2426 |
/// the `Read` trait functions to the `Buf` trait functions. Given that |
| 2427 |
/// `Buf` operations are infallible, none of the `Read` functions will |
| 2428 |
/// return with `Err`. |
| 2429 |
/// |
| 2430 |
/// # Examples |
| 2431 |
/// |
| 2432 |
/// ``` |
| 2433 |
/// use bytes::{Bytes, Buf}; |
| 2434 |
/// use std::io::Read; |
| 2435 |
/// |
| 2436 |
/// let buf = Bytes::from("hello world"); |
| 2437 |
/// |
| 2438 |
/// let mut reader = buf.reader(); |
| 2439 |
/// let mut dst = [0; 1024]; |
| 2440 |
/// |
| 2441 |
/// let num = reader.read(&mut dst).unwrap(); |
| 2442 |
/// |
| 2443 |
/// assert_eq!(11, num); |
| 2444 |
/// assert_eq!(&dst[..11], &b"hello world"[..]); |
| 2445 |
/// ``` |
| 2446 |
#[cfg(feature = "std")] |
| 2447 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 2448 |
fn reader(self) -> Reader<Self> |
| 2449 |
where |
| 2450 |
Self: Sized, |
| 2451 |
{ |
| 2452 |
reader::new(self) |
| 2453 |
} |
| 2454 |
} |
| 2455 |
|
| 2456 |
macro_rules! deref_forward_buf { |
| 2457 |
() => { |
| 2458 |
#[inline] |
| 2459 |
fn remaining(&self) -> usize { |
| 2460 |
(**self).remaining() |
| 2461 |
} |
| 2462 |
|
| 2463 |
#[inline] |
| 2464 |
fn chunk(&self) -> &[u8] { |
| 2465 |
(**self).chunk() |
| 2466 |
} |
| 2467 |
|
| 2468 |
#[cfg(feature = "std")] |
| 2469 |
#[inline] |
| 2470 |
fn chunks_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize { |
| 2471 |
(**self).chunks_vectored(dst) |
| 2472 |
} |
| 2473 |
|
| 2474 |
#[inline] |
| 2475 |
fn advance(&mut self, cnt: usize) { |
| 2476 |
(**self).advance(cnt) |
| 2477 |
} |
| 2478 |
|
| 2479 |
#[inline] |
| 2480 |
fn has_remaining(&self) -> bool { |
| 2481 |
(**self).has_remaining() |
| 2482 |
} |
| 2483 |
|
| 2484 |
#[inline] |
| 2485 |
fn copy_to_slice(&mut self, dst: &mut [u8]) { |
| 2486 |
(**self).copy_to_slice(dst) |
| 2487 |
} |
| 2488 |
|
| 2489 |
#[inline] |
| 2490 |
fn get_u8(&mut self) -> u8 { |
| 2491 |
(**self).get_u8() |
| 2492 |
} |
| 2493 |
|
| 2494 |
#[inline] |
| 2495 |
fn get_i8(&mut self) -> i8 { |
| 2496 |
(**self).get_i8() |
| 2497 |
} |
| 2498 |
|
| 2499 |
#[inline] |
| 2500 |
fn get_u16(&mut self) -> u16 { |
| 2501 |
(**self).get_u16() |
| 2502 |
} |
| 2503 |
|
| 2504 |
#[inline] |
| 2505 |
fn get_u16_le(&mut self) -> u16 { |
| 2506 |
(**self).get_u16_le() |
| 2507 |
} |
| 2508 |
|
| 2509 |
#[inline] |
| 2510 |
fn get_u16_ne(&mut self) -> u16 { |
| 2511 |
(**self).get_u16_ne() |
| 2512 |
} |
| 2513 |
|
| 2514 |
#[inline] |
| 2515 |
fn get_i16(&mut self) -> i16 { |
| 2516 |
(**self).get_i16() |
| 2517 |
} |
| 2518 |
|
| 2519 |
#[inline] |
| 2520 |
fn get_i16_le(&mut self) -> i16 { |
| 2521 |
(**self).get_i16_le() |
| 2522 |
} |
| 2523 |
|
| 2524 |
#[inline] |
| 2525 |
fn get_i16_ne(&mut self) -> i16 { |
| 2526 |
(**self).get_i16_ne() |
| 2527 |
} |
| 2528 |
|
| 2529 |
#[inline] |
| 2530 |
fn get_u32(&mut self) -> u32 { |
| 2531 |
(**self).get_u32() |
| 2532 |
} |
| 2533 |
|
| 2534 |
#[inline] |
| 2535 |
fn get_u32_le(&mut self) -> u32 { |
| 2536 |
(**self).get_u32_le() |
| 2537 |
} |
| 2538 |
|
| 2539 |
#[inline] |
| 2540 |
fn get_u32_ne(&mut self) -> u32 { |
| 2541 |
(**self).get_u32_ne() |
| 2542 |
} |
| 2543 |
|
| 2544 |
#[inline] |
| 2545 |
fn get_i32(&mut self) -> i32 { |
| 2546 |
(**self).get_i32() |
| 2547 |
} |
| 2548 |
|
| 2549 |
#[inline] |
| 2550 |
fn get_i32_le(&mut self) -> i32 { |
| 2551 |
(**self).get_i32_le() |
| 2552 |
} |
| 2553 |
|
| 2554 |
#[inline] |
| 2555 |
fn get_i32_ne(&mut self) -> i32 { |
| 2556 |
(**self).get_i32_ne() |
| 2557 |
} |
| 2558 |
|
| 2559 |
#[inline] |
| 2560 |
fn get_u64(&mut self) -> u64 { |
| 2561 |
(**self).get_u64() |
| 2562 |
} |
| 2563 |
|
| 2564 |
#[inline] |
| 2565 |
fn get_u64_le(&mut self) -> u64 { |
| 2566 |
(**self).get_u64_le() |
| 2567 |
} |
| 2568 |
|
| 2569 |
#[inline] |
| 2570 |
fn get_u64_ne(&mut self) -> u64 { |
| 2571 |
(**self).get_u64_ne() |
| 2572 |
} |
| 2573 |
|
| 2574 |
#[inline] |
| 2575 |
fn get_i64(&mut self) -> i64 { |
| 2576 |
(**self).get_i64() |
| 2577 |
} |
| 2578 |
|
| 2579 |
#[inline] |
| 2580 |
fn get_i64_le(&mut self) -> i64 { |
| 2581 |
(**self).get_i64_le() |
| 2582 |
} |
| 2583 |
|
| 2584 |
#[inline] |
| 2585 |
fn get_i64_ne(&mut self) -> i64 { |
| 2586 |
(**self).get_i64_ne() |
| 2587 |
} |
| 2588 |
|
| 2589 |
#[inline] |
| 2590 |
fn get_u128(&mut self) -> u128 { |
| 2591 |
(**self).get_u128() |
| 2592 |
} |
| 2593 |
|
| 2594 |
#[inline] |
| 2595 |
fn get_u128_le(&mut self) -> u128 { |
| 2596 |
(**self).get_u128_le() |
| 2597 |
} |
| 2598 |
|
| 2599 |
#[inline] |
| 2600 |
fn get_u128_ne(&mut self) -> u128 { |
| 2601 |
(**self).get_u128_ne() |
| 2602 |
} |
| 2603 |
|
| 2604 |
#[inline] |
| 2605 |
fn get_i128(&mut self) -> i128 { |
| 2606 |
(**self).get_i128() |
| 2607 |
} |
| 2608 |
|
| 2609 |
#[inline] |
| 2610 |
fn get_i128_le(&mut self) -> i128 { |
| 2611 |
(**self).get_i128_le() |
| 2612 |
} |
| 2613 |
|
| 2614 |
#[inline] |
| 2615 |
fn get_i128_ne(&mut self) -> i128 { |
| 2616 |
(**self).get_i128_ne() |
| 2617 |
} |
| 2618 |
|
| 2619 |
#[inline] |
| 2620 |
fn get_uint(&mut self, nbytes: usize) -> u64 { |
| 2621 |
(**self).get_uint(nbytes) |
| 2622 |
} |
| 2623 |
|
| 2624 |
#[inline] |
| 2625 |
fn get_uint_le(&mut self, nbytes: usize) -> u64 { |
| 2626 |
(**self).get_uint_le(nbytes) |
| 2627 |
} |
| 2628 |
|
| 2629 |
#[inline] |
| 2630 |
fn get_uint_ne(&mut self, nbytes: usize) -> u64 { |
| 2631 |
(**self).get_uint_ne(nbytes) |
| 2632 |
} |
| 2633 |
|
| 2634 |
#[inline] |
| 2635 |
fn get_int(&mut self, nbytes: usize) -> i64 { |
| 2636 |
(**self).get_int(nbytes) |
| 2637 |
} |
| 2638 |
|
| 2639 |
#[inline] |
| 2640 |
fn get_int_le(&mut self, nbytes: usize) -> i64 { |
| 2641 |
(**self).get_int_le(nbytes) |
| 2642 |
} |
| 2643 |
|
| 2644 |
#[inline] |
| 2645 |
fn get_int_ne(&mut self, nbytes: usize) -> i64 { |
| 2646 |
(**self).get_int_ne(nbytes) |
| 2647 |
} |
| 2648 |
|
| 2649 |
#[inline] |
| 2650 |
fn get_f32(&mut self) -> f32 { |
| 2651 |
(**self).get_f32() |
| 2652 |
} |
| 2653 |
|
| 2654 |
#[inline] |
| 2655 |
fn get_f32_le(&mut self) -> f32 { |
| 2656 |
(**self).get_f32_le() |
| 2657 |
} |
| 2658 |
|
| 2659 |
#[inline] |
| 2660 |
fn get_f32_ne(&mut self) -> f32 { |
| 2661 |
(**self).get_f32_ne() |
| 2662 |
} |
| 2663 |
|
| 2664 |
#[inline] |
| 2665 |
fn get_f64(&mut self) -> f64 { |
| 2666 |
(**self).get_f64() |
| 2667 |
} |
| 2668 |
|
| 2669 |
#[inline] |
| 2670 |
fn get_f64_le(&mut self) -> f64 { |
| 2671 |
(**self).get_f64_le() |
| 2672 |
} |
| 2673 |
|
| 2674 |
#[inline] |
| 2675 |
fn get_f64_ne(&mut self) -> f64 { |
| 2676 |
(**self).get_f64_ne() |
| 2677 |
} |
| 2678 |
|
| 2679 |
#[inline] |
| 2680 |
fn try_copy_to_slice(&mut self, dst: &mut [u8]) -> Result<(), TryGetError> { |
| 2681 |
(**self).try_copy_to_slice(dst) |
| 2682 |
} |
| 2683 |
|
| 2684 |
#[inline] |
| 2685 |
fn try_get_u8(&mut self) -> Result<u8, TryGetError> { |
| 2686 |
(**self).try_get_u8() |
| 2687 |
} |
| 2688 |
|
| 2689 |
#[inline] |
| 2690 |
fn try_get_i8(&mut self) -> Result<i8, TryGetError> { |
| 2691 |
(**self).try_get_i8() |
| 2692 |
} |
| 2693 |
|
| 2694 |
#[inline] |
| 2695 |
fn try_get_u16(&mut self) -> Result<u16, TryGetError> { |
| 2696 |
(**self).try_get_u16() |
| 2697 |
} |
| 2698 |
|
| 2699 |
#[inline] |
| 2700 |
fn try_get_u16_le(&mut self) -> Result<u16, TryGetError> { |
| 2701 |
(**self).try_get_u16_le() |
| 2702 |
} |
| 2703 |
|
| 2704 |
#[inline] |
| 2705 |
fn try_get_u16_ne(&mut self) -> Result<u16, TryGetError> { |
| 2706 |
(**self).try_get_u16_ne() |
| 2707 |
} |
| 2708 |
|
| 2709 |
#[inline] |
| 2710 |
fn try_get_i16(&mut self) -> Result<i16, TryGetError> { |
| 2711 |
(**self).try_get_i16() |
| 2712 |
} |
| 2713 |
|
| 2714 |
#[inline] |
| 2715 |
fn try_get_i16_le(&mut self) -> Result<i16, TryGetError> { |
| 2716 |
(**self).try_get_i16_le() |
| 2717 |
} |
| 2718 |
|
| 2719 |
#[inline] |
| 2720 |
fn try_get_i16_ne(&mut self) -> Result<i16, TryGetError> { |
| 2721 |
(**self).try_get_i16_ne() |
| 2722 |
} |
| 2723 |
|
| 2724 |
#[inline] |
| 2725 |
fn try_get_u32(&mut self) -> Result<u32, TryGetError> { |
| 2726 |
(**self).try_get_u32() |
| 2727 |
} |
| 2728 |
|
| 2729 |
#[inline] |
| 2730 |
fn try_get_u32_le(&mut self) -> Result<u32, TryGetError> { |
| 2731 |
(**self).try_get_u32_le() |
| 2732 |
} |
| 2733 |
|
| 2734 |
#[inline] |
| 2735 |
fn try_get_u32_ne(&mut self) -> Result<u32, TryGetError> { |
| 2736 |
(**self).try_get_u32_ne() |
| 2737 |
} |
| 2738 |
|
| 2739 |
#[inline] |
| 2740 |
fn try_get_i32(&mut self) -> Result<i32, TryGetError> { |
| 2741 |
(**self).try_get_i32() |
| 2742 |
} |
| 2743 |
|
| 2744 |
#[inline] |
| 2745 |
fn try_get_i32_le(&mut self) -> Result<i32, TryGetError> { |
| 2746 |
(**self).try_get_i32_le() |
| 2747 |
} |
| 2748 |
|
| 2749 |
#[inline] |
| 2750 |
fn try_get_i32_ne(&mut self) -> Result<i32, TryGetError> { |
| 2751 |
(**self).try_get_i32_ne() |
| 2752 |
} |
| 2753 |
|
| 2754 |
#[inline] |
| 2755 |
fn try_get_u64(&mut self) -> Result<u64, TryGetError> { |
| 2756 |
(**self).try_get_u64() |
| 2757 |
} |
| 2758 |
|
| 2759 |
#[inline] |
| 2760 |
fn try_get_u64_le(&mut self) -> Result<u64, TryGetError> { |
| 2761 |
(**self).try_get_u64_le() |
| 2762 |
} |
| 2763 |
|
| 2764 |
#[inline] |
| 2765 |
fn try_get_u64_ne(&mut self) -> Result<u64, TryGetError> { |
| 2766 |
(**self).try_get_u64_ne() |
| 2767 |
} |
| 2768 |
|
| 2769 |
#[inline] |
| 2770 |
fn try_get_i64(&mut self) -> Result<i64, TryGetError> { |
| 2771 |
(**self).try_get_i64() |
| 2772 |
} |
| 2773 |
|
| 2774 |
#[inline] |
| 2775 |
fn try_get_i64_le(&mut self) -> Result<i64, TryGetError> { |
| 2776 |
(**self).try_get_i64_le() |
| 2777 |
} |
| 2778 |
|
| 2779 |
#[inline] |
| 2780 |
fn try_get_i64_ne(&mut self) -> Result<i64, TryGetError> { |
| 2781 |
(**self).try_get_i64_ne() |
| 2782 |
} |
| 2783 |
|
| 2784 |
#[inline] |
| 2785 |
fn try_get_u128(&mut self) -> Result<u128, TryGetError> { |
| 2786 |
(**self).try_get_u128() |
| 2787 |
} |
| 2788 |
|
| 2789 |
#[inline] |
| 2790 |
fn try_get_u128_le(&mut self) -> Result<u128, TryGetError> { |
| 2791 |
(**self).try_get_u128_le() |
| 2792 |
} |
| 2793 |
|
| 2794 |
#[inline] |
| 2795 |
fn try_get_u128_ne(&mut self) -> Result<u128, TryGetError> { |
| 2796 |
(**self).try_get_u128_ne() |
| 2797 |
} |
| 2798 |
|
| 2799 |
#[inline] |
| 2800 |
fn try_get_i128(&mut self) -> Result<i128, TryGetError> { |
| 2801 |
(**self).try_get_i128() |
| 2802 |
} |
| 2803 |
|
| 2804 |
#[inline] |
| 2805 |
fn try_get_i128_le(&mut self) -> Result<i128, TryGetError> { |
| 2806 |
(**self).try_get_i128_le() |
| 2807 |
} |
| 2808 |
|
| 2809 |
#[inline] |
| 2810 |
fn try_get_i128_ne(&mut self) -> Result<i128, TryGetError> { |
| 2811 |
(**self).try_get_i128_ne() |
| 2812 |
} |
| 2813 |
|
| 2814 |
#[inline] |
| 2815 |
fn try_get_uint(&mut self, nbytes: usize) -> Result<u64, TryGetError> { |
| 2816 |
(**self).try_get_uint(nbytes) |
| 2817 |
} |
| 2818 |
|
| 2819 |
#[inline] |
| 2820 |
fn try_get_uint_le(&mut self, nbytes: usize) -> Result<u64, TryGetError> { |
| 2821 |
(**self).try_get_uint_le(nbytes) |
| 2822 |
} |
| 2823 |
|
| 2824 |
#[inline] |
| 2825 |
fn try_get_uint_ne(&mut self, nbytes: usize) -> Result<u64, TryGetError> { |
| 2826 |
(**self).try_get_uint_ne(nbytes) |
| 2827 |
} |
| 2828 |
|
| 2829 |
#[inline] |
| 2830 |
fn try_get_int(&mut self, nbytes: usize) -> Result<i64, TryGetError> { |
| 2831 |
(**self).try_get_int(nbytes) |
| 2832 |
} |
| 2833 |
|
| 2834 |
#[inline] |
| 2835 |
fn try_get_int_le(&mut self, nbytes: usize) -> Result<i64, TryGetError> { |
| 2836 |
(**self).try_get_int_le(nbytes) |
| 2837 |
} |
| 2838 |
|
| 2839 |
#[inline] |
| 2840 |
fn try_get_int_ne(&mut self, nbytes: usize) -> Result<i64, TryGetError> { |
| 2841 |
(**self).try_get_int_ne(nbytes) |
| 2842 |
} |
| 2843 |
|
| 2844 |
#[inline] |
| 2845 |
fn try_get_f32(&mut self) -> Result<f32, TryGetError> { |
| 2846 |
(**self).try_get_f32() |
| 2847 |
} |
| 2848 |
|
| 2849 |
#[inline] |
| 2850 |
fn try_get_f32_le(&mut self) -> Result<f32, TryGetError> { |
| 2851 |
(**self).try_get_f32_le() |
| 2852 |
} |
| 2853 |
|
| 2854 |
#[inline] |
| 2855 |
fn try_get_f32_ne(&mut self) -> Result<f32, TryGetError> { |
| 2856 |
(**self).try_get_f32_ne() |
| 2857 |
} |
| 2858 |
|
| 2859 |
#[inline] |
| 2860 |
fn try_get_f64(&mut self) -> Result<f64, TryGetError> { |
| 2861 |
(**self).try_get_f64() |
| 2862 |
} |
| 2863 |
|
| 2864 |
#[inline] |
| 2865 |
fn try_get_f64_le(&mut self) -> Result<f64, TryGetError> { |
| 2866 |
(**self).try_get_f64_le() |
| 2867 |
} |
| 2868 |
|
| 2869 |
#[inline] |
| 2870 |
fn try_get_f64_ne(&mut self) -> Result<f64, TryGetError> { |
| 2871 |
(**self).try_get_f64_ne() |
| 2872 |
} |
| 2873 |
|
| 2874 |
#[inline] |
| 2875 |
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { |
| 2876 |
(**self).copy_to_bytes(len) |
| 2877 |
} |
| 2878 |
}; |
| 2879 |
} |
| 2880 |
|
| 2881 |
impl<T: Buf + ?Sized> Buf for &mut T { |
| 2882 |
deref_forward_buf!(); |
| 2883 |
} |
| 2884 |
|
| 2885 |
impl<T: Buf + ?Sized> Buf for Box<T> { |
| 2886 |
deref_forward_buf!(); |
| 2887 |
} |
| 2888 |
|
| 2889 |
impl Buf for &[u8] { |
| 2890 |
#[inline] |
| 2891 |
fn remaining(&self) -> usize { |
| 2892 |
self.len() |
| 2893 |
} |
| 2894 |
|
| 2895 |
#[inline] |
| 2896 |
fn chunk(&self) -> &[u8] { |
| 2897 |
self |
| 2898 |
} |
| 2899 |
|
| 2900 |
#[inline] |
| 2901 |
fn advance(&mut self, cnt: usize) { |
| 2902 |
if self.len() < cnt { |
| 2903 |
panic_advance(&TryGetError { |
| 2904 |
requested: cnt, |
| 2905 |
available: self.len(), |
| 2906 |
}); |
| 2907 |
} |
| 2908 |
|
| 2909 |
*self = &self[cnt..]; |
| 2910 |
} |
| 2911 |
|
| 2912 |
#[inline] |
| 2913 |
fn copy_to_slice(&mut self, dst: &mut [u8]) { |
| 2914 |
if self.len() < dst.len() { |
| 2915 |
panic_advance(&TryGetError { |
| 2916 |
requested: dst.len(), |
| 2917 |
available: self.len(), |
| 2918 |
}); |
| 2919 |
} |
| 2920 |
|
| 2921 |
dst.copy_from_slice(&self[..dst.len()]); |
| 2922 |
self.advance(dst.len()); |
| 2923 |
} |
| 2924 |
} |
| 2925 |
|
| 2926 |
#[cfg(feature = "std")] |
| 2927 |
impl<T: AsRef<[u8]>> Buf for std::io::Cursor<T> { |
| 2928 |
#[inline] |
| 2929 |
fn remaining(&self) -> usize { |
| 2930 |
saturating_sub_usize_u64(self.get_ref().as_ref().len(), self.position()) |
| 2931 |
} |
| 2932 |
|
| 2933 |
#[inline] |
| 2934 |
fn chunk(&self) -> &[u8] { |
| 2935 |
let slice = self.get_ref().as_ref(); |
| 2936 |
let pos = min_u64_usize(self.position(), slice.len()); |
| 2937 |
&slice[pos..] |
| 2938 |
} |
| 2939 |
|
| 2940 |
#[inline] |
| 2941 |
fn advance(&mut self, cnt: usize) { |
| 2942 |
let len = self.get_ref().as_ref().len(); |
| 2943 |
let pos = self.position(); |
| 2944 |
|
| 2945 |
// We intentionally allow `cnt == 0` here even if `pos > len`. |
| 2946 |
let max_cnt = saturating_sub_usize_u64(len, pos); |
| 2947 |
if cnt > max_cnt { |
| 2948 |
panic_advance(&TryGetError { |
| 2949 |
requested: cnt, |
| 2950 |
available: max_cnt, |
| 2951 |
}); |
| 2952 |
} |
| 2953 |
|
| 2954 |
// This will not overflow because either `cnt == 0` or the sum is not |
| 2955 |
// greater than `len`. |
| 2956 |
self.set_position(pos + cnt as u64); |
| 2957 |
} |
| 2958 |
} |
| 2959 |
|
| 2960 |
// The existence of this function makes the compiler catch if the Buf |
| 2961 |
// trait is "object-safe" or not. |
| 2962 |
fn _assert_trait_object(_b: &dyn Buf) {} |