soruh cd5e904ce1
CI / Stable checks (push) Successful in 32s
CI / Rust 1.85 (push) Successful in 34s
CI / Miri (push) Successful in 48s
CI / Coverage (push) Successful in 36s
move to no_std + alloc
2026-08-04 08:23:29 +02:00
2026-07-31 14:37:36 +02:00
2026-08-04 08:23:29 +02:00
2026-07-26 12:18:09 +02:00
2026-07-31 03:48:26 +02:00
2026-07-31 02:31:43 +02:00
2026-08-04 08:23:29 +02:00
2026-07-31 12:31:30 +02:00

type_erased_vec_capacity

CI

type_erased_vec_capacity keeps a Vec allocation available while its element type is temporarily erased. This is useful when a buffer is reused for different element types with the same Layout, without freeing and reallocating it between uses.

The safe APIs clear the current elements before changing the type. A [ContentGuard] provides ordinary Vec-like access while the type is fixed; dropping the guard returns the allocation to its erased owner.

use type_erased_vec_capacity::TypeErasedVec;

let mut erased = TypeErasedVec::new(vec![1_u32, 2, 3]);
let capacity = erased.capacity();

let mut values = erased.as_type::<i32>();
values.push(-1);

assert_eq!(values.as_slice(), &[-1]);
assert_eq!(values.capacity(), capacity);

The guard can also be used to fill a buffer, inspect it, and leave it ready for the next user:

use type_erased_vec_capacity::TypeErasedVec;

let mut buffer = TypeErasedVec::new(Vec::<u32>::with_capacity(4));
{
    let mut values = buffer.as_type::<u32>();
    values.with(|vec| vec.extend_from_slice(&[10, 20, 30]));
    assert_eq!(values.as_slice(), &[10, 20, 30]);
}

assert_eq!(buffer.length(), 3);

For borrowed values, use a scoped guard. The guard cannot outlive the borrowed data, and its element type must not need a destructor:

use type_erased_vec_capacity::TypeErasedVec;

let mut buffer = TypeErasedVec::new(Vec::<&'static str>::new());
let message = String::from("reused buffer");
{
    let mut values = buffer.as_type_scoped::<&str>();
    values.push(&message);
    assert_eq!(values.as_slice(), &["reused buffer"]);
}

When a buffer needs to cross a thread boundary, use the wrapper matching the element bounds:

use type_erased_vec_capacity::SendTypeErasedVec;

let buffer = SendTypeErasedVec::new(Vec::<String>::new());
let handle = std::thread::spawn(move || {
    let mut buffer = buffer;
    let mut values = buffer.as_type::<String>();
    values.push(String::from("work"));
    values.take()
});

assert_eq!(handle.join().unwrap(), vec!["work"]);

The minimum supported Rust version is documented in the package manifest.

License

Copyright (c) 2026 soruh.

Licensed under either the Apache License, Version 2.0 (LICENSE-APACHE) or the MIT license (LICENSE-MIT), at your option.

S
Description
No description provided
Readme
470 KiB
Languages
Rust 100%