ref-portals

Crates.ioref-portals
lib.rsref-portals
version1.0.0-beta.2
sourcesrc
created_at2020-05-24 21:02:37.120885
updated_at2020-05-27 23:56:42.589704
descriptionSafely use (stack) references outside their original scope
homepage
repositoryhttps://github.com/Tamschi/ref-portals
max_upload_size
id245359
size56,254
Tamme Schichler (Tamschi)

documentation

https://docs.rs/ref-portals/1.0.0-beta.2/

README

ref-portals

Latest Version docs.rs

Safely use (stack) references outside their original scope.

This library provides convenient runtime-checked out-of-scope handles that are:

  • !Send + !Sync or (dependently) Send/Sync,
  • immutable or mutable and
  • target Sync or !Sync values.

Please see the documentation for more information about which to choose.

Example

use ref_portals::rc::Anchor;

let x = "Scoped".to_owned();
let anchor = Anchor::new(&x);
let self_owned: Box<dyn Fn() + 'static> = Box::new({
    let portal = anchor.portal();
    move || println!("{}", *portal)
});

self_owned(); // Scoped

Note that dropping anchor before self_owned would still cause a panic here.
You can use weak portals to work around this:

use ref_portals::rc::Anchor;

let x = "Scoped".to_owned();
let anchor = Anchor::new(&x);
let eternal: &'static dyn Fn() = Box::leak(Box::new({
    let weak_portal = anchor.weak_portal();
    move || println!(
        "{}",
        // Panics iff the anchor has been dropped.
        *weak_portal.upgrade(),
    )
}));

eternal(); // Scoped

Versioning

ref-portals strictly follows Semantic Versioning 2.0.0 with the following exceptions:

  • The minor version will not reset to 0 on major version changes.
    Consider it the global feature level.
  • The patch version will not reset to 0 on major or minor version changes.
    Consider it the global patch level.
Commit count: 42

cargo fmt