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

Option to set the bytes used for encoding str? #226

Closed
tejom opened this issue Feb 19, 2018 · 1 comment
Closed

Option to set the bytes used for encoding str? #226

tejom opened this issue Feb 19, 2018 · 1 comment

Comments

@tejom
Copy link

@tejom tejom commented Feb 19, 2018

Hey,

I thought it might interesting to use this library to decode a network protocol. The protocol Im working with uses one byte to determine the length of a string. Would be it be possible to have an option that sets the amount of bytes used when encoding or decoding?

Something I tried was this

fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
    where
        V: serde::de::Visitor<'de>,
{
    #[cfg(feature="len_u8")]
    let len: u8 = serde::Deserialize::deserialize(&mut *self)?;

    #[cfg(feature="len_usize")]
    let len: usize = serde::Deserialize::deserialize(&mut *self)?;

    self.read_bytes(len as u64)?;
    self.reader.forward_read_str(len as usize, visitor)
}

This struct

struct Point<'a> {
  x: u8,
  y: u8,
  s: &'a str,
}

would be encoded like this
[100,78,4,116,101,115,116]

instead of

[100, 78, 4, 0, 0, 0, 0, 0, 0, 0, 116, 101, 115, 116]

@tejom tejom changed the title Option to set the bytes used for encoding? Option to set the bytes used for encoding str? Feb 19, 2018
@TyOverby
Copy link
Collaborator

@TyOverby TyOverby commented Feb 19, 2018

Nope, not possible. If you are decoding a network protocol, I'd recommend taking a look at Nom.

@TyOverby TyOverby closed this Feb 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.