| 1 | use bytesrust-analyzer cargo bytes 1.12.1 crate/::{BufMutrust-analyzer cargo bytes 1.12.1 buf/buf_mut/BufMut#, BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#}; |
| 2 | |
| 3 | /// An extension for [`BufMut`] for getting a writeable buffer in safe code. |
| 4 | pub trait ReadBufrust-analyzer cargo sqlx-core 0.8.3 io/read_buf/ReadBuf#: BufMutrust-analyzer cargo bytes 1.12.1 buf/buf_mut/BufMut# { |
| 5 | /// Get the full capacity of this buffer as a safely initialized slice. |
| 6 | fn init_mutrust-analyzer cargo sqlx-core 0.8.3 io/read_buf/ReadBuf#init_mut().(&mut self) -> &mut [u8]; |
| 7 | } |
| 8 | |
| 9 | impl ReadBufrust-analyzer cargo sqlx-core 0.8.3 io/read_buf/ReadBuf# for &'_ mut [u8] { |
| 10 | #[inline(always)] |
| 11 | fn init_mutrust-analyzer cargo sqlx-core 0.8.3 io/read_buf/impl#[`&mut [u8]`][ReadBuf]init_mut().(&mut self) -> &mut [u8] { |
| 12 | self |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | impl ReadBufrust-analyzer cargo sqlx-core 0.8.3 io/read_buf/ReadBuf# for BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut# { |
| 17 | #[inline(always)] |
| 18 | fn init_mutrust-analyzer cargo sqlx-core 0.8.3 io/read_buf/impl#[BytesMut][ReadBuf]init_mut().(&mut self) -> &mut [u8] { |
| 19 | // `self.remaining_mut()` returns `usize::MAX - self.len()` |
| 20 | let remaining = self.capacityrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]capacity().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().self.lenrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]len().(); |
| 21 | |
| 22 | // I'm hoping for most uses that this operation is elided by the optimizer. |
| 23 | self.put_bytesrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][BufMut]put_bytes().(0, remaining); |
| 24 | |
| 25 | self |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 30 | fn test_read_buf_bytes_mutrust-analyzer cargo sqlx-core 0.8.3 io/read_buf/test_read_buf_bytes_mut().() { |
| 31 | let mut buf = BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#::with_capacityrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]with_capacity().(8); |
| 32 | buf.put_u32rust-analyzer cargo bytes 1.12.1 buf/buf_mut/BufMut#put_u32().(0x12345678); |
| 33 | |
| 34 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf.init_mutrust-analyzer cargo sqlx-core 0.8.3 io/read_buf/impl#[BytesMut][ReadBuf]init_mut().(), [0x12, 0x34, 0x56, 0x78, 0, 0, 0, 0]); |
| 35 | } |