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
33use parking_lot::RwLock;
34
35mod extended;
36pub mod pointer_like;
37mod ref_once;
38
39pub use extended::func::legacy::{ExtendedFn, ExtendedFnMut, ExtendedFnOnce};
40pub use extended::func::{extend_fn_mut_unchecked, extend_fn_once_unchecked, extend_fn_unchecked};
41pub use extended::future::extend_future_unchecked;
42pub use extended::future::legacy::ExtendedFuture;
43pub use extended::Extender;
44pub use ref_once::RefOnce;
45
46pub fn lock_scope<'env, F, T>(scope: F)
47where
48    F: for<'scope> FnOnce(&'scope Extender<'scope, 'env>) -> T,
49{
50    let rw_lock = RwLock::new(());
51    let extender = Extender::new(&rw_lock);
52    let guard = extender.guard();
53    scope(&extender);
54    drop(guard);
55}
56
57// TODO: zero case test
58// TODO: tests from rust std::thread::scope