-
Notifications
You must be signed in to change notification settings - Fork 341
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
Better support for byte ordered reads and writes? #578
Comments
EDIT: Oops..Sorry..Didn't notice that you've already pointed this out |
From https://internals.rust-lang.org/t/pre-rfc-v2-safe-transmute/11431:
So the traits from the transmute proposal won't work for us. |
The latest tokio (0.2.3) has support for reading/writing integers, they chose |
Implemented the trait-based design for std's Read / Write types in use std::io::{Cursor, Seek, SeekFrom};
use omnom::prelude::*;
let mut buf = Cursor::new(vec![0; 15]);
// Write this u16 as little-endian bytes.
let num = 12_u16;
buf.write_le_bytes(num).unwrap();
buf.seek(SeekFrom::Start(0)).unwrap();
// Read a u16 from little-endian bytes.
let num: u16 = buf.read_le_bytes().unwrap();
assert_eq!(num, 12); This feels like the right choice; very similar to std. Should be trivially portable to async-std as well. |
Oh also for the record: I wrote about this topic in long-form a while ago: https://blog.yoshuawuyts.com/byte-ordered-stream-parsing/ |
is there any progress on this? Anything i can use to that has similar support as byteorder crate? |
Something that came up today was the question how to read and write bytes with using a certain endianness. @goto-bus-stop replied in chat with the following:
However they also pointed out that using
byteorder
one could do:Which seems quite nice. A port of this functionality exists for Tokio in the form of
tokio-byteorder
. With support for thefutures::io::{AsyncRead, AsyncWrite}
currently in the works.Design questions
People are currently already capable of reading and writing bytes with a certain endianness, without any issues. The hard parts are taken care of. However it doesn't quite feel ergonomic yet. So what I'm wondering is if we could perhaps improve the status quo here somewhat by providing support for this out of the box.
Writing bytes is fortunately already a one-liner:
Byteorder inspired
But reading bytes isn't yet. We could probably do better here, and I see a few options. The first is to follow
byteorder
's lead and add 16 methods on the Read trait, twoEndianness
enums, and aNativeEndian
type alias:std inspired
Another option seems to be to add 48 new methods on the Read trait (3 endianness * 16 nums), and try to follow
std
's naming conventions more closely:using traits
The third option, and I have no idea if this works (we should test this) is to add two new methods on the Read trait, and a trait that we implement for all number types so we can be generic over them, and the method knows how to decode them:
This last approach is somewhat iffy because it would show up in the function signature, which means we'd have to expose it (but wouldn't want people to implement it). Or we could make it a sealed trait, but I'm not a fan of doing that.
It seems like https://internals.rust-lang.org/t/pre-rfc-safe-transmute/11347 might be proposing a trait that could potentially cover this, but I'm unsure about the exact implications and relation to this. Maybe we should bring it up?
If we could find a way to make this work this would definitely be my preferred option, as it's easy to add a counterpart to
Write
as well (creating symmetry, and an even smaller one-liner). But that's a big if because there seem to be quite a few hurdlesConclusion
I've talked about the current state of reading and writing bytes from
async_std::io::{Read, Write}
, and explored possible directions to improve this.This is not something we need to find a solution for immediately, but it's something that if we can figure out it'll make writing certain programs easier for sure. Thanks!
The text was updated successfully, but these errors were encountered: