Crates.io | raw-parts |
lib.rs | raw-parts |
version | |
source | src |
created_at | 2021-11-14 16:49:53.575188 |
updated_at | 2024-12-08 22:20:37.294957 |
description | Ergonomic wrapper around `Vec::from_raw_parts` and `Vec::into_raw_parts`. |
homepage | https://github.com/artichoke/raw-parts |
repository | https://github.com/artichoke/raw-parts |
max_upload_size | |
id | 481781 |
Cargo.toml error: | TOML parse error at line 25, column 1 | 25 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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.
Add this to your Cargo.toml
:
[dependencies]
raw-parts = "2.2.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
.
This crate requires at least Rust 1.76.0. This version can be bumped in minor releases.
raw-parts
is licensed under the MIT License (c) Ryan Lopopolo.