mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-14 00:00:13 +02:00
BufMut::put_bytes(self, val, cnt) (#487)
Equivalent to
```
for _ in 0..cnt {
self.put_u8(val);
}
```
but may work faster.
Name and signature is chosen to be consistent with `ptr::write_bytes`.
Include three specializations:
* `Vec<u8>`
* `&mut [u8]`
* `BytesMut`
`BytesMut` and `&mut [u8]` specializations use `ptr::write`, `Vec<u8>`
specialization uses `Vec::resize`.
This commit is contained in:
@@ -1010,6 +1010,19 @@ unsafe impl BufMut for BytesMut {
|
||||
fn put_slice(&mut self, src: &[u8]) {
|
||||
self.extend_from_slice(src);
|
||||
}
|
||||
|
||||
fn put_bytes(&mut self, val: u8, cnt: usize) {
|
||||
self.reserve(cnt);
|
||||
unsafe {
|
||||
let dst = self.uninit_slice();
|
||||
// Reserved above
|
||||
debug_assert!(dst.len() >= cnt);
|
||||
|
||||
ptr::write_bytes(dst.as_mut_ptr(), val, cnt);
|
||||
|
||||
self.advance_mut(cnt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for BytesMut {
|
||||
|
||||
Reference in New Issue
Block a user