#vec #unsafe #raw-pointers #ffi #vector

no-std raw-parts

Ergonomic wrapper around Vec::from_raw_parts and Vec::into_raw_parts

7 stable releases

2.0.0 May 31, 2023
1.1.2 Aug 8, 2022
1.1.1 Dec 12, 2021
1.0.2 Nov 19, 2021
1.0.1 Nov 14, 2021

#324 in Rust patterns

Download history 162/week @ 2023-12-04 185/week @ 2023-12-11 258/week @ 2023-12-18 409/week @ 2023-12-25 237/week @ 2024-01-01 316/week @ 2024-01-08 214/week @ 2024-01-15 249/week @ 2024-01-22 403/week @ 2024-01-29 274/week @ 2024-02-05 296/week @ 2024-02-12 472/week @ 2024-02-19 688/week @ 2024-02-26 287/week @ 2024-03-04 484/week @ 2024-03-11 338/week @ 2024-03-18

1,817 downloads per month
Used in 5 crates (3 directly)

MIT license

24KB
267 lines

raw-parts

GitHub Actions Code Coverage Discord Twitter
Crate API API trunk

A wrapper around the decomposed parts of a Vec<T>.

This struct contains the Vec's internal pointer, length, and allocated capacity.

RawParts makes Vec::from_raw_parts and Vec::into_raw_parts easier to use by giving names to the returned values. This prevents errors from mixing up the two usize values of length and capacity.

Usage

Add this to your Cargo.toml:

[dependencies]
raw-parts = "2.0.0"

Then decompose Vec<T>s like:

use raw_parts::RawParts;

let v: Vec<i32> = vec![-1, 0, 1];

let RawParts { ptr, length, capacity } = RawParts::from_vec(v);

let rebuilt = unsafe {
    // We can now make changes to the components, such as
    // transmuting the raw pointer to a compatible type.
    let ptr = ptr as *mut u32;
    let raw_parts = RawParts { ptr, length, capacity };

    raw_parts.into_vec()
};
assert_eq!(rebuilt, [4294967295, 0, 1]);

no_std

raw-parts is no_std compatible with a required dependency on alloc.

Minimum Supported Rust Version

This crate requires at least Rust 1.56.0. This version can be bumped in minor releases.

License

raw-parts is licensed under the MIT License (c) Ryan Lopopolo.

No runtime deps