| Crates.io | unsized-vec |
| lib.rs | unsized-vec |
| version | 0.0.2-alpha.13 |
| created_at | 2022-10-08 21:49:23.654468+00 |
| updated_at | 2025-02-23 20:19:28.303772+00 |
| description | Like Vec, but for unsized values |
| homepage | |
| repository | https://github.com/Jules-Bertholet/unsized-vec/ |
| max_upload_size | |
| id | 683731 |
| size | 192,358 |
unsized-vecSay goodbye to Vec<Box<dyn Any>>! Cut down on your heap allocations.
UnsizedVec<T> is like Vec<T>, but T can be ?Sized.
Vec API.alloc::vec::Vec for major operations(indexing, push, pop, insert, remove).
T's alignment is not known at compile time (e.g. T is a trait object), this rule has one expection,
explained in the crate docs.T: Sized, UnsizedVec<T> compiles to a newtype around alloc::vec::Vec, and can be trivially converted to/from it.T, there are two heap allocations: one for the elements, and one for the pointer metadata.#[no_std] (but requires alloc).T.#![allow(internal_features)] // for `unsized_fn_params`
#![feature(unsized_fn_params)]
use core::fmt::Debug;
use emplacable::box_new_with;
use unsized_vec::{unsize_vec, UnsizedVec};
fn main() {
let mut vec: UnsizedVec<dyn Debug> = unsize_vec![27.53_f32, "oh the places we'll go", Some(())];
for traitobj in &vec {
dbg!(traitobj);
};
assert_eq!(vec.len(), 3);
let maybe_popped: Option<Box<dyn Debug>> = vec.pop_into().map(box_new_with);
let popped = maybe_popped.unwrap();
dbg!(&*popped);
assert_eq!(vec.len(), 2);
}
unsized-vec is distributed under the terms of both the MIT license and the Apache License (Version 2.0).
See LICENSE-APACHE and LICENSE-MIT for details.