Skip to content

get_yield与yield_with废弃后使用什么函数替换呢? #63

Description

@skyfireitdiy

我需要在回调函数中yield,但是无法传递Scope对象到回调函数,会有生命周期的问题,只能通过自由函数 get_yield与yield_with获取和返回数据,如下代码可以正常运行:

use generator::*;

fn main() {
    let mut g: Generator<i32, f64> = Gn::new_opt(4096, || {
        let data = get_yield::<i32>();
        println!("yield: {:?}", data);
        yield_with(3.6);
        3.6f64
    });

    let ret = g.send(123);
    println!("resume: {:?}", ret);
}

// 输出:
// yield: Some(123)
// resume: 3.6

但是get_yield与yield_with函数被标记为废弃,请问是否有替代函数?(co_get_yield和co_yield_with无法使用):

use generator::*;

fn main() {
    let mut g: Generator<i32, f64> = Gn::new_opt(4096, || {
        let data = co_get_yield::<i32>();
        println!("yield: {:?}", data);
        co_yield_with(3.6);
        3.6f64
    });

    let ret = g.send(123);
    println!("resume: {:?}", ret);
}

// 输出:
// yield: None
// thread 'main' panicked at /home/skyfire/.cargo/registry/src/index.crates.io-6f17d22bba15001f/generator-0.8.2/src/yield_.rs:127:32:
// called `Option::unwrap()` on a `None` value
// note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
// 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions