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

Passing multiple parameters to a map #212

Closed
v1gnesh opened this issue May 14, 2021 · 2 comments
Closed

Passing multiple parameters to a map #212

v1gnesh opened this issue May 14, 2021 · 2 comments

Comments

@v1gnesh
Copy link

v1gnesh commented May 14, 2021

Hello,

Firstly, thank you for this excellent work.
Seriously look forward to how much better this is going to get.

Request your inputs/help please on the following use case:

pub struct H {
  a: u16,
  #[deku(map = "H::morph")]
  b: String,
}

impl H {
  pub fn morph(bytes: [u8; 4]) -> Result<String, DekuError> {
    // here I use both bytes & len
  }
}

What I want to do is use something like #[deku(map = "H::morph(4)")] to specify how many bytes to take in into the function.
That is, is there a way to pass the 4 to the fn morph from the #[deku(map ...)] definition?

Thank you!

@v1gnesh
Copy link
Author

v1gnesh commented May 14, 2021

I noticed that there's a bytes_read attribute.
Can this be updated to make it accept a number directly, rather than having to specify it through another field in the same struct?
That would be awesome; will help my current situation.

@v1gnesh
Copy link
Author

v1gnesh commented May 15, 2021

It worked with a literal.

pub struct H {
  a: u16,
  #[deku(map="morph", bytes_read="4")]
  b: String,
}
pub fn morph(bytes: &[u8]) -> Result<String, DekuError> {
   let len = bytes.len();
  // do stuff
  Ok(whatever)
}

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

1 participant