std::io::Read provides byte-order reading trait (original) (raw)

Proposal

Problem statement

The std::io::Read implementation is far too rudimentary and a real pain to work with. I haven't even found any decent third-party libraries. Take the byteorder library as an example, its code and implementation leave much to be desired.

use std::io::Cursor; use byteorder::{BigEndian, ReadBytesExt};

let mut rdr = Cursor::new(vec![2, 5, 3, 0]); // Note that we use type parameters to indicate which kind of byte order // we want! assert_eq!(517, rdr.read_u16::().unwrap()); assert_eq!(768, rdr.read_u16::().unwrap());

Motivating examples or use cases

use std::io::{ Read, byte_order::ReadLeOrder };

fn test(reader: &mut Read) { let x1 = reader.read_u8()?; let x2 = reader.read_u32()?; let x3: u64 = reader.read_as()?; // Generic Support let arr: [u16; 200] = reader.read_as()?; // Generic Support }

Solution sketch

Alternatives

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

Second, if there's a concrete solution: