with_encapsulated

Function with_encapsulated 

Source
pub fn with_encapsulated<'env, E, F, T>(data: E, f: F) -> T
where E: Encapsulate<'env>, F: FnOnce(E::Encapsulated) -> T,
Expand description

Create a scope for sending references to another thread.

Encapsulate one or more references from data inside of Capsule or CapsuleMut which can be safely passed to another thread (for example, through a channel). The f function is then called with the capsule(s), and then this function will wait until all created capsules have been dropped, even if they are eventually dropped by another thread.

Note that this function will block unless the capsules are dropped by f on the same thread.

data is any type that implements the Encapsulate trait. The most significant implementations of this trait includes:

  • &T which is convertend into a Capsule<T>
  • &mut T which is converted into a CapsuleMut<T>
  • A tuple of up to 8 elements, each of which implements Encapsulate, which is converted into a tuple where each item is converted into the respective Encapsulated type.
  • [T; N] where T implements Encapsulate.
  • encapsulate::Gen which will allow you to directly use the Scope to convert references to capsules inside a function or (more likely) closure.