Comments (8)
@netvl Hmm, is there any particular reason why NativeEndian
doesn't work for you? (This otherwise looks like a dupe of #18.)
from byteorder.
@netvl Oh, I misread your question. I guess I'm not following. What code have you tried? You should be able to invoke either endianness by calling e.g., BigEndian::read_u32
or LittleEndian::read_u32
.
from byteorder.
I'd like to pass the actual endianness to read functions at runtime, something like
let byte_order = if <some check> { LittleEndian } else { BigEndian };
let n = source.read_u16(byte_order);
Here the required byte order is determined at runtime. I also can't use ByteOrder
as a trait object because all its methods are static (and anyway, they don't work with Read
directly).
from byteorder.
@netvl Define some helper functions? e.g.,
enum Endian { Little, Big }
fn read_u16(source: &[u8], endian: Endian) -> byteorder::Result<u16> {
match endian {
Endian::Little => byteorder::LittleEndian::read_u16(source),
Endian::Big => byteorder::BigEndian::read_u16(source),
}
}
read_u16(&[...], Endian::Little)
from byteorder.
Well, yes, that's what I've done. I've written an extension trait for Read
and a macro which generates all the necessary methods. I just think that this should be provided by the library :)
from byteorder.
@netvl I see. I'm not totally convinced the added API surface is really worth it ("I'm trying to use byteorder but there are three different ways to read a big endian number, which one should I use?"). I can see how they would be useful occasionally though.
from byteorder.
Maybe this problem is solved by the proper documentation. Something along the following lines:
There are three ways to read a number in a specific endianness:
- by using methods of
ByteOrder
trait, likeBigEndian::read_u16(buf)
, if you need to read a number from a byte slice with a statically known byte order;- by using methods of
ReadBytesExt
extension trait, likesource.read_u16::<BigEndian>()
, if you need to read a number from aRead
instance with a statically known byte order;- or by using methods of
ReadBytesDynamicExt
extension trait, likesource.read_u16(byte_order)
, if you need to read a number from aRead
instance when the byte order is determined at runtime.
from byteorder.
I still personally feel like expecting one to write a helper function for this is perfectly fine. I'd really rather not make the API more complex than it already is.
from byteorder.
Related Issues (20)
- Support async analogues of Read and Write extensions HOT 2
- Add `read_u40` HOT 1
- Using byteorder with type definitions HOT 1
- upgrade quickcheck to 1.0
- panic in try from slice HOT 4
- Changelog is not up to date
- Document MSRV HOT 1
- Rust is failing to find type definitions or enumerations HOT 2
- Consider supporting outdated Rust versions to make bincode happy HOT 1
- Add impl of `ReadBytesExt` and `WriteBytesExt` for `core2::io::Write` HOT 7
- Implement `read_usize` HOT 5
- Transparently support reading into arrays of types that are byte-compatible with integers HOT 1
- Endianness-managing Read/Write wrapper? HOT 2
- add ReadFrom and WriteTo traits with a derive macro HOT 1
- Any difference to Rust official functions? HOT 1
- Unsound assumption about the alignment of u32 and f32
- write_i64 does not write to the buffer HOT 2
- [Bug] Some unknown error caused by byteorder 1.5.0 HOT 1
- Expose `#[forbid(unsafe_code)]` subset HOT 3
- write_int never panics on out of range input
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from byteorder.