Crate tinypointers

source ·
Expand description

Tiny Pointers

This crate implements pointer types that take less space than the std equivalents. You can choose between 8 or 16-bit using the flags 1byteid and 2byteid respectively.

TinyBox, TinyArc and TinyPtr are equivalent to Box, Arc and *mut T,respectively.

Some care has been taken to ensure it’s a mostly painless transition from rust types to the equivalent tinypointers type. Feel free to open a PR if functionality you need is missing!

How

To accomplish this, memory is allocated on the heap and inserted into a global array. You’re given an index inside the array, and this is what is called an id.

Size optimizations

Since this crate strives to minimize memory footprint, NonZero* are used internally to enable memory layout optimizations. This means both structs have the same size in the following example:

use tinypointers::TinyBox;

struct Bar(TinyBox<i32>);

struct Foo(Option<TinyBox<i32>>);

// 2 == 2
assert_eq!(std::mem::size_of::<Bar>(), std::mem::size_of::<Foo>())

Structs

  • A thread-safe reference-counting tiny pointer. As with all types of this crate, memory is allocated on the heap. It is equivalent to std::sync::Arc.
  • A tiny pointer to a heap allocated memory. As with all types of this crate, memory is allocated on the heap. It is equivalent to std::boxed::Box.
  • A tiny pointer to a mutable value of type T. As with all types of this crate, memory is allocated on the heap.
  • A weak reference to a TinyArc, which is a thread-safe reference-counting tiny pointer. Essentially, it is non owning, and can be upgraded to a TinyArc at any time to access the data.