Function lock_scope

Source
pub fn lock_scope<'env, F, T>(scope: F) -> T
where F: for<'scope> FnOnce(&'scope Extender<'scope, 'env>) -> T,
Examples found in repository?
examples/references.rs (lines 19-29)
3fn main() {
4    let mut a = vec![1, 2, 3];
5    let mut x = 0;
6
7    let f1 = &|()| {
8        println!("hello from the first scoped thread");
9        // We can borrow `a` here.
10        dbg!(&a);
11    };
12    let f2 = &mut |()| {
13        println!("hello from the second scoped thread");
14        // We can even mutably borrow `x` here,
15        // because no other threads are using it.
16        x += a[0] + a[2];
17    };
18
19    scope_lock::lock_scope(|e| {
20        thread::spawn({
21            let f = e.fn_(f1);
22            move || f(())
23        });
24        thread::spawn({
25            let mut f = e.fn_mut(f2);
26            move || f(())
27        });
28        println!("hello from the main thread");
29    });
30
31    // After the scope, we can modify and access our variables again:
32    a.push(4);
33    assert_eq!(x, a.len());
34}
More examples
Hide additional examples
examples/boxed.rs (lines 7-26)
3fn main() {
4    let mut a = vec![1, 2, 3];
5    let mut x = 0;
6
7    scope_lock::lock_scope(|e| {
8        thread::spawn({
9            let f = e.fn_(Box::new(|()| {
10                println!("hello from the first scoped thread");
11                // We can borrow `a` here.
12                dbg!(&a);
13            }));
14            move || f(())
15        });
16        thread::spawn({
17            let mut f = e.fn_mut(Box::new(|()| {
18                println!("hello from the second scoped thread");
19                // We can even mutably borrow `x` here,
20                // because no other threads are using it.
21                x += a[0] + a[2];
22            }));
23            move || f(())
24        });
25        println!("hello from the main thread");
26    });
27
28    // After the scope, we can modify and access our variables again:
29    a.push(4);
30    assert_eq!(x, a.len());
31}
examples/ref_once.rs (lines 11-36)
5fn main() {
6    let mut a = vec![1, 2, 3];
7    let mut x = 0;
8
9    let mut slots = (MaybeUninit::uninit(), MaybeUninit::uninit());
10
11    scope_lock::lock_scope(|e| {
12        thread::spawn({
13            let f = e.fn_(RefOnce::new(
14                |()| {
15                    println!("hello from the first scoped thread");
16                    // We can borrow `a` here.
17                    dbg!(&a);
18                },
19                &mut slots.0,
20            ));
21            move || f(())
22        });
23        thread::spawn({
24            let mut f = e.fn_mut(RefOnce::new(
25                |()| {
26                    println!("hello from the second scoped thread");
27                    // We can even mutably borrow `x` here,
28                    // because no other threads are using it.
29                    x += a[0] + a[2];
30                },
31                &mut slots.1,
32            ));
33            move || f(())
34        });
35        println!("hello from the main thread");
36    });
37
38    // After the scope, we can modify and access our variables again:
39    a.push(4);
40    assert_eq!(x, a.len());
41}