“Clone”特性未为”actix_web::Scope”实现。

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

the trait `Clone` is not implemented for `actix_web::Scope`

问题

我想将我的应用程序路由分组在范围内,以便将来可以根据领域分离它们的文件位置。
我试图做的是将

  1. HttpServer::new(move || App::new().app_data(app_state.clone()).service(delete_comment).service(update_comment).service(get_comments).service(create_comment))
  2. .bind("127.0.0.1", 8080)?
  3. .run()
  4. .await

转换为:

  1. let comment_scope = web::scope("/comments").service(delete_comment).service(update_comment).service(get_comments).service(create_comment);
  2. HttpServer::new(move || App::new().app_data(app_state.clone()).service(comment_scope))
  3. .bind("127.0.0.1", 8080)?
  4. .run()
  5. .await

但它一直告诉我,actix_web::Scope 没有实现 Clone 特性。
我该如何解决这个问题?

英文:

I want to group my app routes in scope, so I can separate their file location per Domain in the future.
what I'm trying to do is convert

  1. HttpServer::new(move || App::new().app_data(app_state.clone()).service(delete_comment).service(update_comment).service(get_comments).service(create_comment))
  2. .bind(("127.0.0.1", 8080))?
  3. .run()
  4. .await

to:

  1. let comment_scope = web::scope("/comments").service(delete_comment).service(update_comment).service(get_comments).service(create_comment);
  2. HttpServer::new(move || App::new().app_data(app_state.clone()).service(comment_scope))
  3. .bind(("127.0.0.1", 8080))?
  4. .run()
  5. .await

but it keeps telling me that the trait Clone is not implemented for actix_web::Scope.
how can I fix this?

答案1

得分: 5

你只需将 comment_scope 声明移到 new 闭包中:

  1. HttpServer::new(move || {
  2. let comment_scope = web::scope("/comments")
  3. .service(delete_comment)
  4. .service(update_comment)
  5. .service(get_comments)
  6. .service(create_comment);
  7. App::new()
  8. .service(comment_scope)
  9. })
  10. .bind(("127.0.0.1", 8080))?
  11. .run()
  12. .await
英文:

You just have to move the comment_scope declaration into new closure:

  1. HttpServer::new(move || {
  2. let comment_scope = web::scope("/comments")
  3. .service(delete_comment)
  4. .service(update_comment)
  5. .service(get_comments)
  6. .service(create_comment);
  7. App::new()
  8. .service(comment_scope)
  9. })
  10. .bind(("127.0.0.1", 8080))?
  11. .run()
  12. .await

答案2

得分: 0

使用TcpListener来尝试这个:

  1. use std::net::TcpListener;
  2. pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
  3. let server = HttpServer::new(|| {
  4. App::new()
  5. .route("/comments", web::post().to(comments))
  6. [/.../]
  7. })
  8. .listen(listener)?
  9. .run();
  10. Ok(server)
  11. }
英文:

How about try this by using TcpListener:

  1. use std::net::TcpListener;
  2. pub fn run(listener: TcpListener) -&gt; Result&lt;Server, std::io::Error&gt; {
  3. let server = HttpServer::new(|| {
  4. App::new()
  5. .route(&quot;/comments&quot;, web::post().to(comments))
  6. [/.../]
  7. })
  8. .listen(listener)?
  9. .run();
  10. Ok(server)
  11. }

huangapple
  • 本文由 发表于 2023年6月4日 23:03:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401029.html
匿名

发表评论

匿名网友

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

确定