Struct scope_lock::Extender

source ·
pub struct Extender<'scope, 'env> { /* private fields */ }

Implementations§

source§

impl<'scope, 'env> Extender<'scope, 'env>

source

pub fn extend_fn<F, I, O>(&'scope self, f: &'scope F) -> ExtendedFn<I, O>
where F: Fn(I) -> O + Sync + 'scope, I: Send + 'scope, O: Send + 'scope,

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());
}
source

pub fn extend_fn_box<F, I, O>( &'scope self, f: F ) -> Box<dyn Fn(I) -> O + Send + Sync>
where F: Fn(I) -> O + Send + Sync + 'scope, I: Send + 'scope, O: Send + 'scope,

source

pub fn extend_fn_mut<F, I, O>( &'scope self, f: &'scope mut F ) -> ExtendedFnMut<I, O>
where F: FnMut(I) -> O + Send + 'scope, I: Send + 'scope, O: Send + 'scope,

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());
}
source

pub fn extend_fn_mut_box<F, I, O>( &'scope self, f: F ) -> Box<dyn FnMut(I) -> O + Send>
where F: FnMut(I) -> O + Send + 'scope, I: Send + 'scope, O: Send + 'scope,

source

pub fn extend_fn_once<F, I, O>( &'scope self, f: RefOnce<'scope, F> ) -> ExtendedFnOnce<I, O>
where F: FnOnce(I) -> O + Send + 'scope, I: Send + 'scope, O: Send + 'scope,

source

pub fn extend_fn_once_box<F, I, O>( &'scope self, f: F ) -> Box<dyn FnOnce(I) -> O + Send>
where F: FnOnce(I) -> O + Send + 'scope, I: Send + 'scope, O: Send + 'scope,

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());
}
source

pub fn extend_future<F>( &'scope self, f: Pin<&'scope mut F> ) -> ExtendedFuture<F::Output>
where F: Future + Send + 'scope, F::Output: Send + 'scope,

source

pub fn extend_future_box<F>( &'scope self, f: F ) -> Pin<Box<dyn Future<Output = F::Output> + Send>>
where F: Future + Send + 'scope, F::Output: Send + 'scope,

Extend lifetime of a future. Use Box::into_pin to pin the future.

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.