因此,目标类型为:
pub type SkittleModuleCommand = fn(ctx: Context, msg: Message, args: Vec<String>) -> dyn Future<Output = Result<()>>
但是rust编译器指出,im试图作为参数传递的函数的类型不正确. 错误:
error[E0308]: mismatched types
--> src/lib.rs:17:37
|
17 | module.register_command("ping", ping);
| ---------------- ^^^^ expected fn pointer, found fn item
| |
| arguments to this method are incorrect
|
= note: expected fn pointer `fn(serenity::prelude::Context, serenity::model::channel::Message, Vec<_>) -> (dyn std::future::Future<Output = Result<(), anyhow::Error>> + 'static)`
found fn item `fn(serenity::prelude::Context, serenity::model::channel::Message, Vec<_>) -> impl std::future::Future<Output = Result<(), anyhow::Error>> {commands::ping::exec}`
= note: when the arguments and return types match, functions can be coerced to function pointers
note: method defined here
--> /home/mcorange/@Projects/rust/autistic-bot/src/lib.rs:62:12
|
62 | pub fn register_command(&mut self, name: &str, comm: SkittleModuleCommand) {
| ^^^^^^^^^^^^^^^^
For more information about this error, try `rustc --explain E0308`.
warning: `skittle-bot-core-module` (lib) generated 4 warnings
整个项目都在github美元.
此外,以下是可重现的最小示例:
use std::{future::Future, collections::HashMap};
pub type SkittleModuleCommand = fn(a: usize) -> dyn Future<Output = ()>;
pub async fn module_fn(a: usize) {}
fn main() {
let mut mod_hm: HashMap<String, SkittleModuleCommand> = HashMap::new();
mod_hm.insert(String::from(""), module_fn);
}
我试着使用变形,但我不能得到错误的类型正确.
谢谢你的帮助!