You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Most of the From conversions to Bytes are shallow copies, just grabbing a pointer and pulling it in. There is also the from_static function to save copying a static set of bytes. However, the From<&'a [u8]> impl will copy the slice into a new buffer (either inline or a new heap allocation, depending on size).
Unfortunately, that &'a [u8] means it applies to &'static [u8] as well. In cases where one is generic over Into<Bytes>, a user can easily and accidentally cause copies of static slices, instead of remembering to call Bytes::from_static. This issue proposes to change the defaults, so that the faster way is easier.
Remove From<&'a [u8]> for Bytes/BytesMut.
Add From<&'static [u8]> for Bytes/BytesMut.
Add a new constructor that can take &'a [u8], that explicitly shows a copy will happen.
The name of this new method could be a few things, these just some examples that occurred to me:
Bytes::copy(slice)
Bytes::copy_from(slice)
The text was updated successfully, but these errors were encountered:
Most of the
From
conversions toBytes
are shallow copies, just grabbing a pointer and pulling it in. There is also thefrom_static
function to save copying a static set of bytes. However, theFrom<&'a [u8]>
impl will copy the slice into a new buffer (either inline or a new heap allocation, depending on size).Unfortunately, that
&'a [u8]
means it applies to&'static [u8]
as well. In cases where one is generic overInto<Bytes>
, a user can easily and accidentally cause copies of static slices, instead of remembering to callBytes::from_static
. This issue proposes to change the defaults, so that the faster way is easier.From<&'a [u8]>
forBytes
/BytesMut
.From<&'static [u8]>
forBytes
/BytesMut
.&'a [u8]
, that explicitly shows a copy will happen.The name of this new method could be a few things, these just some examples that occurred to me:
Bytes::copy(slice)
Bytes::copy_from(slice)
The text was updated successfully, but these errors were encountered: