-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new Extender methods which don't use dynamic dispatch
Methods fn_, fn_mut, fn_once, fn_unsync, future, future_unpin work on any pointer type you can give them, including Box, references, RefOnce, Arc, Rc.
- Loading branch information
1 parent
5a6c897
commit 5bc5e01
Showing
8 changed files
with
272 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::{mem::MaybeUninit, thread}; | ||
|
||
use scope_lock::RefOnce; | ||
|
||
fn main() { | ||
let mut a = vec![1, 2, 3]; | ||
let mut x = 0; | ||
|
||
let mut slots = (MaybeUninit::uninit(), MaybeUninit::uninit()); | ||
|
||
scope_lock::lock_scope(|e| { | ||
thread::spawn({ | ||
let f = e.fn_(RefOnce::new( | ||
|()| { | ||
println!("hello from the first scoped thread"); | ||
// We can borrow `a` here. | ||
dbg!(&a); | ||
}, | ||
&mut slots.0, | ||
)); | ||
move || f(()) | ||
}); | ||
thread::spawn({ | ||
let mut f = e.fn_mut(RefOnce::new( | ||
|()| { | ||
println!("hello from the second scoped thread"); | ||
// We can even mutably borrow `x` here, | ||
// because no other threads are using it. | ||
x += a[0] + a[2]; | ||
}, | ||
&mut slots.1, | ||
)); | ||
move || f(()) | ||
}); | ||
println!("hello from the main thread"); | ||
}); | ||
|
||
// After the scope, we can modify and access our variables again: | ||
a.push(4); | ||
assert_eq!(x, a.len()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.