Struct scope_lock::Extender
source · pub struct Extender<'scope, 'env> { /* private fields */ }
Implementations§
source§impl<'scope, 'env> Extender<'scope, 'env>
impl<'scope, 'env> Extender<'scope, 'env>
sourcepub fn extend_fn<F, I, O>(&'scope self, f: &'scope F) -> ExtendedFn<I, O>
pub fn extend_fn<F, I, O>(&'scope self, f: &'scope F) -> ExtendedFn<I, O>
Examples found in repository?
examples/references.rs (line 21)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
fn main() {
let mut a = vec![1, 2, 3];
let mut x = 0;
let f1 = &|()| {
println!("hello from the first scoped thread");
// We can borrow `a` here.
dbg!(&a);
};
let f2 = &mut |()| {
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];
};
scope_lock::lock_scope(|e| {
thread::spawn({
let f = e.extend_fn(f1);
move || f.call(())
});
thread::spawn({
let mut f = e.extend_fn_mut(f2);
move || f.call(())
});
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());
}
pub fn extend_fn_box<F, I, O>( &'scope self, f: F ) -> Box<dyn Fn(I) -> O + Send + Sync>
sourcepub fn extend_fn_mut<F, I, O>(
&'scope self,
f: &'scope mut F
) -> ExtendedFnMut<I, O>
pub fn extend_fn_mut<F, I, O>( &'scope self, f: &'scope mut F ) -> ExtendedFnMut<I, O>
Examples found in repository?
examples/references.rs (line 25)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
fn main() {
let mut a = vec![1, 2, 3];
let mut x = 0;
let f1 = &|()| {
println!("hello from the first scoped thread");
// We can borrow `a` here.
dbg!(&a);
};
let f2 = &mut |()| {
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];
};
scope_lock::lock_scope(|e| {
thread::spawn({
let f = e.extend_fn(f1);
move || f.call(())
});
thread::spawn({
let mut f = e.extend_fn_mut(f2);
move || f.call(())
});
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());
}
pub fn extend_fn_mut_box<F, I, O>( &'scope self, f: F ) -> Box<dyn FnMut(I) -> O + Send>
pub fn extend_fn_once<F, I, O>( &'scope self, f: RefOnce<'scope, F> ) -> ExtendedFnOnce<I, O>
sourcepub fn extend_fn_once_box<F, I, O>(
&'scope self,
f: F
) -> Box<dyn FnOnce(I) -> O + Send>
pub fn extend_fn_once_box<F, I, O>( &'scope self, f: F ) -> Box<dyn FnOnce(I) -> O + Send>
Examples found in repository?
examples/boxed.rs (lines 9-13)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
fn main() {
let mut a = vec![1, 2, 3];
let mut x = 0;
scope_lock::lock_scope(|e| {
thread::spawn({
let f = e.extend_fn_once_box(|()| {
println!("hello from the first scoped thread");
// We can borrow `a` here.
dbg!(&a);
});
move || f(())
});
thread::spawn({
let f = e.extend_fn_once_box(|()| {
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];
});
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());
}
pub fn extend_future<F>( &'scope self, f: Pin<&'scope mut F> ) -> ExtendedFuture<F::Output> ⓘ
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