raw-parts

Crates.ioraw-parts
lib.rsraw-parts
version2.1.0
sourcesrc
created_at2021-11-14 16:49:53.575188
updated_at2024-07-11 03:02:43.959821
descriptionErgonomic wrapper around `Vec::from_raw_parts` and `Vec::into_raw_parts`.
homepagehttps://github.com/artichoke/raw-parts
repositoryhttps://github.com/artichoke/raw-parts
max_upload_size
id481781
size24,504
crates.io publishers (github:artichoke:crates-io-publishers)

documentation

https://docs.rs/raw-parts

README

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.1.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.

Commit count: 257

cargo fmt