multiptr 0.1.0

Maximally convenient and efficient wrapper around a pointer to an element within a slice
Documentation
  • Coverage
  • 81.82%
    9 out of 11 items documented1 out of 11 items with examples
  • Size
  • Source code size: 7.62 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.32 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • alion02

multiptr

Not production ready. Do not use in production.

crates.io docs.rs

Maximally convenient and efficient wrapper around a pointer to an element within a slice. Allows relative indexing and basic arithmetic. The provided types are akin to Zig many-item pointers, but come with Rust-specific nuances.

In service of convenience, all operations on the types are marked as safe, even though they conceptually aren't. Otherwise, we'd be locked out of using Index and Deref traits and their mut friends. To argue for soundness, we require all creation and proliferation of instances of these types to be treated as unsafe.

Example

let mut slice = [1, 2, 3, 4, 5];

// Create a pointer to the 3rd element (index 2)
let multi_ptr = unsafe { multiptr::MultiPtr::from_slice_index(&slice, 2) };

// Dereference to get the element
assert_eq!(*multi_ptr, 3);

// Index relative to the pointer
assert_eq!(multi_ptr[1], 4);
assert_eq!(multi_ptr[-1], 2);

// Create a mutable pointer to the 2nd element (index 1)
let mut multi_mut = unsafe { multiptr::MultiMut::from_slice_index(&mut slice, 1) };
assert_eq!(*multi_mut, 2);

// Modify the value through the pointer
*multi_mut = 10;
assert_eq!(multi_mut[0], 10);

// Modify through indexing
multi_mut[2] = 30;
assert_eq!(slice[3], 30);