Skip to content

pow示例中的server.rs中出现clone编译错误 #9

@sh1yu

Description

@sh1yu

在实践live_coding/pow/src/server.rs时,相同的引用代码但是编译器报了如下的错误:

🍀 ~/codes/rust/rust-training/practice/pow % cargo build                                                         2021-11-30 13:13:56
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
package:   /Users/psycode/codes/rust/rust-training/practice/algori/Cargo.toml
workspace: /Users/psycode/codes/rust/rust-training/Cargo.toml
   Compiling pow v0.1.0 (/Users/psycode/codes/rust/rust-training/practice/pow)
error[E0599]: the method `clone` exists for enum `Result<pow::pb::BlockHash, Status>`, but its trait bounds were not satisfied
   --> practice/pow/src/server.rs:26:31
    |
26  |             match tx.send(msg.clone()).await {
    |                               ^^^^^ method cannot be called on `Result<pow::pb::BlockHash, Status>` due to unsatisfied trait bounds
    |
   ::: /Users/psycode/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:503:1
    |
503 | pub enum Result<T, E> {
    | --------------------- doesn't satisfy `Result<pow::pb::BlockHash, Status>: Clone`
    |
   ::: /Users/psycode/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/tonic-0.6.1/src/status.rs:36:1
    |
36  | pub struct Status {
    | ----------------- doesn't satisfy `Status: Clone`
    |
    = note: the following trait bounds were not satisfied:
            `Status: Clone`
            which is required by `Result<pow::pb::BlockHash, Status>: Clone`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `pow` due to previous error

不太清楚为什么这里的Result没有实现clone trait,使用的也是anyhowResult.

测试代码:

use anyhow::Result;
use futures::Stream;
use pow::pb::pow_builder_server::*;
use pow::pb::*;
use std::pin::Pin;
use std::sync::Arc;
use std::{collections::HashMap, thread};
use tokio::sync::{mpsc, RwLock};
use tokio_stream::wrappers::ReceiverStream;
use tonic::transport::Server;
use tonic::{Request, Response, Status};

const CHANNEL_SIZE: usize = 8;

#[derive(Debug, Default)]
struct Shared {
    clients: HashMap<String, mpsc::Sender<Result<BlockHash, Status>>>,
}

impl Shared {
    async fn broadcast(&self, msg: Option<BlockHash>) {
        let msg = msg.ok_or(Status::resource_exhausted(
            "Failed to find a suitabe hash".to_string(),
        ));
        for (name, tx) in &self.clients {
            match tx.send(msg.clone()).await {
                Ok(_) => (),
                Err(err) => {
                    println!(
                        "Broadcast error to {} with {:?}, Error: {:?}",
                        name, msg, err
                    )
                }
            }
        }
    }
}

#[derive(Debug)]
pub struct PowService {
    //send block to  pow engine
    tx: mpsc::Sender<Block>,
    shared: Arc<RwLock<Shared>>,
}

// ... 下面省略,和示例代码保持一致

引用依赖:

[dependencies]
prost = "0.9"
tonic = "0.6"
tokio = { version = "1", features = ["sync", "macros", "rt-multi-thread"] }
tokio-stream = "0.1"
futures = "0.3"
anyhow = "1"
blake3 = "1"

[build-dependencies]
tonic-build = "0.6"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions