Skip to main content

Scope

Struct Scope 

Source
pub struct Scope<'scope, 'env: 'scope> { /* private fields */ }
Expand description

A scope for spawning goroutines with bounded lifetimes.

Obtained by calling scope. Every goroutine spawned via Scope::go is guaranteed to finish before scope returns, which allows closures to borrow data with lifetime 'env without requiring 'static.

The type is invariant over both 'scope and 'env to prevent the compiler from shrinking either lifetime in ways that could unsafely extend a goroutine’s ability to access stack data.

Implementations§

Source§

impl<'scope, 'env: 'scope> Scope<'scope, 'env>

Source

pub fn go<F, R>(&'scope self, f: F) -> ScopedJoinHandle<'scope, R>
where F: FnOnce() -> R + Send + 'scope, R: Send + 'static,

Spawn a goroutine in this scope.

The closure may borrow any data with lifetime 'env or longer — i.e. anything that was alive when scope was called.

Returns a ScopedJoinHandle for optional early joining. If you do not need the return value, simply drop the handle; the goroutine still runs to completion before the surrounding scope returns.

Auto Trait Implementations§

§

impl<'scope, 'env> Freeze for Scope<'scope, 'env>

§

impl<'scope, 'env> RefUnwindSafe for Scope<'scope, 'env>

§

impl<'scope, 'env> Send for Scope<'scope, 'env>

§

impl<'scope, 'env> Sync for Scope<'scope, 'env>

§

impl<'scope, 'env> Unpin for Scope<'scope, 'env>

§

impl<'scope, 'env> UnsafeUnpin for Scope<'scope, 'env>

§

impl<'scope, 'env> !UnwindSafe for Scope<'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.