Skip to content

improve byteorder docs for ReadBytesExt/WriteBytesExt #130

@rivertam

Description

@rivertam

I believe I'm doing this correctly. I'm trying to create an f64 from 4 u16s.

extern crate byteorder;

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

// hello
fn main() {
    let mut cursor = std::io::Cursor::new(vec![]);
    cursor.write_u16::<LittleEndian>(0u16).unwrap();
    cursor.write_u16::<LittleEndian>(1u16).unwrap();
    cursor.write_u16::<LittleEndian>(2u16).unwrap();
    cursor.write_u16::<LittleEndian>(3u16).unwrap();

    println!("{:?}", cursor);

    let f = cursor.read_f64::<LittleEndian>().unwrap();
    println!("{}", f);
}

My println outputs Cursor { inner: [0, 0, 1, 0, 2, 0, 3, 0], pos: 8 }, but the read_f64 line panics with:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Custom { kind: UnexpectedEof, error: StringError
("failed to fill whole buffer") }', libcore/result.rs:945:5

Am I doing something wrong? 8 x 8 = 64, so I believe the cursor has the correct number of bytes in it. I see no reason why this would be erroring in this way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions