Skip to content

Don't require Arc in router #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/twirp-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "twirp-build"
version = "0.6.0"
version = "0.7.0"
authors = ["The blackbird team <support@github.com>"]
edition = "2021"
description = "Code generation for async-compatible Twirp RPC interfaces."
Expand Down
23 changes: 20 additions & 3 deletions crates/twirp-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,29 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
}
writeln!(buf, "}}").unwrap();

writeln!(buf, "#[twirp::async_trait::async_trait]").unwrap();
writeln!(buf, "impl<T> {service_name} for std::sync::Arc<T>").unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you could actually impl for AsRef<T> rather than Arc<T> and cover more cases. Arc has a blanket impl for AsRef I believe.

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<T>(api: std::sync::Arc<T>) -> twirp::Router
r#"pub fn router<T>(api: T) -> twirp::Router
where
T: {service_name} + Send + Sync + 'static,
T: {service_name} + Clone + Send + Sync + 'static,
{{
twirp::details::TwirpRouterBuilder::new(api)"#,
)
Expand All @@ -54,7 +71,7 @@ where
let rust_method_name = &m.name;
writeln!(
buf,
r#" .route("/{uri}", |api: std::sync::Arc<T>, 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
}})"#,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/twirp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "twirp"
version = "0.5.0"
version = "0.7.0"
authors = ["The blackbird team <support@github.com>"]
edition = "2021"
description = "An async-compatible library for Twirp RPC in Rust."
Expand Down
9 changes: 5 additions & 4 deletions example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::UNIX_EPOCH;

use twirp::async_trait::async_trait;
Expand All @@ -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()
Expand All @@ -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]
Expand Down Expand Up @@ -151,7 +152,7 @@ mod test {
}

impl NetServer {
async fn start(api_impl: Arc<HaberdasherApiServer>) -> 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()
Expand Down Expand Up @@ -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();
Expand Down
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