ptr-utils
Why This Library?
This library exists primarily for ergonomics. Compare writing pointer operations manually versus using ptr-utils
:
Before (manual pointer casting):
/// # Safety
///
/// - input_ptr must be valid for reads of len bytes
/// - output_ptr must be valid for writes of len bytes
pub unsafe
After (with ptr-utils
):
use ;
/// # Safety
///
/// - input_ptr must be valid for reads of len bytes
/// - output_ptr must be valid for writes of len bytes
pub unsafe
The library eliminates repetitive as *mut T
and as *const T
casts while maintaining the same safety contracts and zero-cost abstractions.
Installation
Add this to your Cargo.toml
:
[]
= "0.1.0"
For no_std
environments:
[]
= { = "0.1.0", = false }
Usage
Unaligned Read/Write Operations
Works with byte pointers as usual:
use ;
let mut buffer = ;
let ptr = buffer.as_mut_ptr;
unsafe
But you can also use it with pointers for other basic types:
use ;
// Works with any pointer type
let mut data: = ;
let typed_ptr: *mut u64 = data.as_mut_ptr;
unsafe
The operations always take byte offsets; for simplicity.
API Overview
UnalignedRead
Trait
Provides unaligned read operations for both *const T
and *mut T
:
read_u8_at
,read_u16_at
,read_u32_at
,read_u64_at
,read_u128_at
,read_usize_at
read_i8_at
,read_i16_at
,read_i32_at
,read_i64_at
,read_i128_at
,read_isize_at
read_f32_at
,read_f64_at
read_bool_at
UnalignedWrite
Trait
Provides unaligned write operations for *mut T
:
write_u8_at
,write_u16_at
,write_u32_at
,write_u64_at
,write_u128_at
,write_usize_at
write_i8_at
,write_i16_at
,write_i32_at
,write_i64_at
,write_i128_at
,write_isize_at
write_f32_at
,write_f64_at
write_bool_at
Safety
This library provides unsafe
functions that require careful use:
- Ensure pointers are valid for the requested read/write operation
- Verify that the memory range
[ptr + offset, ptr + offset + size_of::<T>())
is accessible - For writes, ensure the memory is mutable
- The caller is responsible for preventing data races in multi-threaded contexts
Cargo Features
std
(default): Enables standard library support- Default features can be disabled for
no_std
environments
Development
For information on how to work with this codebase, see README-DEV.MD.
Documentation
Full API documentation is available on docs.rs.
License
Licensed under MIT.
Learn more about Reloaded's general choice of licensing for projects..
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.