Trait not implemented for a thing I think implements the trait.

huangapple go评论57阅读模式
英文:

Trait not implemented for a thing I think implements the trait

问题

以下是翻译的部分,不包括代码:

我正在尝试从一个 Rust 的 crate 中调用一个方法,但出现了一个错误,错误信息显示一个 trait 没有为我传递的某个方法实现。然而,当我在 docs.rs 上查看这个方法时,它似乎已经实现了所需的 trait。

我正在尝试调用一个名为 new_with_count 的方法,来自一个名为 wagyu_ethereum 的 crate。

我用于调用该方法的代码如下:

use rand::{rngs::StdRng, SeedableRng};
use wagyu_ethereum::*;
use wagyu_model::mnemonic::MnemonicCount;
use wagyu_model::MnemonicError;

pub fn generate_mnemonic() -> Result<String, MnemonicError> {
    let mut rng = StdRng::from_entropy();

    let mnemonic = EthereumMnemonic::<
        wagyu_ethereum::network::Mainnet,
        wagyu_ethereum::wordlist::English,
    >::new_with_count(&mut rng, 24)?;

    Ok(String::from("placeholder"))
}

方法的代码(来自项目的 GitHub 页面)如下:

impl<N: EthereumNetwork, W: EthereumWordlist> MnemonicCount for EthereumMnemonic<N, W> {
    /// 根据单词数量返回一个新的助记词。
    fn new_with_count<R: Rng>(rng: &mut R, word_count: u8) -> Result<Self, MnemonicError> {
        let length: usize = match word_count {
            12 => 16,
            15 => 20,
            18 => 24,
            21 => 28,
            24 => 32,
            wc => return Err(MnemonicError::InvalidWordCount(wc)),
        };

        let entropy: [u8; 32] = rng.gen();

        Ok(Self {
            entropy: entropy[0..length].to_vec(),
            _network: PhantomData,
            _wordlist: PhantomData,
        })
    }
}

错误信息:

error[E0277]: the trait bound `StdRng: rand_core::RngCore` is not satisfied
  --> src/lib.rs:12:23
   |
9  |       let mnemonic = EthereumMnemonic::<
   |  ____________________-
10 | |         wagyu_ethereum::network::Mainnet,
11 | |         wagyu_ethereum::wordlist::English,
12 | |     >::new_with_count(&mut rng, 24)?;
   | |                     - ^^^^^^^^ the trait `rand_core::RngCore` is not implemented for `StdRng`
   | |_____________________|
   |                       required by a bound introduced by this call
   |
   = help: the following other types implement trait `rand_core::RngCore`:
             &mut R
             Box<R>
             rand::rngs::adapter::read::ReadRng<R>
             rand::rngs::adapter::reseeding::ReseedingRng<R, Rsdr>
             rand::rngs::entropy::EntropyRng
             rand::rngs::mock::StepRng
             rand::rngs::std::StdRng
             rand::rngs::thread::ThreadRng
           and 6 others
   = note: required because of the requirements on the impl of `rand::Rng` for `StdRng`
note: required by a bound in `new_with_count`
  --> /home/me/.cargo/registry/src/github.com-1ecc6299db9ec823/wagyu-model-0.6.3/src/mnemonic.rs:44:26
   |
44 |     fn new_with_count<R: Rng>(rng: &mut R, word_count: u8) -> Result<Self, MnemonicError>;
   |                          ^^^ required by this bound in `new_with_count`

所以根据我从 docs.rs 上的文档 看到的信息,似乎 StdRng 实际上已经实现了 RngCore。我还找到了这个 问题,它表明可能是我借用的次数过多,但我不确定这是否与此处相关,因为我认为我没有借用太多次。

最后,该项目的 GitHub 账户中有类似于我所做的调用方法的实例(方法调用rng 赋值)。这促使我使用这种方法。当你运行它们的程序时,它们也能正常工作。因此,我想问的是,在尝试调用这个方法时,我做错了什么?

英文:

I am trying to call a method from a rust crate and getting an error stating that a trait is not implemented for one of the methods I am passing however when I look the method up on docs.rs it does appear to be implementing the required trait.

I am trying to call a method called new_with_count from a crate called wagyu_ethereum.

The code I am using to make the call is here:

use rand::{rngs::StdRng, SeedableRng};
use wagyu_ethereum::*;
use wagyu_model::mnemonic::MnemonicCount;
use wagyu_model::MnemonicError;

pub fn generate_mnemonic() -&gt; Result&lt;String, MnemonicError&gt; {
    let mut rng = StdRng::from_entropy();

    let mnemonic = EthereumMnemonic::&lt;
        wagyu_ethereum::network::Mainnet,
        wagyu_ethereum::wordlist::English,
    &gt;::new_with_count(&amp;mut rng, 24)?;

    Ok(String::from(&quot;placeholder&quot;))
}

The code for the method (from the projects GitHub page:

impl&lt;N: EthereumNetwork, W: EthereumWordlist&gt; MnemonicCount for EthereumMnemonic&lt;N, W&gt; {
    /// Returns a new mnemonic given the word count.
    fn new_with_count&lt;R: Rng&gt;(rng: &amp;mut R, word_count: u8) -&gt; Result&lt;Self, MnemonicError&gt; {
        let length: usize = match word_count {
            12 =&gt; 16,
            15 =&gt; 20,
            18 =&gt; 24,
            21 =&gt; 28,
            24 =&gt; 32,
            wc =&gt; return Err(MnemonicError::InvalidWordCount(wc)),
        };

        let entropy: [u8; 32] = rng.gen();

        Ok(Self {
            entropy: entropy[0..length].to_vec(),
            _network: PhantomData,
            _wordlist: PhantomData,
        })
    }
}

The error:

error[E0277]: the trait bound `StdRng: rand_core::RngCore` is not satisfied
  --&gt; src/lib.rs:12:23
   |
9  |       let mnemonic = EthereumMnemonic::&lt;
   |  ____________________-
10 | |         wagyu_ethereum::network::Mainnet,
11 | |         wagyu_ethereum::wordlist::English,
12 | |     &gt;::new_with_count(&amp;mut rng, 24)?;
   | |                     - ^^^^^^^^ the trait `rand_core::RngCore` is not implemented for `StdRng`
   | |_____________________|
   |                       required by a bound introduced by this call
   |
   = help: the following other types implement trait `rand_core::RngCore`:
             &amp;&#39;a mut R
             Box&lt;R&gt;
             rand::rngs::adapter::read::ReadRng&lt;R&gt;
             rand::rngs::adapter::reseeding::ReseedingRng&lt;R, Rsdr&gt;
             rand::rngs::entropy::EntropyRng
             rand::rngs::mock::StepRng
             rand::rngs::std::StdRng
             rand::rngs::thread::ThreadRng
           and 6 others
   = note: required because of the requirements on the impl of `rand::Rng` for `StdRng`
note: required by a bound in `new_with_count`
  --&gt; /home/me/.cargo/registry/src/github.com-1ecc6299db9ec823/wagyu-model-0.6.3/src/mnemonic.rs:44:26
   |
44 |     fn new_with_count&lt;R: Rng&gt;(rng: &amp;mut R, word_count: u8) -&gt; Result&lt;Self, MnemonicError&gt;;
   |                          ^^^ required by this bound in `new_with_count`

So as far as I can tell from the documentation on docs.rs it appears that StdRng does actually implement RngCore. I've also found this question that indicates I might be borrowing to many time but I'm not sure if it is relevant here because I don't think I am borrowing as many times.

Finally on the projects Github account they have instances of calling the method the same way I am doing (method call, rng assignment). This is what prompted me to use this method. Their program also works when you run it. So the question I have is what am I doing wrong when trying to call this method?

答案1

得分: 1

以下是翻译好的部分:

  • 在撰写时,最新版本如下:

    • rand = "0.8.5"
    • wagyu-ethereum = "0.6.3"
    • wagyu-model = "0.6.3"
  • 看起来存在版本不匹配的问题。wagyu-ethereum = "0.6.3" 依赖于 rand = "0.7.3",而如果您执行 cargo add rand 的默认版本是 rand = "0.8.5"

  • 您可以在 cargo tree 中看到这一点:

    rust_test v0.1.0 (/home/me/work/rust_test)
    ├── rand v0.8.5
    │   ├── ...
    ├── wagyu-ethereum v0.6.3
    │   ├── ...
    ├── wagyu-model v0.6.3
    │   ├── ...
    
  • 通常情况下,Cargo会尝试为所有依赖项找到一个共同的库的单一版本,但是根据Rust的语义化版本规则0.7.x0.8.x 不兼容,所以它将库包含两次。当然,它不能允许这两个版本一起使用,因为结果将是不可预测的。

  • 尽管问题很复杂和难以理解,但解决方法很简单:

    请在您的 Cargo.toml 中使用 rand = "0.7.3",直到 wagyu 升级到 rand = "0.8.x"

  • 您可以在他们的问题跟踪器中提出一个问题,但似乎这个项目已经超过一年没有得到维护了。他们的网站甚至不再存在。所以请注意,这个项目可能已被废弃。

英文:

At the time of writing, the newest versions are:

  • rand = &quot;0.8.5&quot;
  • wagyu-ethereum = &quot;0.6.3&quot;
  • wagyu-model = &quot;0.6.3&quot;

It seems that a version mismatch exists here. wagyu-ethereum = &quot;0.6.3&quot; depends on rand = &quot;0.7.3&quot;, while the current default if you do cargo add rand is rand = &quot;0.8.5&quot;.

You can see this in cargo tree:

rust_test v0.1.0 (/home/me/work/rust_test)
├── rand v0.8.5
│   ├── libc v0.2.139
│   ├── rand_chacha v0.3.1
│   │   ├── ppv-lite86 v0.2.17
│   │   └── rand_core v0.6.4
│   │       └── getrandom v0.2.8
│   │           ├── cfg-if v1.0.0
│   │           └── libc v0.2.139
│   └── rand_core v0.6.4 (*)
├── wagyu-ethereum v0.6.3
│   ├── base58 v0.1.0
│   ├── bitvec v0.17.4
│   │   ├── either v1.8.0
│   │   └── radium v0.3.0
│   ├── ethereum-types v0.9.2
│   │   ├── ethbloom v0.9.2
│   │   │   ├── crunchy v0.2.2
│   │   │   ├── fixed-hash v0.6.1
│   │   │   │   ├── byteorder v1.4.3
│   │   │   │   ├── rand v0.7.3
│   │   │   │   │   ├── getrandom v0.1.16
│   │   │   │   │   │   ├── cfg-if v1.0.0
│   │   │   │   │   │   └── libc v0.2.139
│   │   │   │   │   ├── libc v0.2.139
│   │   │   │   │   ├── rand_chacha v0.2.2
│   │   │   │   │   │   ├── ppv-lite86 v0.2.17
│   │   │   │   │   │   └── rand_core v0.5.1
│   │   │   │   │   │       └── getrandom v0.1.16 (*)
│   │   │   │   │   └── rand_core v0.5.1 (*)
│   │   │   │   ├── rustc-hex v2.1.0
│   │   │   │   └── static_assertions v1.1.0
│   │   │   ├── impl-rlp v0.2.1
│   │   │   │   └── rlp v0.4.6
│   │   │   │       └── rustc-hex v2.1.0
│   │   │   ├── impl-serde v0.3.2
│   │   │   │   └── serde v1.0.152
│   │   │   │       └── serde_derive v1.0.152 (proc-macro)
│   │   │   │           ├── proc-macro2 v1.0.49
│   │   │   │           │   └── unicode-ident v1.0.6
│   │   │   │           ├── quote v1.0.23
│   │   │   │           │   └── proc-macro2 v1.0.49 (*)
│   │   │   │           └── syn v1.0.107
│   │   │   │               ├── proc-macro2 v1.0.49 (*)
│   │   │   │               ├── quote v1.0.23 (*)
│   │   │   │               └── unicode-ident v1.0.6
│   │   │   └── tiny-keccak v2.0.2
│   │   │       └── crunchy v0.2.2
│   │   ├── fixed-hash v0.6.1 (*)
│   │   ├── impl-rlp v0.2.1 (*)
│   │   ├── impl-serde v0.3.2 (*)
│   │   ├── primitive-types v0.7.3
│   │   │   ├── fixed-hash v0.6.1 (*)
│   │   │   ├── impl-codec v0.4.2
│   │   │   │   └── parity-scale-codec v1.3.7
│   │   │   │       ├── arrayvec v0.5.2
│   │   │   │       ├── bitvec v0.17.4 (*)
│   │   │   │       ├── byte-slice-cast v0.3.5
│   │   │   │       └── serde v1.0.152 (*)
│   │   │   ├── impl-rlp v0.2.1 (*)
│   │   │   ├── impl-serde v0.3.2 (*)
│   │   │   └── uint v0.8.5
│   │   │       ├── byteorder v1.4.3
│   │   │       ├── crunchy v0.2.2
│   │   │       ├── rustc-hex v2.1.0
│   │   │       └── static_assertions v1.1.0
│   │   └── uint v0.8.5 (*)
│   ├── hex v0.4.3
│   ├── hmac v0.7.1
│   │   ├── crypto-mac v0.7.0
│   │   │   ├── generic-array v0.12.4
│   │   │   │   └── typenum v1.16.0
│   │   │   └── subtle v1.0.0
│   │   └── digest v0.8.1
│   │       └── generic-array v0.12.4 (*)
│   ├── pbkdf2 v0.3.0
│   │   ├── byteorder v1.4.3
│   │   ├── crypto-mac v0.7.0 (*)
│   │   └── rayon v1.6.1
│   │       ├── either v1.8.0
│   │       └── rayon-core v1.10.1
│   │           ├── crossbeam-channel v0.5.6
│   │           │   ├── cfg-if v1.0.0
│   │           │   └── crossbeam-utils v0.8.14
│   │           │       └── cfg-if v1.0.0
│   │           ├── crossbeam-deque v0.8.2
│   │           │   ├── cfg-if v1.0.0
│   │           │   ├── crossbeam-epoch v0.9.13
│   │           │   │   ├── cfg-if v1.0.0
│   │           │   │   ├── crossbeam-utils v0.8.14 (*)
│   │           │   │   ├── memoffset v0.7.1
│   │           │   │   │   [build-dependencies]
│   │           │   │   │   └── autocfg v1.1.0
│   │           │   │   └── scopeguard v1.1.0
│   │           │   │   [build-dependencies]
│   │           │   │   └── autocfg v1.1.0
│   │           │   └── crossbeam-utils v0.8.14 (*)
│   │           ├── crossbeam-utils v0.8.14 (*)
│   │           └── num_cpus v1.15.0
│   │               └── libc v0.2.139
│   ├── rand v0.7.3 (*)
│   ├── regex v1.7.0
│   │   ├── aho-corasick v0.7.20
│   │   │   └── memchr v2.5.0
│   │   ├── memchr v2.5.0
│   │   └── regex-syntax v0.6.28
│   ├── rlp v0.4.6 (*)
│   ├── secp256k1 v0.17.2
│   │   └── secp256k1-sys v0.1.2
│   │       [build-dependencies]
│   │       └── cc v1.0.41
│   ├── serde v1.0.152 (*)
│   ├── serde_json v1.0.91
│   │   ├── itoa v1.0.5
│   │   ├── ryu v1.0.12
│   │   └── serde v1.0.152 (*)
│   ├── sha2 v0.8.2
│   │   ├── block-buffer v0.7.3
│   │   │   ├── block-padding v0.1.5
│   │   │   │   └── byte-tools v0.3.1
│   │   │   ├── byte-tools v0.3.1
│   │   │   ├── byteorder v1.4.3
│   │   │   └── generic-array v0.12.4 (*)
│   │   ├── digest v0.8.1 (*)
│   │   ├── fake-simd v0.1.2
│   │   └── opaque-debug v0.2.3
│   ├── tiny-keccak v1.5.0
│   │   └── crunchy v0.2.2
│   └── wagyu-model v0.6.3
│       ├── base58 v0.1.0
│       ├── base58-monero v0.2.1
│       │   ├── async-stream v0.2.1
│       │   │   ├── async-stream-impl v0.2.1 (proc-macro)
│       │   │   │   ├── proc-macro2 v1.0.49 (*)
│       │   │   │   ├── quote v1.0.23 (*)
│       │   │   │   └── syn v1.0.107 (*)
│       │   │   └── futures-core v0.3.25
│       │   ├── futures-util v0.3.25
│       │   │   ├── futures-core v0.3.25
│       │   │   ├── futures-macro v0.3.25 (proc-macro)
│       │   │   │   ├── proc-macro2 v1.0.49 (*)
│       │   │   │   ├── quote v1.0.23 (*)
│       │   │   │   └── syn v1.0.107 (*)
│       │   │   ├── futures-task v0.3.25
│       │   │   ├── pin-project-lite v0.2.9
│       │   │   ├── pin-utils v0.1.0
│       │   │   └── slab v0.4.7
│       │   │       [build-dependencies]
│       │   │       └── autocfg v1.1.0
│       │   ├── thiserror v1.0.38
│       │   │   └── thiserror-impl v1.0.38 (proc-macro)
│       │   │       ├── proc-macro2 v1.0.49 (*)
│       │   │       ├── quote v1.0.23 (*)
│       │   │       └── syn v1.0.107 (*)
│       │   ├── tiny-keccak v2.0.2 (*)
│       │   └── tokio v0.2.25
│       │       ├── bytes v0.5.6
│       │       ├── futures-core v0.3.25
│       │       ├── memchr v2.5.0
│       │       └── pin-project-lite v0.1.12
│       ├── bech32 v0.6.0
│       ├── byteorder v1.4.3
│       ├── crypto-mac v0.7.0 (*)
│       ├── ethereum-types v0.9.2 (*)
│       ├── failure v0.1.8
│       │   ├── backtrace v0.3.57
│       │   │   ├── addr2line v0.14.1
│       │   │   │   └── gimli v0.23.0
│       │   │   ├── cfg-if v1.0.0
│       │   │   ├── libc v0.2.139
│       │   │   ├── miniz_oxide v0.4.4
│       │   │   │   └── adler v1.0.2
│       │   │   │   [build-dependencies]
│       │   │   │   └── autocfg v1.1.0
│       │   │   ├── object v0.23.0
│       │   │   └── rustc-demangle v0.1.21
│       │   └── failure_derive v0.1.8 (proc-macro)
│       │       ├── proc-macro2 v1.0.49 (*)
│       │       ├── quote v1.0.23 (*)
│       │       ├── syn v1.0.107 (*)
│       │       └── synstructure v0.12.6
│       │           ├── proc-macro2 v1.0.49 (*)
│       │           ├── quote v1.0.23 (*)
│       │           ├── syn v1.0.107 (*)
│       │           └── unicode-xid v0.2.4
│       ├── ff v0.6.0
│       │   ├── byteorder v1.4.3
│       │   └── rand_core v0.5.1 (*)
│       ├── hex v0.4.3
│       ├── rand v0.7.3 (*)
│       ├── rand_core v0.5.1 (*)
│       ├── ripemd160 v0.8.0
│       │   ├── block-buffer v0.7.3 (*)
│       │   ├── digest v0.8.1 (*)
│       │   └── opaque-debug v0.2.3
│       ├── rlp v0.4.6 (*)
│       ├── secp256k1 v0.17.2 (*)
│       ├── serde_json v1.0.91 (*)
│       ├── sha2 v0.8.2 (*)
│       └── uint v0.8.5 (*)
└── wagyu-model v0.6.3 (*)

Usually, Cargo tries to find a single common version of a library for all dependencies, but 0.7.x and 0.8.x are not compatible according to Rust's semver rules, so it includes the library twice. Of course it can't allow those two to be used with each other, as the results would be unpredictable.

While the problem is complicated and obscure, the solution is simple:

Use rand = &quot;0.7.3&quot; in your Cargo.toml until wagyu gets upgraded to rand = &quot;0.8.x&quot;.

You could open an issue in their issue-tracker about it, but it seems that the project as a whole didn't get maintained since over a year. Their website doesn't even exist any more. So be aware that this project might be abandoned.

huangapple
  • 本文由 发表于 2023年1月9日 09:08:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定