+7
-7
lifetime-guard/Cargo.toml
+7
-7
lifetime-guard/Cargo.toml
···
1
1
[package]
2
2
name = "lifetime-guard"
3
-
description = "create weak/strong reference pairs to interior mutable data on the stack"
4
-
keywords = [ "weak_ptr", "value_guard", "no_std" ]
5
-
categories = [ "data-structures", "no-std::no-alloc" ]
6
3
version.workspace = true
7
-
rust-version.workspace = true
8
-
edition.workspace = true
9
-
license.workspace = true
10
4
authors.workspace = true
11
-
repository.workspace = true
5
+
edition.workspace = true
6
+
rust-version.workspace = true
7
+
description = "create weak/strong reference pairs to interior mutable data on the stack"
12
8
homepage.workspace = true
9
+
repository.workspace = true
10
+
license.workspace = true
11
+
keywords = [ "weak_ptr", "value_guard", "no_std" ]
12
+
categories = [ "data-structures", "no-std::no-alloc" ]
13
13
14
14
[dependencies]
+44
lifetime-guard/README.md
+44
lifetime-guard/README.md
···
1
+
# Lifetime Guard
2
+
3
+
`lifetime-guard` provides `ValueGuard` and `RefGuard` structs to allow for
4
+
weak references to interior mutable values, similar to a singular pair of
5
+
`Rc` and `Weak`, but without heap allocation.
6
+
7
+
## Example Usage
8
+
9
+
```rust
10
+
use std::pin;
11
+
use lifetime_guard::{ ValueGuard, RefGuard };
12
+
13
+
let weak = pin::pin!(RefGuard::new());
14
+
{
15
+
let strong = pin::pin!(ValueGuard::new(0));
16
+
strong.as_ref().registration().register(weak.as_ref());
17
+
18
+
assert_eq!(strong.get(), 0);
19
+
assert_eq!(weak.get(), Some(0));
20
+
21
+
strong.as_ref().set(1);
22
+
assert_eq!(strong.get(), 1);
23
+
assert_eq!(weak.get(), Some(1));
24
+
}
25
+
assert_eq!(weak.get(), None);
26
+
```
27
+
28
+
# Safety
29
+
30
+
You *may not* leak any instance of either `ValueGuard` or `RefGuard` to the
31
+
stack using `mem::forget()` or any other mechanism that causes thier
32
+
contents to be overwritten without `Drop::drop()` running.
33
+
Doing so creates unsoundness that likely will lead to dereferencing a null
34
+
pointer.
35
+
36
+
Doing so creates unsoundness that likely will lead to dereferencing a null
37
+
pointer. See the
38
+
[Forget marker trait](https://github.com/rust-lang/rfcs/pull/3782) rfc for
39
+
progress on making interfaces that rely on not being leaked sound.
40
+
41
+
Note that it is sound to leak `ValueGuard` and `RefGuard` to the heap using
42
+
methods including `Box::leak()` because heap allocated data will never be
43
+
overwritten if it is never freed.
44
+
+1
-43
lifetime-guard/src/lib.rs
+1
-43
lifetime-guard/src/lib.rs
···
1
-
//! # Lifetime Guard
2
-
//!
3
-
//! `lifetime-guard` provides `ValueGuard` and `RefGuard` structs to allow for
4
-
//! weak references to interior mutable values, similar to a singular pair of
5
-
//! `Rc` and `Weak`, but without heap allocation.
6
-
//!
7
-
//! ## Example Usage
8
-
//!
9
-
//! ```rust
10
-
//! use std::pin;
11
-
//! use lifetime_guard::{ ValueGuard, RefGuard };
12
-
//!
13
-
//! let weak = pin::pin!(RefGuard::new());
14
-
//! {
15
-
//! let strong = pin::pin!(ValueGuard::new(0));
16
-
//! strong.as_ref().registration().register(weak.as_ref());
17
-
//!
18
-
//! assert_eq!(strong.get(), 0);
19
-
//! assert_eq!(weak.get(), Some(0));
20
-
//!
21
-
//! strong.as_ref().set(1);
22
-
//! assert_eq!(strong.get(), 1);
23
-
//! assert_eq!(weak.get(), Some(1));
24
-
//! }
25
-
//! assert_eq!(weak.get(), None);
26
-
//! ```
27
-
//!
28
-
//! # Safety
29
-
//!
30
-
//! You *may not* leak any instance of either `ValueGuard` or `RefGuard` to the
31
-
//! stack using `mem::forget()` or any other mechanism that causes thier
32
-
//! contents to be overwritten without `Drop::drop()` running.
33
-
//! Doing so creates unsoundness that likely will lead to dereferencing a null
34
-
//! pointer.
35
-
//!
36
-
//! Doing so creates unsoundness that likely will lead to dereferencing a null
37
-
//! pointer. See the
38
-
//! [Forget marker trait](https://github.com/rust-lang/rfcs/pull/3782) rfc for
39
-
//! progress on making interfaces that rely on not being leaked sound.
40
-
//!
41
-
//! Note that it is sound to leak `ValueGuard` and `RefGuard` to the heap using
42
-
//! methods including `Box::leak()` because heap allocated data will never be
43
-
//! overwritten if it is never freed.
1
+
#![doc = include_str!("../README.md")]
44
2
45
3
use std::{cell::Cell, marker::PhantomPinned, pin::Pin, ptr::NonNull};
46
4