tokio-rs / bytes

Utilities for working with bytes
MIT License
1.88k stars 281 forks source link

Splice for BytesMut #706

Open Sytten opened 5 months ago

Sytten commented 5 months ago

In my question to move from Arc<Vec<u8>> to BytesMut, I am facing a couple of missing methods I had on Vec. One of them is splice (https://doc.rust-lang.org/std/vec/struct.Vec.html#method.splice). I am guessing this method could go in BufMut?

Darksonn commented 4 months ago

The Vec::splice method is complicated because it has to work with types that are not Copy. However, u8 is Copy, so it should not be too hard to emulate it with copy_within followed by truncate.

Sytten commented 4 months ago

The hard part really is when the destination range is smaller than the source range since it requires at least one reallocation (if the size hint lower bound proves to be enough).