url_serde要求不相关的特征范围

问题描述

使用serdeurl_serde板条箱,出现错误提示我需要实现不相关的特征范围:

   Compiling ser v0.1.0 (/data/scratch/ser)
error[E0277]: the trait bound `for<'a> url_serde::Ser<'a,url::Url>: a::_::_serde::Serialize` is not satisfied
  --> src/b.rs:4:10
   |
4  | #[derive(Serialize,Deserialize)]
   |          ^^^^^^^^^ the trait `for<'a> a::_::_serde::Serialize` is not implemented for `url_serde::Ser<'a,url::Url>`
   | 
  ::: /home/danj/.cargo/registry/src/github.com-1ecc6299db9ec823/url_serde-0.2.0/src/lib.rs:77:46
   |
77 |     where S: Serializer,for<'a> Ser<'a,T>: Serialize
   |                                              --------- required by this bound in `url_serde::serialize`
   |
   = help: the following implementations were found:
             <url_serde::Ser<'a,std::option::Option<url::Url>> as a::_::_serde::Serialize>
             <url_serde::Ser<'a,url::Url> as a::_::_serde::Serialize>
             <url_serde::Ser<'a,url::host::Host<String>> as a::_::_serde::Serialize>
   = note: this error originates in a derive macro (in Nightly builds,run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `url_serde::De<url::Url>: a::_::_serde::Deserialize<'_>` is not satisfied
   --> src/b.rs:4:21
    |
4   | #[derive(Serialize,Deserialize)]
    |                     ^^^^^^^^^^^ the trait `a::_::_serde::Deserialize<'_>` is not implemented for `url_serde::De<url::Url>`
    | 
   ::: /home/danj/.cargo/registry/src/github.com-1ecc6299db9ec823/url_serde-0.2.0/src/lib.rs:158:40
    |
158 |     where D: Deserializer<'de>,De<T>: Deserialize<'de>
    |                                        ---------------- required by this bound in `url_serde::deserialize`
    |
    = help: the following implementations were found:
              <url_serde::De<std::option::Option<url::Url>> as a::_::_serde::Deserialize<'de>>
              <url_serde::De<url::Url> as a::_::_serde::Deserialize<'de>>
              <url_serde::De<url::host::Host> as a::_::_serde::Deserialize<'de>>
    = note: this error originates in a derive macro (in Nightly builds,run with -Z macro-backtrace for more info)

error: aborting due to 2 prevIoUs errors

For more @R_730_4045@ion about this error,try `rustc --explain E0277`.
error: Could not compile `ser`.

To learn more,run the command again with --verbose.

Cargo.toml

[package]
name = "ser"
version = "0.1.0"
authors = ["..."]
edition = "2018"

# See more keys and their deFinitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0.115",features = ["derive"] }
url = "2.1.1"
url_serde = "0.2.0"

src / lib.rs

pub mod a;
pub mod b;

src / a.rs

use serde::{Deserialize,Serialize};

#[derive(Serialize,Deserialize)]
pub struct A {
    pub name: String,}

src / b.rs

use serde::{Deserialize,Serialize};
use url::Url;

#[derive(Serialize,Deserialize)]
pub struct B {
    #[serde(with = "url_serde")]
    uri: Url,}

解决方法

通过crates.io链接到升级:https://github.com/servo/rust-url/blob/b03895bd479d72c75600dc09c8c7906f5ee203ea/UPGRADING.md

tl; dr

url = { version = "2.0",features = ["serde"] } 并删除所有对url_serde

的引用