GithubHelp home page GithubHelp logo

jalcine / compact-encoding-rs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mkroehnert/compact-encoding-rs

1.0 0.0 0.0 57 KB

A series of compact encoding schemes for building small and fast parsers and serializers (based on https://github.com/compact-encoding/compact-encoding)

License: MIT License

Nix 0.59% Rust 99.41%

compact-encoding-rs's Introduction

compact-encoding

Rust implementation of <https://github.com/compact-encoding/compact-encoding>

Usage

use compact_encoding::*;

let mut state = State::new();

let unsigned8: u8 = 42;
// use pre_encode to determine the required buffer size
unsigned8.pre_encode(&mut state);
//String::pre_encode(&mut state, "hi");

println!("State: {:#?}", state);

// allocate the buffer based on previous pre_encode() calls
state.alloc();

// actually encode the values into the buffer
unsigned8.encode(&mut state);
//String::encode(&mut state, "hi");

// for decoding, the decode() method is used (state.start should point to start of buffer)
state.start = 0;

let uint8 = u8::decode(&mut state).expect("could not decode");
println!("{:?}", uint8);
//let string = String::decode(&mut state).expect("could not decode"));
//println!("{:?}", string);

Encoder API

State

See [State]

val.preencode(state);

Does a fast preencode dry-run that only sets state.end. Use this to figure out the required buffer size. Afterwards call state.alloc(); to create state.buffer.

val.encode(state).expect("could not encode");

Encodes val into state.buffer at position state.start. Updates state.start to point after the encoded value when done.

let val = <type>::decode(state).expect("could not decode");

Decodes a value from state.buffer at position state.start. Updates state.start to point after the decoded value in the buffer when done.

Helpers

If you are just encoding to a buffer or decoding from one you can use the encode and decode helpers to reduce your boilerplate

//const buf = cenc.encode(cenc.bool, true)
//const bool = cenc.decode(cenc.bool, buf)

Spec

Details on the encoding are documented in spec.md.

Design

Details on design ideas are documented in design.md.

License

MIT

compact-encoding-rs's People

Stargazers

 avatar

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.