diff --git a/Cargo.lock b/Cargo.lock index 553f5fb..ded9741 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1099,7 +1099,7 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "twirp" -version = "0.5.0" +version = "0.7.0" dependencies = [ "async-trait", "axum", @@ -1119,7 +1119,7 @@ dependencies = [ [[package]] name = "twirp-build" -version = "0.6.0" +version = "0.7.0" dependencies = [ "prost-build", ] diff --git a/crates/twirp-build/Cargo.toml b/crates/twirp-build/Cargo.toml index a646156..e690bc2 100644 --- a/crates/twirp-build/Cargo.toml +++ b/crates/twirp-build/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "twirp-build" -version = "0.6.0" +version = "0.7.0" authors = ["The blackbird team "] edition = "2021" description = "Code generation for async-compatible Twirp RPC interfaces." diff --git a/crates/twirp-build/src/lib.rs b/crates/twirp-build/src/lib.rs index c418909..0f11398 100644 --- a/crates/twirp-build/src/lib.rs +++ b/crates/twirp-build/src/lib.rs @@ -38,12 +38,29 @@ impl prost_build::ServiceGenerator for ServiceGenerator { } writeln!(buf, "}}").unwrap(); + writeln!(buf, "#[twirp::async_trait::async_trait]").unwrap(); + writeln!(buf, "impl {service_name} for std::sync::Arc").unwrap(); + writeln!(buf, "where").unwrap(); + writeln!(buf, " T: {service_name} + Sync + Send").unwrap(); + writeln!(buf, "{{").unwrap(); + for m in &service.methods { + writeln!( + buf, + " async fn {}(&self, ctx: twirp::Context, req: {}) -> Result<{}, twirp::TwirpErrorResponse> {{", + m.name, m.input_type, m.output_type, + ) + .unwrap(); + writeln!(buf, " (*self).{}(ctx, req).await", m.name).unwrap(); + writeln!(buf, " }}").unwrap(); + } + writeln!(buf, "}}").unwrap(); + // add_service writeln!( buf, - r#"pub fn router(api: std::sync::Arc) -> twirp::Router + r#"pub fn router(api: T) -> twirp::Router where - T: {service_name} + Send + Sync + 'static, + T: {service_name} + Clone + Send + Sync + 'static, {{ twirp::details::TwirpRouterBuilder::new(api)"#, ) @@ -54,7 +71,7 @@ where let rust_method_name = &m.name; writeln!( buf, - r#" .route("/{uri}", |api: std::sync::Arc, ctx: twirp::Context, req: {req_type}| async move {{ + r#" .route("/{uri}", |api: T, ctx: twirp::Context, req: {req_type}| async move {{ api.{rust_method_name}(ctx, req).await }})"#, ) diff --git a/crates/twirp/Cargo.toml b/crates/twirp/Cargo.toml index f633473..2497747 100644 --- a/crates/twirp/Cargo.toml +++ b/crates/twirp/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "twirp" -version = "0.5.0" +version = "0.7.0" authors = ["The blackbird team "] edition = "2021" description = "An async-compatible library for Twirp RPC in Rust." diff --git a/example/src/main.rs b/example/src/main.rs index 2376040..18d992a 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -1,5 +1,4 @@ use std::net::SocketAddr; -use std::sync::Arc; use std::time::UNIX_EPOCH; use twirp::async_trait::async_trait; @@ -24,7 +23,7 @@ async fn ping() -> &'static str { #[tokio::main] pub async fn main() { - let api_impl = Arc::new(HaberdasherApiServer {}); + let api_impl = HaberdasherApiServer {}; let middleware = twirp::tower::builder::ServiceBuilder::new() .layer(middleware::from_fn(request_id_middleware)); let twirp_routes = Router::new() @@ -45,6 +44,8 @@ pub async fn main() { } } +// Note: If your server type can't be Clone, consider wrapping it in `std::sync::Arc`. +#[derive(Clone)] struct HaberdasherApiServer; #[async_trait] @@ -151,7 +152,7 @@ mod test { } impl NetServer { - async fn start(api_impl: Arc) -> Self { + async fn start(api_impl: HaberdasherApiServer) -> Self { let twirp_routes = Router::new().nest(haberdash::SERVICE_FQN, haberdash::router(api_impl)); let app = Router::new() @@ -194,7 +195,7 @@ mod test { #[tokio::test] async fn test_net() { - let api_impl = Arc::new(HaberdasherApiServer {}); + let api_impl = HaberdasherApiServer {}; let server = NetServer::start(api_impl).await; let url = Url::parse(&format!("http://localhost:{}/twirp/", server.port)).unwrap(); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy