是否可以通过渠道发送关闭消息?

迈克·克莱因(Maik Klein)

我想通过渠道发送闭幕消息:

use std::thread;
use std::sync::mpsc;

#[derive(Debug)]
struct Test {
    s1: String,
    s2: String,
}

fn main() {
    let t = Test {
        s1: "Hello".to_string(),
        s2: "Hello".to_string(),
    };
    let (tx, rx) = mpsc::channel::<FnOnce(&mut Test)>();
    thread::spawn(move || {
        let mut test = t;
        let f = rx.recv().unwrap();
        f(&mut test);
        println!("{:?}", test);
    });
    tx.send(move |t: &mut Test| {
        let s = "test".to_string();
        t.s1 = s;
    });
}

游乐场

我收到很多错误:

error[E0277]: the trait bound `for<'r> std::ops::FnOnce(&'r mut Test): std::marker::Sized` is not satisfied
  --> src/main.rs:15:20
   |
15 |     let (tx, rx) = mpsc::channel::<FnOnce(&mut Test)>();
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `for<'r> std::ops::FnOnce(&'r mut Test)` does not have a constant size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `for<'r> std::ops::FnOnce(&'r mut Test)`
   = note: required by `std::sync::mpsc::channel`

error[E0277]: the trait bound `for<'r> std::ops::FnOnce(&'r mut Test): std::marker::Sized` is not satisfied
  --> src/main.rs:15:20
   |
15 |     let (tx, rx) = mpsc::channel::<FnOnce(&mut Test)>();
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `for<'r> std::ops::FnOnce(&'r mut Test)` does not have a constant size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `for<'r> std::ops::FnOnce(&'r mut Test)`
   = note: required by `std::sync::mpsc::Sender`

error[E0599]: no method named `recv` found for type `std::sync::mpsc::Receiver<for<'r> std::ops::FnOnce(&'r mut Test)>` in the current scope
  --> src/main.rs:18:20
   |
18 |         let f = rx.recv().unwrap();
   |                    ^^^^
   |
   = note: the method `recv` exists but the following trait bounds were not satisfied:
           `for<'r> std::ops::FnOnce(&'r mut Test) : std::marker::Sized`

error[E0599]: no method named `send` found for type `std::sync::mpsc::Sender<for<'r> std::ops::FnOnce(&'r mut Test)>` in the current scope
  --> src/main.rs:22:8
   |
22 |     tx.send(move |t: &mut Test| {
   |        ^^^^
   |
   = note: the method `send` exists but the following trait bounds were not satisfied:
           `for<'r> std::ops::FnOnce(&'r mut Test) : std::marker::Sized`

看来这FnOnce不是可发送的,但我不明白为什么。

DK。

是的。您的代码有一些问题。

首先,它FnOnce是一个特征,因此您不能直接使用它。特性必须是对具体类型的约束,或者是某种间接类型的后面。由于要将封包发送到其他地方,因此您需要类似的内容Box<FnOnce(...)>

其次,你不能使用Box<FnOnce(...)>,因为,由于对象的安全规则,你不能真正称之为一个FnOnce通过间接。

(顺便说一句,您也不想使用FnOnce<...>语法,这在技术上是不稳定的;请FnOnce(...)改为使用。)

为了解决这个问题,你可以切换到FnFnMut 使用尚未稳定的FnBox特质。我之所以走这条路,是因为它可能具有您想要的语义,并且在不久的将来可能会得到稳定。如果您对此感到不舒服,则需要适当地修改闭合。

以下是我和Manishearth之间的共同努力(他指出我错过了+ Send约束):

// NOTE: Requires a nightly compiler, as of Rust 1.0.

#![feature(core)]
use std::boxed::FnBox;
use std::thread;
use std::sync::mpsc;

#[derive(Debug)]
struct Test {
    s1: String,
    s2: String,
}

type ClosureType = Box<FnBox(&mut Test) + Send>;

fn main() {
    let t = Test { s1: "Hello".to_string(), s2: "Hello".to_string() };
    let (tx, rx) = mpsc::channel::<ClosureType>();

    thread::spawn(move || {
        let mut test = t;
        let f = rx.recv().unwrap();
        f.call_box((&mut test,));
        println!("{:?}", test);
    });

    tx.send(Box::new(move |t: &mut Test| {
        let s = "test".to_string();
        t.s1 = s;
    })).unwrap();

    // To give the output time to show up:
    thread::sleep_ms(100);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在应用程序关闭时通过套接字frn DLL发送消息?

来自分类Dev

是否可以通过JSON从刀片模板发送html输出?

来自分类Dev

是否可以通过通知操作发送LocalBroadcast?

来自分类Dev

android SmsManager显示消息是否发送了已关闭的号码?

来自分类Dev

libcurl是否能够通过UDP发送http消息?

来自分类Dev

是否可以通过python关闭程序?

来自分类Dev

是否可以向Erlang中未注册的进程发送消息?

来自分类Dev

您可以通过Twilio发送富文本消息吗?

来自分类Dev

通过worker.postMessage()发送的消息是否排队?

来自分类Dev

通过UDP发送数据时,是否可以将数据流化为ZeroMQ消息?

来自分类Dev

是否可以将Azure队列消息发送到端点

来自分类Dev

是否可以通过cURL发送“ OPTIONS *”?

来自分类Dev

如果无法发送消息,Netty是否总是关闭频道

来自分类Dev

我可以通过网络发送消息吗?

来自分类Dev

是否可以使用SignalR向特定用户发送消息?

来自分类Dev

是否可以从外部渠道向Microsoft团队发送消息

来自分类Dev

是否可以将Websocket消息发送到kafka主题?

来自分类Dev

无法通过其他渠道发送消息,但团队(nodejs)的通用msbot除外

来自分类Dev

如何获得发送消息的渠道?

来自分类Dev

Discord Py,通过ID在任何渠道中发送消息

来自分类Dev

是否可以向脱机的Skype联系人发送消息?

来自分类Dev

Facebook。是否可以发送私人消息?

来自分类Dev

libcurl是否能够通过UDP发送http消息?

来自分类Dev

是否可以通过串行线路发送NUL?

来自分类Dev

getmail可以通过SMTP发送消息吗?

来自分类Dev

是否可以通过渠道使用 Any 特性?

来自分类Dev

Socket.io。是否可以在不指定命名空间的情况下通过套接字 id 发送到单个 socketid(私人消息)?

来自分类Dev

是否可以“暂停”发送消息以进行检查?

来自分类Dev

grpc 是否可以向所有子频道发送消息?

Related 相关文章

  1. 1

    如何在应用程序关闭时通过套接字frn DLL发送消息?

  2. 2

    是否可以通过JSON从刀片模板发送html输出?

  3. 3

    是否可以通过通知操作发送LocalBroadcast?

  4. 4

    android SmsManager显示消息是否发送了已关闭的号码?

  5. 5

    libcurl是否能够通过UDP发送http消息?

  6. 6

    是否可以通过python关闭程序?

  7. 7

    是否可以向Erlang中未注册的进程发送消息?

  8. 8

    您可以通过Twilio发送富文本消息吗?

  9. 9

    通过worker.postMessage()发送的消息是否排队?

  10. 10

    通过UDP发送数据时,是否可以将数据流化为ZeroMQ消息?

  11. 11

    是否可以将Azure队列消息发送到端点

  12. 12

    是否可以通过cURL发送“ OPTIONS *”?

  13. 13

    如果无法发送消息,Netty是否总是关闭频道

  14. 14

    我可以通过网络发送消息吗?

  15. 15

    是否可以使用SignalR向特定用户发送消息?

  16. 16

    是否可以从外部渠道向Microsoft团队发送消息

  17. 17

    是否可以将Websocket消息发送到kafka主题?

  18. 18

    无法通过其他渠道发送消息,但团队(nodejs)的通用msbot除外

  19. 19

    如何获得发送消息的渠道?

  20. 20

    Discord Py,通过ID在任何渠道中发送消息

  21. 21

    是否可以向脱机的Skype联系人发送消息?

  22. 22

    Facebook。是否可以发送私人消息?

  23. 23

    libcurl是否能够通过UDP发送http消息?

  24. 24

    是否可以通过串行线路发送NUL?

  25. 25

    getmail可以通过SMTP发送消息吗?

  26. 26

    是否可以通过渠道使用 Any 特性?

  27. 27

    Socket.io。是否可以在不指定命名空间的情况下通过套接字 id 发送到单个 socketid(私人消息)?

  28. 28

    是否可以“暂停”发送消息以进行检查?

  29. 29

    grpc 是否可以向所有子频道发送消息?

热门标签

归档