Rust Serde Deserialize Vec<Self> Error 报错的解决方法 9038 | 特厉害计划
  • Why Github?
  • Team
  • Enterprise
  • Explore
  • Marketplace
  • Pricing
Sign inSign up
Watch996
Star102.4k
Fork61.8k
Tag: learning.rust
Switch branches/tags
Branches
Tags
K / Rust Serde Deserialize Vec<Self> Error 报错的解决方法.md
32 lines 5.05 KB
First commit on 19 Jun 2020

    问题重现

    给菜单做递归数组的反序列化遇到的问题,结构大致如下,报错信息附在文末。

    #[derive(Deserialize)]
    struct Menu {
        title: String,
        url: String,
        children: Vec<Self>,
    }
    

    解决方案

    这是一个类型识别的 bug,参见 issue #1565

    只需要把 children: Vec<Self>, 改成 children: Vec<Menu>, 就可以了,上上周已经有人发了 Pull Request #1830 ,我还没有升级,估计新版已经修复了此问题。

    报错信息

    error[E0277]: the trait bound `file::menu::_IMPL_DESERIALIZE_FOR_SiteMenu::<impl common::config::_IMPL_DESERIALIZE_FOR_Menus::_serde::Deserialize<'de> for file::menu::SiteMenu>::deserialize::__Visitor<'de>: common::config::_IMPL_DESERIALIZE_FOR_Menus::_serde::Deserialize<'_>` is not satisfied
      --> src\file\menu.rs:13:5
       |
    13 |     children: Vec<Self>,
       |     ^^^^^^^^ the trait `common::config::_IMPL_DESERIALIZE_FOR_Menus::_serde::Deserialize<'_>` is not implemented for `file::menu::_IMPL_DESERIALIZE_FOR_SiteMenu::<impl common::config::_IMPL_DESERIALIZE_FOR_Menus::_serde::Deserialize<'de> for file::menu::SiteMenu>::deserialize::__Visitor<'de>`
       |
       = note: required because of the requirements on the impl of `common::config::_IMPL_DESERIALIZE_FOR_Menus::_serde::Deserialize<'_>` for `std::vec::Vec<file::menu::_IMPL_DESERIALIZE_FOR_SiteMenu::<impl common::config::_IMPL_DESERIALIZE_FOR_Menus::_serde::Deserialize<'de> for file::menu::SiteMenu>::deserialize::__Visitor<'de>>`
       = note: required by `common::config::_IMPL_DESERIALIZE_FOR_Menus::_serde::de::SeqAccess::next_element`
    
    error[E0308]: mismatched types
     --> src\file\menu.rs:9:21
      |
    9 | #[derive(Serialize, Deserialize, Clone)]
      |                     ^^^^^^^^^^^ expected struct `file::menu::SiteMenu`, found struct `file::menu::_IMPL_DESERIALIZE_FOR_SiteMenu::<impl common::config::_IMPL_DESERIALIZE_FOR_Menus::_serde::Deserialize<'de> for file::menu::SiteMenu>::deserialize::__Visitor`
      |
      = note: expected struct `std::vec::Vec<file::menu::SiteMenu>`
                 found struct `std::vec::Vec<file::menu::_IMPL_DESERIALIZE_FOR_SiteMenu::<impl common::config::_IMPL_DESERIALIZE_FOR_Menus::_serde::Deserialize<'de> for file::menu::SiteMenu>::deserialize::__Visitor<'_>>`
      = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)