pub struct Extender<'scope, 'env> { /* private fields */ }
Implementations§
Source§impl<'scope, 'env> Extender<'scope, 'env>
impl<'scope, 'env> Extender<'scope, 'env>
pub fn fn_once<'extended, P, I, O>( &'scope self, f: P, ) -> impl FnOnce(I) -> O + Send + Sync + 'extended
Sourcepub fn fn_mut<'extended, P, I, O>(
&'scope self,
f: P,
) -> impl FnMut(I) -> O + Send + Sync + 'extended
pub fn fn_mut<'extended, P, I, O>( &'scope self, f: P, ) -> impl FnMut(I) -> O + Send + Sync + 'extended
Examples found in repository?
examples/references.rs (line 25)
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
examples/boxed.rs (lines 17-22)
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 24-32)
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}
Sourcepub fn fn_<'extended, P, I, O>(
&'scope self,
f: P,
) -> impl Fn(I) -> O + Send + Sync + 'extended
pub fn fn_<'extended, P, I, O>( &'scope self, f: P, ) -> impl Fn(I) -> O + Send + Sync + 'extended
Examples found in repository?
examples/references.rs (line 21)
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
examples/boxed.rs (lines 9-13)
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 13-20)
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}
pub fn fn_unsync<'extended, P, I, O>( &'scope self, f: P, ) -> impl Fn(I) -> O + Send + 'extended
Auto Trait Implementations§
impl<'scope, 'env> Freeze for Extender<'scope, 'env>
impl<'scope, 'env> RefUnwindSafe for Extender<'scope, 'env>
impl<'scope, 'env> Send for Extender<'scope, 'env>
impl<'scope, 'env> Sync for Extender<'scope, 'env>
impl<'scope, 'env> Unpin for Extender<'scope, 'env>
impl<'scope, 'env> !UnwindSafe for Extender<'scope, 'env>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more