-
Notifications
You must be signed in to change notification settings - Fork 115
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 way to create a BitVec
from an u8
?
#6
Comments
Hi! Thanks for your issue; I really appreciate feedback from people using this in their work. At present, I do not have any methods to create a collection from a single scalar. I do provide methods to convert Rust collections into the collections of this crate, such as: let source: u8 = 0b1010_0101;
let bs: &BitSlice = (&[source] as &[u8]).into();
let bv: BitVec = (&[source] as &[u8]).into();
let bv: BitVec = vec![source].into(); with the traits I will add implementations for single values and small arrays for the |
Thank you for your answer. By the way, the library is great, thanks for sharing your work. What I'm trying to achieve is reading a file as a Vec buffer and then convert it into a BitVec. The original buffer can be discarded after this point. Thanks to your anwer, I have the following code: let mut buffer = Vec::new();
// Fill buffer with file content
let total_read = file.read_to_end(&mut buffer).unwrap();
// Consume original buffer in u8 to a BitVec buffer
let mut bit_buff = bitvec![BigEndian, u8;];
for n in buffer.into_iter() {
let mut bv: BitVec = (&[n] as &[u8]).into();
bit_buff.append(&mut bv);
} This, so far, is the best approach I've found. But I'm a little worried about performance (especially memory allocations). Do you think It's the right way to do it, or is there another way? |
Short answer: Fill the let mut buffer = Vec::new();
let total_read = file.read_to_end(&mut buffer).unwrap();
let mut bit_buff: BitVec<BigEndian, u8> = buffer.into();
Detailed answer: The implementation of the Any Rust borrowed slice I will add this example to the README, since the low-cost creation of bit ranges from existing data is a very important part of the crate's usage. |
Thank you so much for the detailed answer. I got a lot more insight in how this crate works. let output: Vec<u8> = bit_buff.into(); If you want to see the full code I'm working on for examples please check this repo: If you need any help with the README I would gladly collaborate. |
Yes; the library is written with the full intention that users will turn byte slices into bit slices, and bit slices back into byte slices, and I'll add named functions akin to |
|
Sorry to revive an old issue, but wondering what the current way to do this is? |
Thank you for doing so! I have discarded and rebuilt this crate a couple times and I am utterly unsurprised that I've forgotten about some of my APIs. I have restored |
Thanks! |
I would like to do something like this:
I think I can convert a
u8
to aVec<bool>
, but I would like to avoid as many conversions as possible, since performance is critical.The text was updated successfully, but these errors were encountered: