封闭中的`move`没有克隆数据

时间:2017-05-18 09:20:58

标签: asynchronous rust closures

我有关闭:

move |p| match Params::parse::<Value>(p) {
            Ok(ref v) if v.as_str().is_some() => {
                let chain = v.as_str().unwrap();
                match sender.send(chain) {
                    Ok(_) => futures::done(Ok(Value::Null)).boxed(),
                    Err(err) => futures::failed(JsonRpcError::invalid_params(
                        format!("Node not responding: {}", err.to_string())))
                        .boxed(),
                }
            }
            Ok(_) | Err(_) => {
                futures::failed(JsonRpcError::invalid_params("Invalidlabel"))
                    .boxed()
            }
        };

编译因错误而失败:

the trait bound `std::sync::mpsc::Sender<&str>: std::marker::Sync` is not satisfied in `[closure@src/rpc/mod.rs:223:13: 237:14 sender:std::sync::mpsc::Sender<&str>]`

不是move关键字应该捕获并克隆Sender吗?

1 个答案:

答案 0 :(得分:2)

BEGIN INSERT INTO `donut`.`myshop` SELECT vs.id , vs.script_id , 0 as my_status , NULL as time_limit , NULL as time_limit2 , NULL as time_limit3 , NULL as items FROM bread.reserve_table rt LEFT OUTER JOIN baker.view_shops vs ON rt.id = vs.id WHERE rt.start_date = CURRENT_DATE() AND vs.start_date <> rt.start_date ON DUPLICATE KEY UPDATE id = values(id) , script_id = values(script_id) , my_status = values(my_status) , time_limit = values(time_limit) , time_limit2 = values(time_limit2) , time_limit3 = values(time_limit3) , items = values(items); UPDATE baker.shops s SET start_date = ( SELECT start_date AS DATE FROM bread.reserve_table WHERE s.id = id LIMIT 1 ) ,end_date = ( SELECT end_date AS DATE FROM bread.reserve_table WHERE s.id = id LIMIT 1 ) ,random_id = 0 ,random_flg = 0 WHERE EXISTS ( SELECT 1 FROM bread.reserve_table WHERE s.id = bread.reserve_table.id AND bread.reserve_table.start_date =CURRENT_DATE() AND bread.reserve_table.start_date <> s.start_date ); END 关键字会导致move对象移动进入闭包,即它专门用于防止借用发生。您的错误消息表明Sender不是sender,这表示实际上并未发生移动。正如@ red75prim所指出的,发生这种情况的一种方法是Sync是一个引用,这将导致它被复制到闭包中,并且反过来要求底层的sender对象为{{ 1}}。

要将mpsc Sender发送到另一个线程,您需要首先克隆它,然后将克隆移动到闭包中:

Sync