1 unstable release

0.1.0 Dec 26, 2022

#1180 in Data structures

MIT/Apache

54KB
1K SLoC

A Vec<T: ?Sized>

A dynamic length collection of unsized elements, akin to std::vec::Vec.

Check the crate-level documentation for more info.


lib.rs:

A Vec<T: ?Sized>

DynVec, a dynamic length collection of unsized elements, akin to std::vec::Vec.

Examples

You can create a vector with DynVec::new:

let vec: DynVec<dyn Debug> = DynVec::new();

or with the dyn_vec! macro:

let vec: DynVec<i32> = dyn_vec![1, 2, 3];
// check the docs for `dyn_vec!` for more info on this syntax
let vec_boxed: DynVec<dyn Debug> = dyn_vec![box:
    Box::new(1) as _,
    Box::new("foo") as _,
    Box::new(true) as _
];
let vec_unsized: DynVec<dyn Debug> = dyn_vec![unsized: 1, "foo", true];
let vec_from_elem: DynVec<i32> = dyn_vec![3; 5];

A vector can be pushed to with DynVec::push:

let mut vec: DynVec<i32> = dyn_vec![];
vec.push(1);
vec.push(2);
vec.push(3);

...and with push_box and push_unsize (push_unsize_stable without the "unstable"feature):

let mut vec: DynVec<dyn Debug> = dyn_vec![];
vec.push_box(Box::new(1));
vec.push_box(Box::new("foo"));
vec.push_box(Box::new(true));

// these closures are only needed for the `_stable` versions
vec.push_unsize_stable(2, |v| v as _);
vec.push_unsize_stable("bar", |v| v as _);
vec.push_unsize_stable(false, |v| v as _);

Finally, a vector can be unsized to another vector (unsize_stable on stable):

let vec: DynVec<i32> = dyn_vec![1, 2, 3];
// vec.push_unsize_stable("foo", |v| v as _); // not yet...
let mut vec: DynVec<dyn Debug> = vec.unsize_stable(|v| v as _);
vec.push_unsize_stable("foo", |v| v as _); // now we can!

To use the _stable variations, one can generally add the argument |v| v as _.

Stability

This crate is currently stable, but lacks some functionality. To enable this functionality, use the "unstable" crate feature, which depends on the following nightly features:

and enables the following functionality:

In addition, the "unstable" feature also enables dependence on the following nightly features in order to conform to strict provenance:

Data Layout

DynVec<T>
┌────┬────┬────┬────┐
│ptr │len │cap │end │
└─┬──┴────┴─┬──┴─┬──┘
  │         │    │
  │         └────┼───────────────────────────────────────────────┐
┌─┘              └───────────────────┐                           │
│                                    │                           │
▼                                    ▼                           ▼
┌────┬────┬─────┬──────────┬───┬─────┬───────────────┬───┬───┬───┐
│pad │elem│pad  │elem      │pad│elem │               │ptr│ptr│ptr│
└────┴────┴─────┴──────────┴───┴─────┴───────────────┴─┬─┴─┬─┴─┬─┘
▲    ▲          ▲              ▲                       │   │   │ ▲
│    │          │              └───────────────────────┘   │   │ │
│    │          └──────────────────────────────────────────┘   │ │
│    └─────────────────────────────────────────────────────────┘ │
│                                                                │
└─────────────── aligned to align_of::<*const T>() ──────────────┘

No runtime deps