Rust 无法在mpsc::channel:bool:Future上使用Stream::take_

Rust 无法在mpsc::channel:bool:Future上使用Stream::take_,rust,future,rust-tokio,Rust,Future,Rust Tokio,我想在一个线程中运行一个事件循环,并处理来自UDP套接字的数据,直到另一个线程发出停止工作的信号 这对我来说是一项困难的任务,因此我想从一项更简单的任务开始: 一个线程启动事件循环并等待另一个线程发出结束信号: use futures::{future, future::Future, stream::Stream, sync::mpsc}; use std::{io, io::BufRead, thread}; fn main() { let (mut tx, rx) = mpsc:

我想在一个线程中运行一个事件循环,并处理来自UDP套接字的数据,直到另一个线程发出停止工作的信号

这对我来说是一项困难的任务,因此我想从一项更简单的任务开始: 一个线程启动事件循环并等待另一个线程发出结束信号:

use futures::{future, future::Future, stream::Stream, sync::mpsc};
use std::{io, io::BufRead, thread};

fn main() {
    let (mut tx, rx) = mpsc::channel::<bool>(1);

    let thr = thread::spawn(|| {
        let mut runtime = tokio::runtime::current_thread::Runtime::new().unwrap();
        runtime.spawn(
            future::lazy(|| {
                println!("event loop started");
                Ok(())
            })
            .and_then(rx.take_while(|x| *x == true).into_future()),
        );

        runtime.run()
    });

    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        println!("{}", line);
        if line == "exit" {
            tx.try_send(false).unwrap();
            break;
        }
    }
    thr.join().unwrap().unwrap();
}
使用未来::{future,future::future,stream::stream,sync::mpsc};
使用std::{io,io::BufRead,thread};
fn main(){
let(mut-tx,rx)=mpsc::信道::(1);
让thr=thread::spawn(| |{
让mut runtime=tokio::runtime::current_thread::runtime::new().unwrap();
runtime.spawn(
未来:懒惰{
println!(“事件循环已启动”);
好(())
})
,然后(rx.take_while(|x |*x==true).into_future()),
);
runtime.run()
});
设stdin=io::stdin();
对于stdin.lock().lines()中的行{
让line=line.unwrap();
println!(“{}”,行);
如果行==“退出”{
tx.try_send(false).unwrap();
打破
}
}
thr.join().unwrap().unwrap();
}
此代码不编译:

error[E0277]:特性绑定'bool:futures::future::future'不满足
-->src/main.rs:14:26
|
14 |和|然后(rx.take | u while(| x |*x==true).进入|未来()),
|^^^^^^^^^^^未为`bool'实现特性`futures::future::future``
|
=注:由于'bool'的'futures::future::intofurture'impl中的要求,因此需要`
错误[E0599]:在当前作用域中找不到类型为“futures::stream::take_while::TakeWhile”的名为“into_future”的方法
-->src/main.rs:14:53
|
14 |和|然后(rx.take | u while(| x |*x==true).进入|未来()),
|                                                     ^^^^^^^^^^^
|
=注意:存在“进入未来”方法,但不满足以下特征界限:
`futures::stream::take_while::TakeWhile:futures::stream::stream`
`&mut futures::stream::take_while::TakeWhile:futures::stream::stream`

如何修复编译错误?

阅读并理解您试图使用的方法的文档和函数签名:

fn take_while<P, R>(self, pred: P) -> TakeWhile<Self, P, R>
where
    P: FnMut(&Self::Item) -> R,
    R: IntoFuture<Item = bool, Error = Self::Error>,
    Self: Sized, 
另见:


但我的问题也在于加入
future::lazy
rx.take_while

这与你所问的问题无关。再次,我们看一下文档,这次是为了:


真的,你应该只使用一个;这要简单得多:

use futures::{
    future::{self, Future},
    sync::oneshot,
};
use std::thread;

fn main() {
    let (tx, rx) = oneshot::channel();

    let thr = thread::spawn(|| {
        let mut runtime = tokio::runtime::current_thread::Runtime::new().unwrap();

        runtime.spawn({
            future::lazy(|| {
                println!("event loop started");
                Ok(())
            })
            .and_then(|_| rx.map_err(drop))
        });

        runtime.run()
    });

    let lines = ["hello", "goodbye", "exit"];
    for &line in &lines {
        if line == "exit" {
            tx.send(()).unwrap();
            break;
        }
    }

    thr.join().unwrap().unwrap();
}

作为每个人避免的另一种选择:
rx。在(
也起作用。但我的问题也在于加入
future::lazy
rx。在更新用户1244932时采取行动。@ÖmerErden如果答案仍然正确,请不要觉得需要删除答案;你在我之前就意识到OP想要链接和
进入未来。总有可能OP认为你的答案更符合他们的代码。
fn and_then<F, B>(self, f: F) -> AndThen<Self, B, F>
where
    F: FnOnce(Self::Item) -> B,
    B: IntoFuture<Error = Self::Error>,
    Self: Sized, 
runtime.spawn({
    future::lazy(|| {
        println!("event loop started");
        Ok(())
    })
    .and_then(|_| {
        rx.take_while(|&x| future::ok(x))
            .into_future()
            .map(drop)
            .map_err(drop)
    })
    .map(drop)
});
use futures::{
    future::{self, Future},
    sync::oneshot,
};
use std::thread;

fn main() {
    let (tx, rx) = oneshot::channel();

    let thr = thread::spawn(|| {
        let mut runtime = tokio::runtime::current_thread::Runtime::new().unwrap();

        runtime.spawn({
            future::lazy(|| {
                println!("event loop started");
                Ok(())
            })
            .and_then(|_| rx.map_err(drop))
        });

        runtime.run()
    });

    let lines = ["hello", "goodbye", "exit"];
    for &line in &lines {
        if line == "exit" {
            tx.send(()).unwrap();
            break;
        }
    }

    thr.join().unwrap().unwrap();
}