Bitpack a boolean into a pointer using bit magic.

Overview

ptr-bool

tl;dr: a pointer and boolean with the same size as a pointer.

A convenience crate used to bitpack a boolean and pointer into the same eight bytes (four on 32-bit systems, two on 16-bit systems). This is done by storing the boolean in the one-bit of a two-aligned pointer.

Usually a pointer and a boolean would be padded to be 16 bytes together, while this method allows the pointer and boolean to fit inside of 8 bytes.

This project was inspired by the rust-gc project, which used this method for garbage collected references. I saw the method and thought that this was incredibly smart, so I created my own version with some convenience wrapping and made its own crate.

Caveats

  • The pointer must be aligned by two to ensure that the one-bit is unnecessary and can be used to stored the boolean value.

  • Possibly a slight overhead when reading the values of the PtrBool, as the boolean value must be omitted from the pointer each time it's read, and the pointer value must be omitted from the boolean value each time it is read. That is, unless there is some sort of unknown optimization built into Rust for the case of storing a boolean in one bit.

  • The value stored inside of a PtrBool must be Sized. This is because the pointer is converted to and from a usize; which loses any metadata which would have been in the raw pointer.

You might also like...
A tool that helps you to turn in one command a Rust crate into a Haskell Cabal library!
A tool that helps you to turn in one command a Rust crate into a Haskell Cabal library!

cabal-pack A tool that helps you to turn in one command a Rust crate into a Haskell Cabal library! To generate bindings, you need to annotate the Rust

Static-checked parsing of regexes into structs

Statically-checked regex parsing into structs. This avoids common regex pitfalls like Off by one capture indexes Trying to get nonexistent captures De

Time to dive into Rust!

Lets-Learn-Rust Time to dive into Rust! Day 1 Installation Running a Simple Rust Program Managing Projects with Cargo Basic Programming - Comments and

A comprehensive and FREE Online Rust hacking tutorial utilizing the x64, ARM64 and ARM32 architectures going step-by-step into the world of reverse engineering Rust from scratch.
A comprehensive and FREE Online Rust hacking tutorial utilizing the x64, ARM64 and ARM32 architectures going step-by-step into the world of reverse engineering Rust from scratch.

FREE Reverse Engineering Self-Study Course HERE Hacking Rust A comprehensive and FREE Online Rust hacking tutorial utilizing the x64, ARM64 and ARM32

Code accompanying the 1 Hour Dive into Async live stream.
Code accompanying the 1 Hour Dive into Async live stream.

1 Hour Async This is the code accompanying the 1 Hour Dive into Async live-stream. The slides are here You can watch the recorded event on YouTube Inc

Conversion of .eopkg into .stone

a-piece-of-pisi Crazy time: We're building a tool to convert a selection of .eopkg packages into .stone packages to be consumed by moss-rs to vastly a

Rust explained using easy English
Rust explained using easy English

Update 22 December 2020: mdBook can be found here. 28 November 2020: Now also available in simplified Chinese thanks to kumakichi! 1 February 2021: No

A fast uuid generator in Python using Rust

ruuid A fast UUID generator for Python built using Rust. Its a simple wrapper on top of Rust's UUID crate. How to use? Installation: pip3 install ruui

(Ab)using technology for fun & profit.

(Ab)using technology for fun & profit. Code accompanying my blog

Owner
Zack
programming language nerd
Zack
Rust based magic-string with source map chains support

enhanced-magic-string Rust implementation of https://www.npmjs.com/package/magic-string with original sourcemap chain support. license. This project i

Farm 3 Nov 5, 2023
Single-side boolean deserializers.

serde-bool Single value, true or false, boolean deserializers. Examples Supporting serde untagged enums where only one boolean value is valid, allowin

null 5 Dec 3, 2023
A bit-packed k-mer representation (and relevant utilities) for rust

K-mer class for rust The purpose of this repository is to build a simple library that exposes a bit-packed k-mer class for use in rust-based bioinform

COMBINE lab 41 Dec 15, 2022
Isn't it time to be a bit nicer to rustc?

politeness-macro Aren't we all too rude to computers? Isn't it time to bring a bit more politeness into our programming? Shouldn't we be a bit nicer t

Rin 6 Mar 11, 2022
fanum tax 64-bit integers with LEB128

rizz64 Fanum* tax 64-bit integers. * Fanum is a popular streamer who taxes his friends by taking bites of their food. This crate provides an efficient

Matthew Kim 11 May 22, 2024
Simple bit-level protocol definitions in Rust.

bin-proto Simple & fast structured bit-level binary co/dec in Rust. An improved and modernized fork of protocol. A more efficient but (slightly) less

null 16 Jun 13, 2024
An inquiry into nondogmatic software development. An experiment showing double performance of the code running on JVM comparing to equivalent native C code.

java-2-times-faster-than-c An experiment showing double performance of the code running on JVM comparing to equivalent native C code ⚠️ The title of t

xemantic 49 Aug 14, 2022
A translation of akaritakai/AdventOfCode2021 into Rust

Advent Of Code 2021 Solutions This repo contains my Advent Of Code 2021 solutions in Rust. This repo is a learning opportunity (a "my first Rust proje

Justin Kaufman 2 Feb 7, 2022
A tool to deserialize data from an input encoding, transform it and serialize it back into an output encoding.

dts A simple tool to deserialize data from an input encoding, transform it and serialize it back into an output encoding. Requires rust >= 1.56.0. Ins

null 11 Dec 14, 2022
cargo-add command to make dependencies into dylibs

cargo add-dynamic This cargo command allows to wrap dependencies as dylibs. For why you might want this see Speeding up incremental Rust compilation w

Robert Krahn 62 Dec 27, 2022