#raw-pointers #ptr #offset #add #methods #non-null-t #post-inc

no-std rawpointer

Extra methods for raw pointers and NonNull<T>. For example .post_inc() and .pre_dec() (c.f. ptr++ and --ptr), offset and add for NonNull<T>, and the function ptrdistance.

3 unstable releases

Uses old Rust 2015

0.2.1 Sep 12, 2019
0.2.0 Sep 8, 2019
0.1.0 Dec 15, 2016

#451 in Rust patterns

Download history 179061/week @ 2023-11-21 201219/week @ 2023-11-28 189487/week @ 2023-12-05 182612/week @ 2023-12-12 145850/week @ 2023-12-19 87183/week @ 2023-12-26 161285/week @ 2024-01-02 182757/week @ 2024-01-09 197948/week @ 2024-01-16 193529/week @ 2024-01-23 209809/week @ 2024-01-30 202861/week @ 2024-02-06 209041/week @ 2024-02-13 221240/week @ 2024-02-20 229646/week @ 2024-02-27 186316/week @ 2024-03-05

882,652 downloads per month
Used in 1,896 crates (10 directly)

MIT/Apache

9KB
140 lines

rawpointer

Please read the API documentation here

build_status

Recent Changes

  • 0.2.0
    • Add support for NonNull<T>
    • Added more documentation and an example
    • Now requires Rust 1.26 or later
  • 0.1.0
    • Initial release

lib.rs:

Rawpointer adds extra utility methods to raw pointers *const T, *mut T and NonNull<T>.

Features include:

  • Strided offsets - .stride_offset(stride, index) make it easy to compute pointer offsets where the index is unsigned and the stride is signed.

  • Offsetting methods in general for NonNull, since it does not have these from libcore

  • Post- and preincrement and post- and predecrement methods

use rawpointer::PointerExt;

unsafe {
    // In this example:
    // Use .post_inc() to iterate and overwrite the first four
    // elements of the array.

    let mut xs = [0; 16];
    let mut ptr = xs.as_mut_ptr();
    let end = ptr.offset(4);
    let mut i = 0;
    while ptr != end {
        *ptr.post_inc() = i;
        i += 1;
    }
    assert_eq!(&xs[..8], &[0, 1, 2, 3, 0, 0, 0, 0]);
}

Safety

See the Rust core::ptr documentation for more information.

Rust Version

This version of the crate requires Rust 1.26 or later

No runtime deps