inplace-box

Crates.ioinplace-box
lib.rsinplace-box
version0.2.1
created_at2025-07-18 09:53:19.844647+00
updated_at2025-07-28 08:50:46.676237+00
descriptionA stack-allocated container similar to Box but without heap allocation
homepagehttps://github.com/drmingdrmer/inplace-box
repositoryhttps://github.com/drmingdrmer/inplace-box
max_upload_size
id1758810
size41,435
张炎泼 (drmingdrmer)

documentation

https://docs.rs/inplace-box

README

InplaceBox

InplaceBox is a Rust library that provides a stack-allocated container similar to Box, but without heap allocation. It stores data inline within a fixed-size buffer.

Features

  • Store values on the stack without heap allocation
  • Support for dynamically sized types (like trait objects)
  • Similar interface to Box with Deref and DerefMut implementations

Usage

use inplace_box::InplaceBox;

trait MyTrait {
    fn method(&self) -> i32;
}

struct MyStruct(i32);

impl MyTrait for MyStruct {
    fn method(&self) -> i32 {
        self.0
    }
}

fn main() {
    let inplace_box = InplaceBox::<dyn MyTrait>::new(MyStruct(42));
    assert_eq!(inplace_box.method(), 42);
}

Requirements

This crate requires the nightly Rust compiler as it uses the following unstable features:

  • generic_const_exprs
  • ptr_metadata
  • unsize
  • coerce_unsized

How It Works

InplaceBox uses Rust's advanced type system features to:

  1. Store the data in a fixed-size inline buffer
  2. Store vtable metadata for trait objects
  3. Implement smart pointer traits for seamless usage

It's ideal for embedded systems, performance-critical code, or situations where heap allocations should be avoided.

License

MIT

Contributions

Contributions are welcome! Please feel free to submit a Pull Request.

Commit count: 0

cargo fmt