From f2e430505d079d873cbe4615f3eb821f0b78fb16 Mon Sep 17 00:00:00 2001 From: Pavel Bakun Date: Tue, 4 Jul 2023 19:33:12 +0300 Subject: [PATCH] add generics example to actix-web --- actix-web/examples/generics.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 actix-web/examples/generics.rs diff --git a/actix-web/examples/generics.rs b/actix-web/examples/generics.rs new file mode 100644 index 000000000..f27ae6618 --- /dev/null +++ b/actix-web/examples/generics.rs @@ -0,0 +1,34 @@ +use actix_web::{web, App, Error, HttpResponse, HttpServer, Result}; + +async fn index(api: web::Data) -> Result { + let hi = api.dont_worry(); + println!("{}", hi); + + Ok(HttpResponse::Ok().body(hi)) +} + +pub trait ApiProvider { + fn dont_worry(&self) -> String; +} + +pub struct Api {} + +impl ApiProvider for Api { + fn dont_worry(&self) -> String { + "be happy".to_string() + } +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + let api = web::Data::new(Api {}); + + HttpServer::new(move || { + App::new() + .app_data(api.clone()) + .service(web::resource("/").route(web::get().to(index::))) + }) + .bind(("127.0.0.1", 8080))? + .run() + .await +}