rust - 自定义属性因 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 。
解决方案
我打开了一个问题,回购所有者给了我一个回复。看起来_
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
和更高版本中得到修复。
推荐阅读
- git - Docker git 私有 ssh 密钥
- python - 将包含每列值的标题行添加到多个 CSV 文件
- c# - 实体框架中的单边一对一关系
- flutter - 如何在 Flutter 中设置部分文本的样式?
- vba - MS-Access VBA - 如何将文本框使用的当前查询字段名称分配给变量
- html - 高度的功能是什么:这里的自动?
- javascript - 如何检查布尔值是否设置为 true,然后将其推送到嵌套数组
- mysql - MySQL select 语句缺少一些字段
- node.js - 使用 NodeJS 抓取网站时出现问题验证码
- css - 字体真棒包 - 没有得到正确的图标