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

fix tests due to packed issue #24

Closed
m4b opened this issue Feb 1, 2018 · 1 comment
Closed

fix tests due to packed issue #24

m4b opened this issue Feb 1, 2018 · 1 comment

Comments

@m4b
Copy link
Owner

m4b commented Feb 1, 2018

assert_eq borrows the packed structs which is now Against The Rules

ref rust-lang/rust#46043

@m4b
Copy link
Owner Author

m4b commented Feb 1, 2018

E.g.:

#[derive(Default, Copy, Clone)]
#[repr(packed)]
struct Foo {
    foo: i64,
    bar: u32,
}

impl scroll::ctx::FromCtx<scroll::Endian> for Foo {
    fn from_ctx(bytes: &[u8], ctx: scroll::Endian) -> Self {
        Foo { foo: bytes.cread_with::<i64>(0, ctx), bar: bytes.cread_with::<u32>(8, ctx) }
    }
}

impl scroll::ctx::SizeWith<scroll::Endian> for Foo {
    type Units = usize;
    fn size_with(_: &scroll::Endian) -> Self::Units {
        ::std::mem::size_of::<Foo>()
    }
}

#[test]
fn ioread_api() {
    use std::io::Cursor;
    use scroll::{LE, IOread};
    let bytes_ = [0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0xef,0xbe,0x00,0x00,];
    let mut bytes = Cursor::new(bytes_);
    let foo = bytes.ioread_with::<i64>(LE).unwrap();
    let bar = bytes.ioread_with::<u32>(LE).unwrap();
    assert_eq!(foo, 1);
    assert_eq!(bar, 0xbeef);
    let error = bytes.ioread_with::<f64>(LE);
    assert!(error.is_err());
    let mut bytes = Cursor::new(bytes_);
    let foo_ = bytes.ioread_with::<Foo>(LE).unwrap();
    assert_eq!(foo_.foo, foo);
    assert_eq!(foo_.bar, bar);
}

hdhoang added a commit to hdhoang/scroll that referenced this issue May 2, 2018
@m4b m4b closed this as completed in #28 May 6, 2018
willglynn pushed a commit to willglynn/scroll that referenced this issue Nov 2, 2018
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