方法for_each存在,但使用StreamDeckSocket时无法满足特征范围

时间:2019-08-18 04:36:02

标签: rust

我正在尝试使用streamdeck-rs板条箱,但在使example code正常工作时遇到了麻烦。

我的实现基本上是给出的确切示例:

use futures::future::Future;
use std::env;
use streamdeck_rs::registration::*;
use streamdeck_rs::socket::StreamDeckSocket;

//the fields of these structs doesn't matter
struct GlobalSettings {}
struct ActionSettings {}
struct MessageToSd {}
struct MessageFromSd {}

fn register() {
    let params = RegistrationParams::from_args(env::args()).unwrap();
    let connect =
        StreamDeckSocket::<GlobalSettings, ActionSettings, MessageFromSd, MessageToSd>::connect(
            params.port,
            params.event,
            params.uuid,
        );
    tokio::run(
        connect
            .map_err(|e| println!("connection failed: {:?}", e))
            .and_then(|socket| {
                socket
                    .for_each(|message| println!("recieved: {:?}", message))
                    .map_err(|e| println!("read error: {:?}", e))
            }),
    );
}

我的错误消息根据我要导入的内容而变化,但是它始终是“方法for_each存在,但不满足以下特征范围的变体”。使用现在列出的导入,我的错误消息是:

error[E0599]: no method named `for_each` found for type `streamdeck_rs::socket::StreamDeckSocket<GlobalSettings, ActionSettings, MessageFromSd, MessageToSd>` in the current scope
  --> src/main.rs:37:35
   |
37 |         .and_then(|socket| socket.for_each(|message| println!("recieved: {:?}", message))
   |                                   ^^^^^^^^
   |
   = note: the method `for_each` exists but the following trait bounds were not satisfied:
           `&mut streamdeck_rs::socket::StreamDeckSocket<GlobalSettings, ActionSettings, MessageFromSd, MessageToSd> : std::iter::Iterator`

我可以导入future::Stream

use streamdeck_rs::registration::*;
use streamdeck_rs::socket::StreamDeckSocket;
use std::env;
use futures::future::Future;
use futures::Stream;
//rest same as before

我收到此错误:

error[E0599]: no method named `for_each` found for type `streamdeck_rs::socket::StreamDeckSocket<GlobalSettings, ActionSettings, MessageFromSd, MessageToSd>` in the current scope
  --> src/main.rs:37:35
   |
37 |         .and_then(|socket| socket.for_each(|message| println!("recieved: {:?}", message))
   |                                   ^^^^^^^^
   |
   = note: the method `for_each` exists but the following trait bounds were not satisfied:
           `&mut streamdeck_rs::socket::StreamDeckSocket<GlobalSettings, ActionSettings, MessageFromSd, MessageToSd> : futures::stream::Stream`
           `&mut streamdeck_rs::socket::StreamDeckSocket<GlobalSettings, ActionSettings, MessageFromSd, MessageToSd> : std::iter::Iterator`
           `streamdeck_rs::socket::StreamDeckSocket<GlobalSettings, ActionSettings, MessageFromSd, MessageToSd> : futures::stream::Stream`



warning: unused import: `futures::Stream`

也请注意有关未使用的导入的警告。

由于StreamDeckSocket是在板条箱中定义的,因此我无法在其上实现任何特征。

我知道这个问题以几种形式存在,但据我了解,这些答案仅在我使用的板条箱中缺少特征实现时适用。

1 个答案:

答案 0 :(得分:1)

示例代码片段不太正确,但是非常接近:

  • 结构需要#[derive(Deserialize, Debug)]
  • 根据the documentation,传递给for_each的闭包类型错误。

以下编译:

use futures::future::Future;
use futures::stream::Stream;
use serde::Deserialize;
use std::env;
use streamdeck_rs::registration::*;
use streamdeck_rs::socket::StreamDeckSocket;

#[derive(Deserialize, Debug)]
struct GlobalSettings {}

#[derive(Deserialize, Debug)]
struct ActionSettings {}

#[derive(Deserialize, Debug)]
struct MessageToSd {}

#[derive(Deserialize, Debug)]
struct MessageFromSd {}

fn main() {
    let params = RegistrationParams::from_args(env::args()).unwrap();
    let connect =
        StreamDeckSocket::<GlobalSettings, ActionSettings, MessageFromSd, MessageToSd>::connect(
            params.port,
            params.event,
            params.uuid,
        );
    tokio::run(
        connect
            .map_err(|e| println!("connection failed: {:?}", e))
            .and_then(|socket| {
                socket
                    .for_each(|message| {
                        println!("recieved: {:?}", message);
                        Ok(())
                    })
                    .map_err(|e| println!("read error: {:?}", e))
            }),
    );
}