-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
add optional const_generics
support
#187
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! Thanks!
great, thank you! |
Added `const_generics` feature to enable const generics over slice implementations
Hey @soruh, could you show an example of how this can be used? |
It allows you to use arrays that are more than 32 elements long and arrays with const generic sizes, so this impl here's an example: use deku::{DekuContainerRead, DekuContainerWrite, DekuRead, DekuUpdate, DekuWrite};
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(endian = "big")]
struct DekuTest<const N: usize> {
#[deku(bits = "4")]
field_a: u8,
#[deku(bits = "4")]
field_b: u8,
field_c: [u8; 40],
field_d: [u8; N],
}
fn main() {
const N: usize = 128;
let mut data = vec![0u8; 1 + 40 + N];
data[0] = 170;
data[1..]
.iter_mut()
.enumerate()
.for_each(|(i, x)| *x = i as u8);
let (_, value) = DekuTest::<N>::from_bytes((data.as_ref(), 0)).unwrap();
dbg!(value);
} without the feature this does not compile. It's a only a feature, because const-generics were very new back then and older compiler versions do not support const-generics. |
Do I have to add something to my project's Cargo.toml to make use of this? field: MoString<n> where pub struct MoString<const N: usize> (
#[deku(map=func)
String,
); |
The feature should is enabled by default, unless you explicitly turned it of with default-features=false or are using an ancient version. I think you probably made a mistake while implementing your MyString type. Also generally, a merged pull request from two years ago is not the greatest place to ask these things... Maybe open a discussion or something and please provide the error you're getting instead of just saying you're getting an error. |
Yes, sure, will try to reproduce separately and open a discussion. Thanks for responding here. |
Adds a
const_generics
feature that usesmin_const_generics
which will be stabilized on March 25, 2021 as part of Rust 1.51 (and is currently available in beta) to allow for reading/writing arrays with >32 elements when enabled.