Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a comparison of bytes::Bytes and std::io::Bytes/&[u8]`? #640

Closed
MaxG87 opened this issue Nov 28, 2023 · 2 comments
Closed

Is there a comparison of bytes::Bytes and std::io::Bytes/&[u8]`? #640

MaxG87 opened this issue Nov 28, 2023 · 2 comments

Comments

@MaxG87
Copy link

MaxG87 commented Nov 28, 2023

I am relatively new to Rust but already had to deal with in-memory buffers for binary data. I noticed that one could use &[u8] or Vec<u8> for that. However, I also noticed there is bytes::Bytes, which sounds promising as it bothered to give a name to that concept.

Unfortunately, using it was a bit cumbersome, as I had a hard time to to convince all involved modules to just accept Bytes. That might be totally due to me, as I am still novice, though. Anyhow, I wondered whether the fuzz is worth it.

Is there a resource explaining when one should opt to bytes::Bytes and when there are no benefits compared to Vec<u8>? It might be opinionated, but I would love to learn about when it would provide some benefits.

In case there is no such resource, I would like to suggest that a brief section could be added to the documentation.

@Darksonn
Copy link
Contributor

The primary feature of Bytes is that the .clone() operation is really cheap. A clone on a Bytes just increments a counter for how many clones there are, whereas cloning a Vec<u8> copies all of the data in the vector. It is comparable to an Arc<Vec<u8>>, except that Bytes supports slicing so that you only see part of the buffer.

@MaxG87
Copy link
Author

MaxG87 commented Nov 29, 2023

Thank you very much! This is good to know. With that I understand the description on https://docs.rs/bytes/ better.

@MaxG87 MaxG87 closed this as completed Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants