Skip to content

Nested vec #336

Answered by sondrelg
sondrelg asked this question in Q&A
May 14, 2023 · 2 comments · 1 reply
Discussion options

You must be logged in to vote

I found this to be a little simpler:

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
pub struct DekuString {
    #[deku(bytes = "1")]
    content_len: u8,
    #[deku(count = "content_len")]
    content: Vec<u8>,
}

impl Into<String> for DekuString {
    fn into(self) -> String {
        String::from_utf8_lossy(&self.content).to_string()
    }
}

impl From<String> for DekuString {
    fn from(value: String) -> Self {
        // TODO: Handle u8 conversion
        DekuString { content_len: value.len() as u8, content: Vec::from(value) }
    }
}

and then doing:

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
pub struct Foo {
    #[deku(bytes = "1")]
    foo_len: u8,
    #[deku(count = "foo_len"

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@wcampbell0x2a
Comment options

Answer selected by sondrelg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants