英文:
Generic type not found in scope Rocket's router handler
问题
以下是您要翻译的代码部分:
#[post("/handle_message", format = "json", data = "<json_message>")]
pub async fn handle_message<G>(
session: &State<Arc<SessionState>>,
peer_state: &State<Arc<PeerState>>,
json_message: Json<models::JsonMessage<G>>,
cfg: &State<ReloadableConfig>,
)
where
G: Group + GroupEncoding + Default + Serialize,
{
trace!("Handling POST /handle_message");
let cfg = cfg.load_full();
session.handle_message(json_message.message.clone());
}
我得到了编译器错误:无法在此范围中找到类型 G
。
英文:
What's the correct way to use generics in Rocket's router handler. I'm trying the following:
#[post("/handle_message", format = "json", data = "<json_message>")]
pub async fn handle_message<G>(
session: &State<Arc<SessionState>>,
peer_state: &State<Arc<PeerState>>,
json_message: Json<models::JsonMessage<G>>,
cfg: &State<ReloadableConfig>,
)
where
G: Group + GroupEncoding + Default + Serialize,
{
trace!("Handling POST /handle_message");
let cfg = cfg.load_full();
session.handle_message(json_message.message.clone());
}
I'm getting the compiler error: cannot find type G in this scope not found in this scope
答案1
得分: 0
抱歉,我只提供中文翻译,不会回答问题。以下是您要翻译的内容:
"Rocket" 目前不能拥有通用的请求处理程序。
这 (可悲地) 是 Rust 类型系统的限制,并已由 Sergio 在 此 GitHub 问题 中讨论。
英文:
You can't currently have generic request handlers in Rocket.
This is (sadly) a limitation of the type system in Rust and has been discussed by Sergio in this github issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论