Crates.io | ref-portals |
lib.rs | ref-portals |
version | 1.0.0-beta.2 |
source | src |
created_at | 2020-05-24 21:02:37.120885 |
updated_at | 2020-05-27 23:56:42.589704 |
description | Safely use (stack) references outside their original scope |
homepage | |
repository | https://github.com/Tamschi/ref-portals |
max_upload_size | |
id | 245359 |
size | 56,254 |
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
,Sync
or !Sync
values.Please see the documentation for more information about which to choose.
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
ref-portals
strictly follows Semantic Versioning 2.0.0 with the following exceptions: