sptr

Crates.iosptr
lib.rssptr
version0.3.2
sourcesrc
created_at2022-03-28 18:17:07.005142
updated_at2022-06-27 00:31:20.475047
descriptionsptr: The Strict Provenance Polyfill
homepage
repositoryhttps://github.com/Gankra/sptr
max_upload_size
id558086
size58,344
Ralf Jung (RalfJung)

documentation

README

sptr: The Strict Provenance Polyfill

crates.io Rust CI

This library provides a stable polyfill for Rust's [Strict Provenance] experiment.

Mapping to STD APIs:

This crate "overlays" a bunch of unstable std apis, here are the mappings:

core::ptr (sptr)

core::pointer (sptr::Strict)

  • pub fn addr(self) -> usize;

  • pub fn expose_addr(self) -> usize;

  • pub fn with_addr(self, addr: usize) -> Self;

  • pub fn map_addr(self, f: impl FnOnce(usize) -> usize) -> Self;

NON-STANDARD EXTENSIONS (disabled by default, use at your own risk)

  • sptr::uptr (feature = uptr)

  • sptr::iptr (feature = uptr)

  • sptr::OpaqueFnPtr (feature = opaque_fn)

Applying The Overlay

Swapping between sptr and core::ptr should be as simple as switching between sptr:: and ptr:: for static functions. For methods, you must import sptr::Strict into your module for the extension trait's methods to overlay std. The compiler will (understandably) complain that you are overlaying std, so you will need to also silence that as seen in the following example:

#![allow(unstable_name_collisions)]
use sptr::Strict;

let ptr = sptr::invalid_mut::<u8>(1);
println!("{}", ptr.addr());

By default, this crate will also mark methods on pointers as "deprecated" if they are incompatible with strict_provenance. If you don't want this, set default-features = false in your Cargo.toml.

Rust is the canonical source of definitions for these APIs and semantics, but the docs here will vaguely try to mirror the docs checked into Rust.

Commit count: 41

cargo fmt