Skip to content

Commit 663d4d7

Browse files
authored
Merge pull request #235 from Xynnn007/arcer
Codegen: convert Arc<Box<T>> to Arc<T>
2 parents f681eb1 + 444a490 commit 663d4d7

File tree

8 files changed

+16
-30
lines changed

8 files changed

+16
-30
lines changed

compiler/src/codegen.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'a> MethodGen<'a> {
145145
"}",
146146
|w| {
147147
w.write_line(&format!(
148-
"service: Arc<Box<dyn {} + Send + Sync>>,",
148+
"service: Arc<dyn {} + Send + Sync>,",
149149
self.service_name
150150
));
151151
},
@@ -558,7 +558,7 @@ impl<'a> ServiceGen<'a> {
558558
fn write_sync_server_create(&self, w: &mut CodeWriter) {
559559
let method_handler_name = "::ttrpc::MethodHandler";
560560
let s = format!(
561-
"create_{}(service: Arc<Box<dyn {} + Send + Sync>>) -> HashMap<String, Box<dyn {} + Send + Sync>>",
561+
"create_{}(service: Arc<dyn {} + Send + Sync>) -> HashMap<String, Box<dyn {} + Send + Sync>>",
562562
to_snake_case(&self.service_name()),
563563
self.service_name(),
564564
method_handler_name,
@@ -577,7 +577,7 @@ impl<'a> ServiceGen<'a> {
577577

578578
fn write_async_server_create(&self, w: &mut CodeWriter) {
579579
let s = format!(
580-
"create_{}(service: Arc<Box<dyn {} + Send + Sync>>) -> HashMap<String, {}>",
580+
"create_{}(service: Arc<dyn {} + Send + Sync>) -> HashMap<String, {}>",
581581
to_snake_case(&self.service_name()),
582582
self.service_name(),
583583
"::ttrpc::r#async::Service"
@@ -642,7 +642,6 @@ fn write_generated_common(w: &mut CodeWriter) {
642642
w.write_line("#![cfg_attr(rustfmt, rustfmt_skip)]");
643643
w.write_line("#![allow(unknown_lints)]");
644644
w.write_line("#![allow(clipto_camel_casepy)]");
645-
w.write_line("#![allow(box_pointers)]");
646645
w.write_line("#![allow(dead_code)]");
647646
w.write_line("#![allow(missing_docs)]");
648647
w.write_line("#![allow(non_camel_case_types)]");

example/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ path = "./async-stream-client.rs"
5151

5252
[build-dependencies]
5353
ttrpc-codegen = { path = "../ttrpc-codegen"}
54+
55+
[patch.crates-io]
56+
ttrpc-compiler = { path = "../compiler"}
57+

example/async-client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
mod protocols;
77
mod utils;
88
#[cfg(unix)]
9-
use protocols::r#async::{agent, agent_ttrpc, health, health_ttrpc};
9+
use protocols::asynchronous::{agent, agent_ttrpc, health, health_ttrpc};
1010
use ttrpc::context::{self, Context};
1111
#[cfg(unix)]
1212
use ttrpc::r#async::Client;

example/async-server.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::sync::Arc;
1414
use log::LevelFilter;
1515

1616
#[cfg(unix)]
17-
use protocols::r#async::{agent, agent_ttrpc, health, health_ttrpc, types};
17+
use protocols::asynchronous::{agent, agent_ttrpc, health, health_ttrpc, types};
1818
#[cfg(unix)]
1919
use ttrpc::asynchronous::Server;
2020
use ttrpc::error::{Error, Result};
@@ -97,13 +97,8 @@ fn main() {
9797
async fn main() {
9898
simple_logging::log_to_stderr(LevelFilter::Trace);
9999

100-
let h = Box::new(HealthService {}) as Box<dyn health_ttrpc::Health + Send + Sync>;
101-
let h = Arc::new(h);
102-
let hservice = health_ttrpc::create_health(h);
103-
104-
let a = Box::new(AgentService {}) as Box<dyn agent_ttrpc::AgentService + Send + Sync>;
105-
let a = Arc::new(a);
106-
let aservice = agent_ttrpc::create_agent_service(a);
100+
let hservice = health_ttrpc::create_health(Arc::new(HealthService {}));
101+
let aservice = agent_ttrpc::create_agent_service(Arc::new(AgentService {}));
107102

108103
utils::remove_if_sock_exist(utils::SOCK_ADDR).unwrap();
109104

example/async-stream-client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
mod protocols;
77
mod utils;
88
#[cfg(unix)]
9-
use protocols::r#async::{empty, streaming, streaming_ttrpc};
9+
use protocols::asynchronous::{empty, streaming, streaming_ttrpc};
1010
use ttrpc::context::{self, Context};
1111
#[cfg(unix)]
1212
use ttrpc::r#async::Client;

example/async-stream-server.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::sync::Arc;
1111
use log::{info, LevelFilter};
1212

1313
#[cfg(unix)]
14-
use protocols::r#async::{empty, streaming, streaming_ttrpc};
14+
use protocols::asynchronous::{empty, streaming, streaming_ttrpc};
1515
#[cfg(unix)]
1616
use ttrpc::{asynchronous::Server, Error};
1717

@@ -163,11 +163,7 @@ fn main() {
163163
#[tokio::main(flavor = "current_thread")]
164164
async fn main() {
165165
simple_logging::log_to_stderr(LevelFilter::Info);
166-
167-
let s = Box::new(StreamingService {}) as Box<dyn streaming_ttrpc::Streaming + Send + Sync>;
168-
let s = Arc::new(s);
169-
let service = streaming_ttrpc::create_streaming(s);
170-
166+
let service = streaming_ttrpc::create_streaming(Arc::new(StreamingService {}));
171167
utils::remove_if_sock_exist(utils::SOCK_ADDR).unwrap();
172168

173169
let mut server = Server::new()

example/protocols/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44
//
55
#[cfg(unix)]
66
pub mod asynchronous;
7-
#[cfg(unix)]
8-
pub use asynchronous as r#async;
97
pub mod sync;

example/server.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,8 @@ impl agent_ttrpc::AgentService for AgentService {
8181

8282
fn main() {
8383
simple_logging::log_to_stderr(LevelFilter::Trace);
84-
85-
let h = Box::new(HealthService {}) as Box<dyn health_ttrpc::Health + Send + Sync>;
86-
let h = Arc::new(h);
87-
let hservice = health_ttrpc::create_health(h);
88-
89-
let a = Box::new(AgentService {}) as Box<dyn agent_ttrpc::AgentService + Send + Sync>;
90-
let a = Arc::new(a);
91-
let aservice = agent_ttrpc::create_agent_service(a);
84+
let hservice = health_ttrpc::create_health(Arc::new(HealthService {}));
85+
let aservice = agent_ttrpc::create_agent_service(Arc::new(AgentService {}));
9286

9387
utils::remove_if_sock_exist(utils::SOCK_ADDR).unwrap();
9488
let mut server = Server::new()

0 commit comments

Comments
 (0)