Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
skyfireitdiy opened this issue Aug 17, 2024 · 2 comments
Open

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

skyfireitdiy opened this issue Aug 17, 2024 · 2 comments

Comments

@skyfireitdiy
Copy link

我需要在回调函数中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
// 
@Xudong-Huang
Copy link
Owner

虽然不推荐直接使用get_yield()yield_with,但是你的用法比较特殊,一般来说使用了generator就不再使用回调了。所以还是得用这两个函数。目前我还没有计划把这两个函数彻底删除,所以你还是可以接着使用的。

@skyfireitdiy
Copy link
Author

好的,非常感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants