heap-slice 0.1.0

Basically `Box<[T]>` but smaller stack size
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented0 out of 0 items with examples
  • Size
  • Source code size: 26.43 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 4.97 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • dragazo/heap-slice
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • dragazo

As we know, Box<T> is a simple wrapper for a pointer into the heap. So it's always 8-bytes on the stack, right? Wrong! In order to make many unsized types work (e.g., [T] and str), Rust stores some extra information (e.g., length) in the pointer itself. This creates a larger stack structure called a "fat" pointer. Thus, Box<[T]> and Box<str> actually take up 16 bytes on the stack!

For some applications, this can be very undesirable (e.g., one large enum variant will make the whole enum large). This crate provides two new types called [HeapSlice<T>] and [HeapStr] which solve this problem by storing the length in the heap rather than the stack. Thus, these types serve as a drop-in replacement for Box<[T]> and Box<str>, but only take 8 bytes on the stack rather than 16.

Both types support [Option] size optimization and a special non-allocating case for empty slices/strings.

no_std

This crate supports building in no_std environments out of the box, but naturally alloc is still required.