Function mononym::with_seed[][src]

pub fn with_seed<R>(cont: impl for<'name> FnOnce(Seed<Life<'name>>) -> R) -> R
Expand description

Provides the continuation closure with a unique Seed with a unique lifetime 'name and a unique name Life<'name>.

This is achieved using higher-ranked trait bounds by requiring the continuation closure to work for all lifetime 'name.

It is safe to have multiple nested calls to with_seed, as each call will generate new seed type with a unique 'name lifetime. For example, the following code should fail to compile:

fn same<T>(_: T, _: T) {}
with_seed(|seed1| {
  with_seed(|seed2| {
    same(seed1, seed2); // error
    same(seed1.new_named(1), seed2.new_named(1)); // error
  });
});

The function allows the continuation closure to return any concrete type R, provided that the return type R does not depend on the provided Seed type in some way. This means that types such as Name, Named, and Seed cannot be used as a return value, as Rust consider that as allowing the lifetime 'name to escape. For example, the following code should fail to compile:

let res = with_seed(|seed| { seed.new_named(42).into_value() }); // ok
let res = with_seed(|seed| { seed }); // error
let res = with_seed(|seed| { seed.new_name() }); // error
let res = with_seed(|seed| { seed.new_named(42) }); // error