unsafe-binders 0.0.1

Stable rust polyfill of `unsafe<'lt> …` binders
Documentation
  • Coverage
  • 100%
    9 out of 9 items documented4 out of 4 items with examples
  • Size
  • Source code size: 47.19 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.31 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • danielhenrymantilla

::unsafe-binders

Stable rust polyfill of unsafe<'lt> … binders.

Repository Latest version Documentation MSRV License CI no_std compatible


Conceptually, unsafe binders are the way to express the 'unsafe/'never/'expired/'dangling lifetime used in certain unsafe constructs (e.g., self-referential data), in a way less unsoundness-prone way as using 'static instead would be:

  • by virtue of requiring unsafe to access the data,

  • and it not performing liveness or (lack-of-)aliasing assertions (courtesy of ::maybe-dangling, like 'static-infected types may accidentally do.

Example

use ::unsafe_binders::{DropMarker, Unsafe};

pub struct SelfReferential {
    full_name: String,
    first_name: Unsafe![DropMarker::NoDropGlue, <'this> &'this str],
                                           // using lifetime-elision shorthand syntax:
    last_name: Unsafe![DropMarker::NoDropGlue, &str],
}

impl SelfReferential {
    pub fn new(full_name: String) -> Option<Self> {
        let (first_name, last_name) = full_name.split_once(" ")?;
        // erase the `&'full_name` lifetime:
        let first_name: Unsafe![_, &str] = Unsafe::wrap_binder_copy(first_name);
                               // shorthand syntax
        let last_name: Unsafe![&str] = Unsafe::wrap_binder_copy(last_name);
        Some(Self {
            full_name,
            first_name,
            last_name,
        })
    }

    pub fn first_name<'r>(&'r self) -> &'r str {
        unsafe {
            self.first_name.unwrap_binder_ref::<'_, 'r>()
        }
    }

    pub fn last_name<'r>(&'r self) -> &'r str {
        unsafe {
            self.last_name.unwrap_binder_ref::<'_, 'r>()
        }
    }
}