2

I'm writing a library that uses generators to hold continuations. Sometimes I want to pass a closure with no suspension points, or no yields, but the compiler complains that the closure doesn't implement the Generator trait.

I want to compile the following code without adding a yield to the closure; how can I let the compiler treat the closure as a generator?

#![feature(generators, generator_trait)]

use std::ops::Generator;

fn library_func(mut g: Box<dyn Generator<Yield = (), Return = ()>>) {
    let x = unsafe { g.resume() };
    println!("{:?}", x);
}

fn main() {
    // a closure without yield
    let x = Box::new(|| {
        // uncommenting this line makes it compile, but changes the behavior
        // yield ();
    });

    library_func(x);
}
error[E0277]: the trait bound `[closure@src/main.rs:12:22: 15:6]: std::ops::Generator` is not satisfied
  --> src/main.rs:17:18
   |
17 |     library_func(x);
   |                  ^ the trait `std::ops::Generator` is not implemented for `[closure@src/main.rs:12:22: 15:6]`
   |
   = note: required for the cast to the object type `dyn std::ops::Generator<Yield=(), Return=()>`

1 Answer 1

4

A closure isn't a generator, so the compiler can't really treat it as one. It is unclear whether the generator you wish to implement is supposed to return or yield the return value of the function; assuming you want the former, you can use a yield statement after a return statement to create a generator that does not yield:

let x = Box::new(|| {
    return;
    yield;
});

If you need this frequently, you can also wrap this in a function:

fn into_generator<F, T>(f: F) -> impl Generator<Yield = (), Return = T>
where
    F: FnOnce() -> T,
{
    #[allow(unreachable_code)]
    || {
        return f();
        yield;
    }
}

(Full code on the playground)

5
  • Is it possible to define a conversion trait like IntoIterator that automatically calls into_generator for functions? I tried this but encountered conflicting implementations error: play.rust-lang.org/… I understand there is a possibility of conflict, but I couldn't find a workaround.
    – pandaman
    Dec 13, 2018 at 11:13
  • @pandaman I can't see a good way of implementing a trait for both all generators and all closures. You generally get only one blanket implementation, since there is no way to exclude types that implement both traits. An explicit conversion function doesn't seem too shabby as a solution, though I don't understand your use case. Dec 13, 2018 at 12:32
  • I'm writing an Algebraic Effects library for Rust, and the actual occurrence of the closures are inside a macro. So it would be good if I can handle both generators and functions in a syntactically equivalent manner.
    – pandaman
    Dec 13, 2018 at 13:43
  • @pandaman It is sometiems possible to resolve conflicts between implementations using "sealed" traits that are private to the crate, so they can't be implemented by downstream crates – see for example the discussion on this question and the description in the Rust API guidelines. However, I don't think the pattern can be applied here, since we can't really prevent downstream crates from implementing both… Dec 13, 2018 at 14:17
  • FnOnce and Generator for the same type. Dec 13, 2018 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.