GithubHelp home page GithubHelp logo

eio's Introduction

eio

Read and write numbers in big-endian and little-endian.

๐Ÿš€ Getting started

Add the following to your Cargo manifest.

[dependencies]
eio = "0.1"

And bring the ReadExt and/or WriteExt traits into scope.

use eio::{ReadExt, WriteExt};

๐Ÿคธ Usage

The most common usage is parsing numbers from a source. You can do this using the read_le() and read_be() methods on anything that implements Read.

use eio::ReadExt;

// `Cursor` implements `Read`
let mut rdr = std::io::Cursor::new([
  0x37, 0x13,
  0x12, 0x34, 0x56, 0x78,
  0x00, 0x09, 0x10,
]);

// Read a two byte `u16` in little-endian order
let i: u16 = rdr.read_le()?;
assert_eq!(i, 0x1337);

// Read a four byte `i32` in big-endian order
let i: i32 = rdr.read_be()?;
assert_eq!(i, 0x12345678);

// Read a three byte array
let a: [u8; 3] = rdr.read_array()?;
assert_eq!(a, [0x00, 0x09, 0x10]);

Serialization of numbers can be done using the write_le() and write_be(). This can be done on anything that implements Write.

use eio::WriteExt;

// `&mut [u8]` implements `Write`.
let mut wtr = Vec::new();

// Write a four byte `f32` in little-endian order
wtr.write_le(1_f32)?;
// Write a one byte `u8`
wtr.write_be(7_u8)?;

assert_eq!(wtr, &[0, 0, 0x80, 0x3f, 0x07]);

In no_std contexts the FromBytes and ToBytes traits can be used directly.

use eio::{FromBytes, ToBytes};

let x: u32 = FromBytes::from_be_bytes([0, 0, 0, 7]);
assert_eq!(x, 7);

let data = ToBytes::to_le_bytes(x);
assert_eq!(data, [7, 0, 0, 0]);

๐Ÿ’ก Prior art

eio provides the same capabilities as the popular byteorder crate but with a very different API. The advantages of eio are the following:

  • It is extendible, anyone can implement FromBytes or ToBytes for their own integer types.
  • Uses the core/std {from,to}_{le,be}_bytes functions to do the conversion for floats and integers. byteorder reimplements these.
  • Doesn't require turbofish type annotations all the time.
    // byteorder
    let i = rdr.read_u16::<BigEndian>()?;
    // eio
    let i: u16 = rdr.read_be()?;

License

Licensed under either of

at your option.

eio's People

Contributors

rossmacarthur avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

vorot93

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.