• Stars
    star
    107
  • Rank 322,087 (Top 7 %)
  • Language
    Rust
  • Created over 4 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Rust crate that allows you to match, bind, and pack the individual bits of integers.

Bitmatch

Crates.io Library Version Number Crates.io Library License Docs.rs Documentation Version Number

The bitmatch crate provides tools for packing and unpacking integers as sequences of bits. Supports #![no_std].

Examples

Using #[bitmatch] with a let unpacks the bits into separate single-character variables for each letter you use.

Using bitpack!() re-packs the bits from those single-character variables into a single integer.

use bitmatch::bitmatch;

#[bitmatch]
fn interleave(n: u8) -> u8 {
    #[bitmatch]
    let "xxxx_yyyy" = n;
    bitpack!("xyxy_xyxy")
}

fn main() {
    assert_eq!(interleave(0xF0), 0xAA);
}

You can use #[bitmatch] on a match as well, and it will ensure that the literal portions match, and bind the variable portions.

use bitmatch::bitmatch;
#[bitmatch]
fn decode(inst: u8) -> String {
    #[bitmatch]
    match inst {
        "00oo_aabb" => format!("Op {}, {}, {}", o, a, b),
        "0100_aaii" => format!("Val {}, {}", a, i),
        "01??_????" => format!("Invalid"),
        "10ii_aabb" => format!("Ld {}, {}, {}", a, b, i),
        "11ii_aabb" => format!("St {}, {}, {}", a, b, i),
    }
}

More Repositories

1

pixel-canvas

A crate to make drawing in a buffer of pixels easy!
Rust
32
star
2

sleeptracker-ios

An iOS app written in Swift that tracks sleep habits, built to learn the HealthKit API.
Swift
12
star
3

scheme-jit

A JIT compiler for a small scheme dialect, as a project for Advanced Compilers.
Python
10
star
4

feedburst

A tool to consume comic RSS feeds and present them to you in bunches, on a schedule of your choosing
Rust
10
star
5

minecraft-eda

Electronic design automation for Minecraft
Python
9
star
6

buddhabrot

Render a buddhabrot fractal
Rust
9
star
7

hyalite

A bounded model checker for an IMP-style imperative language.
Rust
7
star
8

nangate

Yosys passes to syntheize to NaN gates (à la http://tom7.org/nand/)
C++
7
star
9

graphics-demos

Every graphics demo I wrote for my talk
Rust
5
star
10

canvas-cli

A CLI interface to the Canvas LMS
Rust
5
star
11

ess

Parsing s-expressions in rust
Rust
4
star
12

learn-languages

Let's learn lots and lots of programming languages!
Coq
4
star
13

cgl

Following https://github.com/ssloy/tinyrenderer/wiki to write a software renderer in Rust!
Rust
3
star
14

acyc-lang

An experimental reference counted strict functional language
Python
3
star
15

wav-synth

Synthesize straight into a .WAV file from C
Python
2
star
16

apple-jbo-dictionary

Generate an English/Lojban dictionary for the macOS Dictionary.app based on wordlists.
Python
2
star
17

aoc-2018

Doing Advent of Code in 25 languages!
Assembly
2
star
18

comic-publisher

A tool for self-publishing comic websites.
Rust
2
star
19

vcd-view

A lightweight vcd-viewer that emits a tiny viewer webpage
Rust
1
star
20

hs-total-conversion

A project that allows you to replace various parts of homestuck.com. Originally created for calibornstuck.
1
star
21

ICBM

Strategic Offense Initiative — A game simulating user-designed programmable ICBMs, and your own personal nuclear war.
C++
1
star
22

compiler-parts

Parts of a compiler backend, from scratch
OCaml
1
star
23

celestial-echo

Rust
1
star
24

aoc-2019

Advent of Code in 25 languages
Assembly
1
star
25

julia

A parallel realtime Julia set renderer in Rust
Rust
1
star
26

chemset.js

A javascript library to typeset chemical equations written in a human readable format.
JavaScript
1
star
27

PythonTAG

An open-source python Text Adventure Game engine
Python
1
star
28

emily-quest

Emily Quest for SNES
Assembly
1
star
29

NavierStokes

A little experiment to try out fluid simulations
C
1
star