通用类型在 Rocket 的路由处理程序中未找到。

huangapple go评论104阅读模式
英文:

Generic type not found in scope Rocket's router handler

问题

以下是您要翻译的代码部分:

  1. #[post("/handle_message", format = "json", data = "<json_message>")]
  2. pub async fn handle_message<G>(
  3. session: &State<Arc<SessionState>>,
  4. peer_state: &State<Arc<PeerState>>,
  5. json_message: Json<models::JsonMessage<G>>,
  6. cfg: &State<ReloadableConfig>,
  7. )
  8. where
  9. G: Group + GroupEncoding + Default + Serialize,
  10. {
  11. trace!("Handling POST /handle_message");
  12. let cfg = cfg.load_full();
  13. session.handle_message(json_message.message.clone());
  14. }

我得到了编译器错误:无法在此范围中找到类型 G

英文:

What's the correct way to use generics in Rocket's router handler. I'm trying the following:

  1. #[post(&quot;/handle_message&quot;, format = &quot;json&quot;, data = &quot;&lt;json_message&gt;&quot;)]
  2. pub async fn handle_message&lt;G&gt;(
  3. session: &amp;State&lt;Arc&lt;SessionState&gt;&gt;,
  4. peer_state: &amp;State&lt;Arc&lt;PeerState&gt;&gt;,
  5. json_message: Json&lt;models::JsonMessage&lt;G&gt;&gt;,
  6. cfg: &amp;State&lt;ReloadableConfig&gt;,
  7. )
  8. where
  9. G: Group + GroupEncoding + Default + Serialize,
  10. {
  11. trace!(&quot;Handling POST /handle_message&quot;);
  12. let cfg = cfg.load_full();
  13. session.handle_message(json_message.message.clone());
  14. }

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.

huangapple
  • 本文由 发表于 2023年6月12日 15:35:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定