Struct Extender

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

Implementations§

Source§

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

Source

pub fn fn_once<'extended, P, I, O>( &'scope self, f: P, ) -> impl FnOnce(I) -> O + Send + Sync + 'extended
where P: PointerIntoInner + Send, P::Pointee: FnOnce(I) -> O, I: Send + 'extended, O: Send + 'extended, 'extended: 'scope,

Source

pub fn fn_mut<'extended, P, I, O>( &'scope self, f: P, ) -> impl FnMut(I) -> O + Send + Sync + 'extended
where P: PointerDerefMut + Send, P::Pointee: FnMut(I) -> O, I: Send + 'extended, O: Send + 'extended, 'extended: 'scope,

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
Hide additional 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}
Source

pub fn fn_<'extended, P, I, O>( &'scope self, f: P, ) -> impl Fn(I) -> O + Send + Sync + 'extended
where P: PointerDeref + Send, P::Pointee: Fn(I) -> O + Sync, I: Send + 'extended, O: Send + 'extended, 'extended: 'scope,

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
Hide additional 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}
Source

pub fn fn_unsync<'extended, P, I, O>( &'scope self, f: P, ) -> impl Fn(I) -> O + Send + 'extended
where P: PointerDeref + Send, P::Pointee: Fn(I) -> O, I: Send + 'extended, O: Send + 'extended, 'extended: 'scope,

Source§

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

Source

pub fn future<'extended, P, O>( &'scope self, f: P, ) -> impl Future<Output = O> + Send + Sync + 'extended
where P: PointerPinUnforgotten + Send + 'scope, P::Pointee: Future<Output = O>, O: Send + 'extended, 'extended: 'scope,

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>,

Source§

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>,

Source§

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.