copy_to_bytes() 在链上的表现如何?

How does copy_to_bytes() behave on Chain?

copy_to_bytes() 的文档说:

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).

我想知道 copy_to_bytes() 应用于 Chain 时实际表现如何?它会同时复制 Bytes 还是只复制一个?还是只是增加了引用计数?

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());
}

Playground

我也问过这个问题 issue on tokio-rs 但没有得到答案。

它们只是被视为 附加。 正如您从 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).

所以,是的,值被复制到字节缓冲区中。

Playground