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

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

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(&quot;/handle_message&quot;, format = &quot;json&quot;, data = &quot;&lt;json_message&gt;&quot;)]
pub async fn handle_message&lt;G&gt;(
    session: &amp;State&lt;Arc&lt;SessionState&gt;&gt;,
    peer_state: &amp;State&lt;Arc&lt;PeerState&gt;&gt;,
    json_message: Json&lt;models::JsonMessage&lt;G&gt;&gt;,
    cfg: &amp;State&lt;ReloadableConfig&gt;,
)
where
    G: Group + GroupEncoding + Default + Serialize,
{
    trace!(&quot;Handling POST /handle_message&quot;);
    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.

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:

确定