1 unstable release
0.0.1 | May 12, 2023 |
---|
#77 in #smart-pointers
18KB
310 lines
inscope
This crate implements an Option
-like type that can be used to represent
values that are "in scope" or "out of scope".
This crate is currently unstable and is not recommended for use in production.
Why?
This crate was created to solve a problem that I encountered while working on
various other Panthios utilities. I needed a way to represent values that were
deletable in other threads, and the deletion should be reflected in the
original thread. This could be solved with an Arc<Mutex<T>>
, but that is
overkill for this specific use case. Instead, the mutable-constant
crate can be used to create a mutable Option
that can be set to None
from
another thread.
Example
use inscope::Scope;
let scope = Scope::new(42);
assert_eq!(*scope, Some(42));
// This requires unsafe because defiant
// `Mc` mutations are unsafe
unsafe {
scope.delete();
}
assert_eq!(*scope, None);
lib.rs
:
inscope
This crate provides a smart pointer that allows dropping a value while it is still borrowed. In most cases, this is completely useless. However, it can be useful when a resource is deleted but still needs to be used while other threads process the deletion.
Deleting a value while it is still borrowed is an
unsafe operation. You will need to use unsafe
blocks
to use this crate effectively.
Example
use inscope::Scope;
let scope = Scope::new(42);
assert_eq!(*scope, Some(42));
unsafe {
scope.delete();
}
assert_eq!(*scope, None);