Home > OS >  MongoDB Rust and BSON driver make rustc compiler to claim error E0277
MongoDB Rust and BSON driver make rustc compiler to claim error E0277

Time:08-31

I got the following scenario, using:

Rust version: 1.63
Ubuntu 20.04

...and MongoDB Rust and BSON drivers as follows:

I'm using MongoDB 5.0.4, with a replica set topology.

Describing the bug:

At compile time, error E0277 arises with the following message: the trait bound ValueBag<'_>: From is not satisfied. The code pretends to get the list of databases hosted in the MongoDB instance and print'em. I'm pretty new at Rust, and a solid consumer of MongoDB ecosystem, via mongosh, via other APIs like the ones from Python, Java, Scala, etc. And I'm also pretty sure that I run this code using Rust 1.57. I've tested other topologies, like the simplest one: localhost, default port, standalone mode, but scenario remains the same, triggering E0277 at compile time.

To reproduce the code, just type one of the examples:

use mongodb::{Client, options::ClientOptions};

async fn main() -> mongodb::error::Result<()>{
   let mut client_options = ClientOptions::parse(
    "mongodb://<a_member_of_ReplSet_IP>,<another_member_of_ReplSet_IP/?replicaSet=myReplSet").await?;
    client_options.app_name = Some("My Copier app".to_string());
    let client = Client::with_options(client_options)?;
    for db_name in client.list_database_names(None, None).await? {
        println!("{}", db_name);
    }
    Ok(())
}

The error occurs at compile time:

Compiling log v0.4.14
error[E0277]: the trait bound `ValueBag<'_>: From<u128>` is not satisfied
   --> /home/jolmedo/.cargo/registry/src/github.com-1ecc6299db9ec823/log-0.4.14/src/kv/value.rs:364:21
    |
364 |                       Value::from_value_bag(value)
    |                       ^^^^^^^^^^^^^^^^^^^^^ the trait `From<u128>` is not implemented for `ValueBag<'_>`
...
384 | / impl_to_value_primitive![
385 | |     usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64, char, bool,
386 | | ];
    | |_- in this macro invocation
    |
    = help: the following other types implement trait `From<T>`:
              <ValueBag<'v> as From<&'a ()>>
              <ValueBag<'v> as From<&'a bool>>
              <ValueBag<'v> as From<&'a char>>
              <ValueBag<'v> as From<&'a f32>>
              <ValueBag<'v> as From<&'a f64>>
              <ValueBag<'v> as From<&'a i16>>
              <ValueBag<'v> as From<&'a i32>>
              <ValueBag<'v> as From<&'a i64>>
            and 25 others
    = note: required because of the requirements on the impl of `Into<ValueBag<'_>>` for `u128`
note: required by a bound in `Value::<'v>::from_value_bag`
   --> /home/jolmedo/.cargo/registry/src/github.com-1ecc6299db9ec823/log-0.4.14/src/kv/value.rs:250:12
    |
248 |     fn from_value_bag<T>(value: T) -> Self
    |        -------------- required by a bound in this
249 |     where
250 |         T: Into<ValueBag<'v>>,
    |            ^^^^^^^^^^^^^^^^^^ required by this bound in `Value::<'v>::from_value_bag`
    = note: this error originates in the macro `impl_to_value_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `ValueBag<'_>: From<i128>` is not satisfied
   --> /home/jolmedo/.cargo/registry/src/github.com-1ecc6299db9ec823/log-0.4.14/src/kv/value.rs:364:21
    |
364 |                       Value::from_value_bag(value)
    |                       ^^^^^^^^^^^^^^^^^^^^^ the trait `From<i128>` is not implemented for `ValueBag<'_>`
...
384 | / impl_to_value_primitive![
385 | |     usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64, char, bool,
386 | | ];
    | |_- in this macro invocation
    |
    = help: the following other types implement trait `From<T>`:
              <ValueBag<'v> as From<&'a ()>>
              <ValueBag<'v> as From<&'a bool>>
              <ValueBag<'v> as From<&'a char>>
              <ValueBag<'v> as From<&'a f32>>
              <ValueBag<'v> as From<&'a f64>>
              <ValueBag<'v> as From<&'a i16>>
              <ValueBag<'v> as From<&'a i32>>
              <ValueBag<'v> as From<&'a i64>>
            and 25 others
    = note: required because of the requirements on the impl of `Into<ValueBag<'_>>` for `i128`
note: required by a bound in `Value::<'v>::from_value_bag`
   --> /home/jolmedo/.cargo/registry/src/github.com-1ecc6299db9ec823/log-0.4.14/src/kv/value.rs:250:12
    |
248 |     fn from_value_bag<T>(value: T) -> Self
    |        -------------- required by a bound in this
249 |     where
250 |         T: Into<ValueBag<'v>>,
    |            ^^^^^^^^^^^^^^^^^^ required by this bound in `Value::<'v>::from_value_bag`
    = note: this error originates in the macro `impl_to_value_primitive` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `log` due to 2 previous errors

Process finished with exit code 101

Thanks in advance, for time and concern. Best regards.

CodePudding user response:

To start off, this error is not your fault. The maintainers of the log crate released as version that fails to compile on some systems. The code they released looked fine and worked on most CPUs, but ran into issues on others. This issue should not have occurred, but has since been patched.

Luckily, you can fix it adding a newer version of log to your project. Version 0.4.14 has this issue, but it has since been fixed (the newest version is 0.4.16). If you add a version with a higher patch-version number to your project, Cargo will see it is compatible and switch out the version used by your dependencies to use it instead. This is because crates with versions x.y.a and x.y.b are supposed to have compatible APIs so Cargo will use the higher of the two for both crates when compiling a project.

So the fix is to just add log = "0.4.16" to your dependencies in your Cargo.toml.

  • Related