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

add optional const_generics support #187

Merged
merged 1 commit into from
Mar 16, 2021

Conversation

soruh
Copy link
Contributor

@soruh soruh commented Mar 15, 2021

Adds a const_generics feature that uses min_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.

Copy link
Owner

@sharksforarms sharksforarms left a 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!

@sharksforarms sharksforarms merged commit c5c01df into sharksforarms:master Mar 16, 2021
@soruh
Copy link
Contributor Author

soruh commented Mar 16, 2021

great, thank you!

@soruh soruh deleted the const_generics branch March 16, 2021 13:58
sharksforarms pushed a commit that referenced this pull request Mar 16, 2021
Added `const_generics` feature to enable const generics over slice implementations
@v1gnesh
Copy link

v1gnesh commented Jul 15, 2023

Hey @soruh, could you show an example of how this can be used?

@wcampbell0x2a
Copy link
Collaborator

Hey @soruh, could you show an example of how this can be used?

This feature is enabled on master:

default = ["std", "const_generics"]

@soruh
Copy link
Contributor Author

soruh commented Jul 15, 2023

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.

@v1gnesh
Copy link

v1gnesh commented Jul 15, 2023

Hey @soruh, could you show an example of how this can be used?

This feature is enabled on master:

default = ["std", "const_generics"]

Do I have to add something to my project's Cargo.toml to make use of this?
I get a squiggly red line under DekuRead when my Struct uses has a field like this:

    field: MoString<n>

where

pub struct MoString<const N: usize> (
    #[deku(map=func)
    String,
);

@soruh
Copy link
Contributor Author

soruh commented Jul 15, 2023

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.

@v1gnesh
Copy link

v1gnesh commented Jul 15, 2023

Yes, sure, will try to reproduce separately and open a discussion. Thanks for responding here.

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

Successfully merging this pull request may close these issues.

4 participants