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 +}