Code Monkey home page Code Monkey logo

Comments (8)

BurntSushi avatar BurntSushi commented on May 30, 2024

@netvl Hmm, is there any particular reason why NativeEndian doesn't work for you? (This otherwise looks like a dupe of #18.)

from byteorder.

BurntSushi avatar BurntSushi commented on May 30, 2024

@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.

netvl avatar netvl commented on May 30, 2024

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.

BurntSushi avatar BurntSushi commented on May 30, 2024

@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.

netvl avatar netvl commented on May 30, 2024

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.

BurntSushi avatar BurntSushi commented on May 30, 2024

@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.

netvl avatar netvl commented on May 30, 2024

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, like BigEndian::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, like source.read_u16::<BigEndian>(), if you need to read a number from a Read instance with a statically known byte order;
  • or by using methods of ReadBytesDynamicExt extension trait, like source.read_u16(byte_order), if you need to read a number from a Read instance when the byte order is determined at runtime.

from byteorder.

BurntSushi avatar BurntSushi commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.