https://github.com/artichoke/raw-parts
🪣 Types for a `Vec`'s raw parts
https://github.com/artichoke/raw-parts
artichoke ffi no-std pointers rust rust-crate vector
Last synced: 4 months ago
JSON representation
🪣 Types for a `Vec`'s raw parts
- Host: GitHub
- URL: https://github.com/artichoke/raw-parts
- Owner: artichoke
- License: mit
- Created: 2021-11-14T15:17:22.000Z (almost 4 years ago)
- Default Branch: trunk
- Last Pushed: 2025-04-01T00:18:18.000Z (7 months ago)
- Last Synced: 2025-04-01T01:26:37.736Z (7 months ago)
- Topics: artichoke, ffi, no-std, pointers, rust, rust-crate, vector
- Language: Rust
- Homepage: https://crates.io/crates/raw-parts
- Size: 1.79 MB
- Stars: 7
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# raw-parts
[](https://github.com/artichoke/raw-parts/actions)
[](https://codecov.artichokeruby.org/raw-parts/index.html)
[](https://discord.gg/QCe2tp2)
[](https://twitter.com/artichokeruby)
[](https://crates.io/crates/raw-parts)
[](https://docs.rs/raw-parts)
[](https://artichoke.github.io/raw-parts/raw_parts/)
A wrapper around the decomposed parts of a `Vec`.
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`:
```toml
[dependencies]
raw-parts = "2.2.0"
```
Then decompose `Vec`s like:
```rust
use raw_parts::RawParts;
let v: Vec = 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.76.0. This version can be bumped in minor
releases.
## License
`raw-parts` is licensed under the [MIT License](LICENSE) (c) Ryan Lopopolo.
[`vec::from_raw_parts`]:
https://doc.rust-lang.org/alloc/vec/struct.Vec.html#method.from_raw_parts
[`vec::into_raw_parts`]:
https://doc.rust-lang.org/alloc/vec/struct.Vec.html#method.into_raw_parts
[`alloc`]: https://doc.rust-lang.org/alloc/