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_atread_i8_at,read_i16_at,read_i32_at,read_i64_at,read_i128_at,read_isize_atread_f32_at,read_f64_atread_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_atwrite_i8_at,write_i16_at,write_i32_at,write_i64_at,write_i128_at,write_isize_atwrite_f32_at,write_f64_atwrite_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_stdenvironments
Developer Manual
For step-by-step development guidance, see the Developer Manual.
Contributing
We welcome contributions! See the Contributing Guide for details.
License
Licensed under MIT.