Code Monkey home page Code Monkey logo

Comments (3)

Robbepop avatar Robbepop commented on July 17, 2024

Thanks for the suggestion!

The to_bytes conversion is simple. We can just generate:

fn to_bytes(&self) -> &[u8] { &self.data }

The from_bytes conversion is actually the annoying one since we may have to consider bitfield structs that might contain invalid bit sets. For example this could be well introduced with the feature to support non-powers-of-two enum bit specifiers.

Also it is an open question whether to take [u8; N] where N is the total number of bytes in the struct or whether something more generically useful such as &[u8] or impl Iterator<Item = u8> should be taken as argument in the from_bytes constructor.

Also for the cases with invalid bit ranges mentioned above we need to return a Result instead of Self directly.

I currently strive for the following definition:

fn from_bytes(bytes: &[u8]) -> Result<Self, Error>;

Where Error is either an OutOfBounds or InvalidByteAmount error kind.

Endianess

One could argue if endianess should be considered.

I don't think that we should do that here since there is a well set endianess already within the internal encoding of the bitfield struct that is little endian.

Core Read and Write

An improvement over operating directly on &[u8] would be to operate generically on types implementing Read and Write. However, this API cannot be made available for no_std environments currently.

Future Problems

Without #3 this API is a lot easier since it doesn't need to handle bit holes that are implied by implementing the mentioned feature.

from modular-bitfield.

Robbepop avatar Robbepop commented on July 17, 2024

With 48bd16c in place we should have an easier life implementing a sane API for converting from bits.

from modular-bitfield.

Robbepop avatar Robbepop commented on July 17, 2024

Simplified implementation that does not yet respect the eventually future addition of non-power-of-two enums: 5c55798

from modular-bitfield.

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.