The documentation of copy_to_bytes()
says:
Consumes
len
bytes insideself
and returns new instance of Bytes with this data. This function may be optimized by the underlying type to avoid actual copies. For example, Bytes implementation will do a shallow copy (ref-count increment).
I wonder how copy_to_bytes()
actually behaves when being applied on Chain
? Does it copy both Bytes
, or just one? Or does it just increase the ref-count?
use bytes::*;
fn main() {
let slice1 = &[1,2,3];
let slice2 = &[4,5,6];
let a = Bytes::from_static(slice1);
let b = Bytes::from_static(slice2);
let chained = a.chain(b).copy_to_bytes(slice1.len() + slice2.len());
}
I also asked this question as an issue on tokio-rs but obtained no answer.
They are just treated as if they were appended. As you pointed from the documentation:
Consumes len bytes inside self and returns new instance of Bytes with this data.
This function may be optimized by the underlying type to avoid actual copies. For example, Bytes implementation will do a shallow copy (ref-count increment).
So, yes, the values are copied into the bytes buffer.