首页 > 解决方案 > 自定义属性因 enum_dispatch 宏而恐慌

问题描述

使用时出现Unsupported Argument Type错误#[enum_dispatch]。我下面的代码非常接近docs 中的示例,因此我不确定问题可能是什么,因为此错误消息非常不透明。关于原因可能是什么或如何获得更详细的错误报告的任何想法?我可以很好地运行该示例,因此我认为这不是编译器的问题。

enum_dispatch = "0.3.5"
undo = {version = "0.46.0", features = ["colored"]}
use undo::{Action, History, Merged};
use enum_dispatch::enum_dispatch;

pub struct AppState{
    names: Vec<String>
}

#[enum_dispatch(AppActionTest)]
pub trait AppAction{
    fn apply(&mut self, target: &mut AppState) -> Result<(), String>;

    fn undo(&mut self, target: &mut AppState) -> Result<(), String>;

    fn redo(&mut self, target: &mut AppState) -> Result<(), String> {
        self.apply(target)
    }

    fn merge(&mut self, _: &mut AppActionTest) -> Merged {
        Merged::No
    }
}

pub struct AddMask;
impl Action for AddMask{
    type Target = AppState;
    type Output = ();
    type Error = String;
    fn apply(&mut self, target: &mut AppState) -> Result<(), String>{
        dbg!("APPLYING ADD MASK");
        Ok(())
    }
    fn undo(&mut self, target: &mut AppState) -> Result<(), String>{
        Ok(())
    }
    fn redo(&mut self, target: &mut AppState) -> Result<(), String> {
        self.apply(target)
    }
}

#[enum_dispatch]
pub enum AppActionTest {
    AM(AddMask)
}

fn main(){
    dbg!("running");
}

给出错误声明:

error: custom attribute panicked
  --> src\main.rs:41:1
   |
41 | #[enum_dispatch]
   | ^^^^^^^^^^^^^^^^
   |
   = help: message: Unsupported argument type

error[E0412]: cannot find type `AppActionTest` in this scope
  --> src\main.rs:19:33
   |
19 |     fn merge(&mut self, _: &mut AppActionTest) -> Merged {
   |                                 ^^^^^^^^^^^^^ not found in this scope

如果你想运行它,我用这个代码创建了一个repo 。

标签: rust

解决方案


我打开了一个问题,回购所有者给了我一个回复。看起来_inAppAction::merge对此负责。相反,请尝试:

#[enum_dispatch(AppActionTest)]
pub trait AppAction{
    fn apply(&mut self, target: &mut AppState) -> Result<(), String>;

    fn undo(&mut self, target: &mut AppState) -> Result<(), String>;

    fn redo(&mut self, target: &mut AppState) -> Result<(), String> {
        self.apply(target)
    }

    fn merge(&mut self, _unused: &mut AppActionTest) -> Merged {
        Merged::No
    }
}

编辑:这个问题现在应该在版本enum_dispatch 0.3.6和更高版本中得到修复。


推荐阅读