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

fixed length arrays have their size encoded #86

Closed
oohal opened this issue Aug 27, 2016 · 10 comments
Closed

fixed length arrays have their size encoded #86

oohal opened this issue Aug 27, 2016 · 10 comments

Comments

@oohal
Copy link

@oohal oohal commented Aug 27, 2016

When a encoding a structure that contains a fixed length array the length of the array is also encoded. For example, this code:

extern crate rustc_serialize;
extern crate bincode;

use bincode::SizeLimit;
use bincode::rustc_serialize::encode;

#[derive(RustcEncodable)]
struct TestStruct {
    a : u16,
    b : [u8; 6],
}

fn main() {
    let test = TestStruct {a: 0xffff, b: [1, 2, 3, 4, 5, 6]};
    let encoded: Vec<u8> = encode(&test, SizeLimit::Infinite).unwrap();

    println!("header: {:?}", encoded);
}

Produces this output:

header: [255, 255, 0, 0, 0, 0, 0, 0, 0, 6, 1, 2, 3, 4, 5, 6]

This behaviour makes it hard to use bincode for unpacking externally defined binary formats (e.g ELF headers). I suppose this library isn't really designed for that, but it works well enough for BE formats so it would be nice to see this fixed in the next major version.

@TyOverby
Copy link
Collaborator

@TyOverby TyOverby commented Aug 30, 2016

I don't think that there is a way to do this with rustc-serialize. At least, there isn't a trait method for statically sized arrays.

@Matvey-Kuk
Copy link

@Matvey-Kuk Matvey-Kuk commented Sep 14, 2016

+1 for this idea

@TyOverby
Copy link
Collaborator

@TyOverby TyOverby commented Sep 14, 2016

This feature is only possible if we drop RustcSerialize support. I'm planning on doing that eventually, but it's on the backlist until serde hits 1.0

@Matvey-Kuk
Copy link

@Matvey-Kuk Matvey-Kuk commented Sep 14, 2016

@TyOverby thank you! Just wanted to mention there is somebody else who need this =)

@aldanor
Copy link

@aldanor aldanor commented Jan 26, 2017

Very much in support of this as well. Now that serde 0.9 is out, is it a good time to revisit this? (also now that custom derive will get stabilised in a few weeks)

@darfink
Copy link

@darfink darfink commented Jan 29, 2017

I'd like to chime in on this as well. Working with external data and this issue hit me, ended up having to use a fork.

@mikedilger
Copy link
Contributor

@mikedilger mikedilger commented Jan 30, 2017

I also ran into this. What if we change the code to skip these lengths if the rustc_serialize feature is disabled?

(I noticed https://github.com/serde-rs/bench says it is like bincode w/o these lengths)

@TyOverby
Copy link
Collaborator

@TyOverby TyOverby commented Jan 30, 2017

Since there's so much interest in this, I think I'll begin work on removing rustc-serialize support from bincode altogether. I've wanted to do this for a long time, but since serde is hitting 1.0 and macros are landing, I think it'll be pretty reasonable to move on to.

@TyOverby
Copy link
Collaborator

@TyOverby TyOverby commented Jan 31, 2017

rustc-serialize removal happening here: #95

@aldanor
Copy link

@aldanor aldanor commented Jan 31, 2017

That's great, thanks! Given that this will be a (very-) breaking change release, does it make sense to break things a little further and add endianness support?

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
6 participants
You can’t perform that action at this time.