scope_lock/
lib.rs

1//! # Scope lock
2//!
3//! Scope lock allows you to extend lifetime for certain kind of objects like closures to use them
4//! where larger lifetimes are required, like [`std::thread::spawn`]. Start from [`lock_scope`].
5//!
6//! ## Examples
7//!
8//! Using boxes (requires allocation):
9//!
10//! ```
11#![doc = include_str!("../examples/boxed.rs")]
12//! ```
13//!
14//! Using [`RefOnce`]:
15//!
16//! ```
17#![doc = include_str!("../examples/ref_once.rs")]
18//! ```
19#![warn(
20    unsafe_op_in_unsafe_fn,
21    clippy::std_instead_of_core,
22    clippy::alloc_instead_of_core
23)]
24
25// TODO: #![warn(missing_docs)]
26// TODO: trait UnwrapArgTuple
27// TODO: rename
28// TODO: no_std support
29
30// TODO: gate under a feature
31extern crate alloc;
32
33mod extended;
34pub mod pointer_like;
35mod ref_once;
36
37pub use extended::Extender;
38pub use extended::func::{extend_fn_mut_unchecked, extend_fn_once_unchecked, extend_fn_unchecked};
39pub use extended::future::extend_future_unchecked;
40pub use extended::future::legacy::ExtendedFuture;
41pub use ref_once::RefOnce;
42
43pub fn lock_scope<'env, F, T>(scope: F) -> T
44where
45    F: for<'scope> FnOnce(&'scope Extender<'scope, 'env>) -> T,
46{
47    let rw_lock = extended::sync::ReferenceCounter::new();
48    let extender = Extender::new(&rw_lock);
49    let _guard = extender.guard();
50    scope(&extender)
51}
52
53// TODO: zero case test
54// TODO: tests from rust std::thread::scope