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

Allow struct members to be [de]serialised in a certain number of bits #58

Closed
aka-demik opened this issue Feb 29, 2016 · 1 comment
Closed

Comments

@aka-demik
Copy link

@aka-demik aka-demik commented Feb 29, 2016

It is often necessary for binary protocols.

A la:

#[derive(RustcEncodable, RustcDecodable, PartialEq)]
struct Entity {
    #[bits(4)]
    x: u8,
    #[bits(4)]
    y: u8,
}
@TyOverby
Copy link
Collaborator

@TyOverby TyOverby commented Feb 29, 2016

This is practically impossible because bincode needs to work with rustc_serialize and serde; both of which are general enough that it would hard to get that information through them and back into the reader/writer in bincode.

I suggest that you do something like this

struct Entity {
    xy: u8,
}

impl Entity {
    fn x(&self) -> u8 {
        self.xy & 0xF
    }

    fn y(&self) -> u8 {
        self.xy & 0xF0
    }
}
@TyOverby TyOverby closed this Feb 29, 2016
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.