Skip to content
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

Add default out_dir support #262

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 17 additions & 17 deletions compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<'a> MethodGen<'a> {
&format!("struct {}Method {{", self.struct_name()),
"}",
|w| {
w.write_line(&format!(
w.write_line(format!(
"service: Arc<dyn {} + Send + Sync>,",
self.service_name
));
Expand All @@ -167,7 +167,7 @@ impl<'a> MethodGen<'a> {
|w| {
w.block("fn handler(&self, ctx: ::ttrpc::TtrpcContext, req: ::ttrpc::Request) -> ::ttrpc::Result<()> {", "}",
|w| {
w.write_line(&format!("::ttrpc::request_handler!(self, ctx, req, {}, {}, {});",
w.write_line(format!("::ttrpc::request_handler!(self, ctx, req, {}, {}, {});",
proto_path_to_rust_mod(self.root_scope.find_message(self.proto.get_input_type()).get_scope().get_file_descriptor().get_name()),
self.root_scope.find_message(self.proto.get_input_type()).rust_name(),
self.name()));
Expand All @@ -184,7 +184,7 @@ impl<'a> MethodGen<'a> {
|w| {
w.block("async fn handler(&self, ctx: ::ttrpc::r#async::TtrpcContext, req: ::ttrpc::Request) -> ::ttrpc::Result<::ttrpc::Response> {", "}",
|w| {
w.write_line(&format!("::ttrpc::async_request_handler!(self, ctx, req, {}, {}, {});",
w.write_line(format!("::ttrpc::async_request_handler!(self, ctx, req, {}, {}, {});",
proto_path_to_rust_mod(self.root_scope.find_message(self.proto.get_input_type()).get_scope().get_file_descriptor().get_name()),
self.root_scope.find_message(self.proto.get_input_type()).rust_name(),
self.name()));
Expand All @@ -197,7 +197,7 @@ impl<'a> MethodGen<'a> {
|w| {
w.block("async fn handler(&self, ctx: ::ttrpc::r#async::TtrpcContext, inner: ::ttrpc::r#async::StreamInner) -> ::ttrpc::Result<Option<::ttrpc::Response>> {", "}",
|w| {
w.write_line(&format!("::ttrpc::async_client_streamimg_handler!(self, ctx, inner, {});",
w.write_line(format!("::ttrpc::async_client_streamimg_handler!(self, ctx, inner, {});",
self.name()));
});
});
Expand All @@ -208,7 +208,7 @@ impl<'a> MethodGen<'a> {
|w| {
w.block("async fn handler(&self, ctx: ::ttrpc::r#async::TtrpcContext, mut inner: ::ttrpc::r#async::StreamInner) -> ::ttrpc::Result<Option<::ttrpc::Response>> {", "}",
|w| {
w.write_line(&format!("::ttrpc::async_server_streamimg_handler!(self, ctx, inner, {}, {}, {});",
w.write_line(format!("::ttrpc::async_server_streamimg_handler!(self, ctx, inner, {}, {}, {});",
proto_path_to_rust_mod(self.root_scope.find_message(self.proto.get_input_type()).get_scope().get_file_descriptor().get_name()),
self.root_scope.find_message(self.proto.get_input_type()).rust_name(),
self.name()));
Expand All @@ -221,7 +221,7 @@ impl<'a> MethodGen<'a> {
|w| {
w.block("async fn handler(&self, ctx: ::ttrpc::r#async::TtrpcContext, inner: ::ttrpc::r#async::StreamInner) -> ::ttrpc::Result<Option<::ttrpc::Response>> {", "}",
|w| {
w.write_line(&format!("::ttrpc::async_duplex_streamimg_handler!(self, ctx, inner, {});",
w.write_line(format!("::ttrpc::async_duplex_streamimg_handler!(self, ctx, inner, {});",
self.name()));
});
});
Expand Down Expand Up @@ -277,8 +277,8 @@ impl<'a> MethodGen<'a> {
let method_name = self.name();
if let MethodType::Unary = self.method_type().0 {
w.pub_fn(&self.unary(&method_name), |w| {
w.write_line(&format!("let mut cres = {}::new();", self.output()));
w.write_line(&format!(
w.write_line(format!("let mut cres = {}::new();", self.output()));
w.write_line(format!(
"::ttrpc::client_request!(self, ctx, req, \"{}.{}\", \"{}\", cres);",
self.package_name,
self.service_name,
Expand All @@ -295,8 +295,8 @@ impl<'a> MethodGen<'a> {
// Unary RPC
MethodType::Unary => {
pub_async_fn(w, &self.unary(&method_name), |w| {
w.write_line(&format!("let mut cres = {}::new();", self.output()));
w.write_line(&format!(
w.write_line(format!("let mut cres = {}::new();", self.output()));
w.write_line(format!(
"::ttrpc::async_client_request!(self, ctx, req, \"{}.{}\", \"{}\", cres);",
self.package_name,
self.service_name,
Expand All @@ -307,7 +307,7 @@ impl<'a> MethodGen<'a> {
// Client Streaming RPC
MethodType::ClientStreaming => {
pub_async_fn(w, &self.client_streaming(&method_name), |w| {
w.write_line(&format!(
w.write_line(format!(
"::ttrpc::async_client_stream_send!(self, ctx, \"{}.{}\", \"{}\");",
self.package_name,
self.service_name,
Expand All @@ -318,7 +318,7 @@ impl<'a> MethodGen<'a> {
// Server Streaming RPC
MethodType::ServerStreaming => {
pub_async_fn(w, &self.server_streaming(&method_name), |w| {
w.write_line(&format!(
w.write_line(format!(
"::ttrpc::async_client_stream_receive!(self, ctx, req, \"{}.{}\", \"{}\");",
self.package_name,
self.service_name,
Expand All @@ -329,7 +329,7 @@ impl<'a> MethodGen<'a> {
// Bidirectional streaming RPC
MethodType::Duplex => {
pub_async_fn(w, &self.duplex_streaming(&method_name), |w| {
w.write_line(&format!(
w.write_line(format!(
"::ttrpc::async_client_stream!(self, ctx, \"{}.{}\", \"{}\");",
self.package_name,
self.service_name,
Expand Down Expand Up @@ -496,13 +496,13 @@ impl<'a> ServiceGen<'a> {

fn write_sync_client(&self, w: &mut CodeWriter) {
w.write_line("#[derive(Clone)]");
w.pub_struct(&self.client_name(), |w| {
w.pub_struct(self.client_name(), |w| {
w.field_decl("client", "::ttrpc::Client");
});

w.write_line("");

w.impl_self_block(&self.client_name(), |w| {
w.impl_self_block(self.client_name(), |w| {
w.pub_fn("new(client: ::ttrpc::Client) -> Self", |w| {
w.expr_block(&self.client_name(), |w| {
w.write_line("client,");
Expand All @@ -518,13 +518,13 @@ impl<'a> ServiceGen<'a> {

fn write_async_client(&self, w: &mut CodeWriter) {
w.write_line("#[derive(Clone)]");
w.pub_struct(&self.client_name(), |w| {
w.pub_struct(self.client_name(), |w| {
w.field_decl("client", "::ttrpc::r#async::Client");
});

w.write_line("");

w.impl_self_block(&self.client_name(), |w| {
w.impl_self_block(self.client_name(), |w| {
w.pub_fn("new(client: ::ttrpc::r#async::Client) -> Self", |w| {
w.expr_block(&self.client_name(), |w| {
w.write_line("client,");
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel="1.77.0"
channel="1.81.0"
profile="default"
components=["rustfmt", "clippy"]
27 changes: 13 additions & 14 deletions src/asynchronous/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use nix::unistd::close;
use tokio::{self, sync::mpsc, task};

use crate::common::client_connect;
Expand Down Expand Up @@ -161,19 +160,19 @@ impl Client {
))
}
}

struct ClientClose {
fd: RawFd,
close_fd: RawFd,
}

impl Drop for ClientClose {
fn drop(&mut self) {
close(self.close_fd).unwrap();
close(self.fd).unwrap();
trace!("All client is droped");
}
}
// Annotate the code because it has not been used
// struct ClientClose {
// fd: RawFd,
// close_fd: RawFd,
// }

// impl Drop for ClientClose {
// fn drop(&mut self) {
// close(self.close_fd).unwrap();
// close(self.fd).unwrap();
// trace!("All client is droped");
// }
// }

#[derive(Debug)]
struct ClientBuilder {
Expand Down
26 changes: 19 additions & 7 deletions ttrpc-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
//! .run()
//! .expect("Gen async code failed.");
//! }
//! ```
//! If there's no out_dir and use 'gen_mod' feature
//! You can use the following method to include the target file
//! include!(concat!(env!("OUT_DIR"), "/mod.rs"));

pub use protobuf_codegen::{
Customize as ProtobufCustomize, CustomizeCallback as ProtobufCustomizeCallback,
Expand All @@ -45,8 +49,8 @@ mod str_lit;
/// Invoke pure rust codegen.
#[derive(Debug, Default)]
pub struct Codegen {
/// --lang_out= param
out_dir: PathBuf,
/// --lang_out= param ,if out_dir is none ,will use env 'OUT_DIR' path
out_dir: Option<PathBuf>,
/// -I args
includes: Vec<PathBuf>,
/// List of .proto files to compile
Expand All @@ -65,9 +69,9 @@ impl Codegen {
Self::default()
}

/// Set the output directory for codegen.
/// Set the output directory for codegen. Support None out_dir
pub fn out_dir(&mut self, out_dir: impl AsRef<Path>) -> &mut Self {
self.out_dir = out_dir.as_ref().to_owned();
self.out_dir = Some(out_dir.as_ref().to_owned());
self
}

Expand Down Expand Up @@ -132,11 +136,19 @@ impl Codegen {
let includes: Vec<&Path> = self.includes.iter().map(|p| p.as_path()).collect();
let inputs: Vec<&Path> = self.inputs.iter().map(|p| p.as_path()).collect();
let p = parse_and_typecheck(&includes, &inputs)?;
// If out_dir is none ,dst_path will be setting in path_dir
let dst_path = self.out_dir.clone().unwrap_or_else(|| {
// Add default path from env OUT_DIR, if no OUT_DIR env ,that's will be current path
std::env::var("OUT_DIR").map_or_else(
|_| std::env::current_dir().unwrap_or_default(),
PathBuf::from,
)
});

if self.rust_protobuf {
self.rust_protobuf_codegen
.pure()
.out_dir(&self.out_dir)
.out_dir(&dst_path)
.inputs(&self.inputs)
.includes(&self.includes)
.run()
Expand All @@ -146,7 +158,7 @@ impl Codegen {
ttrpc_compiler::codegen::gen_and_write(
&p.file_descriptors,
&p.relative_paths,
&self.out_dir,
&dst_path,
&self.customize,
)
}
Expand Down Expand Up @@ -245,7 +257,7 @@ impl<'a> Run<'a> {
}

fn add_file(&mut self, protobuf_path: &str, fs_path: &Path) -> io::Result<()> {
if self.parsed_files.get(protobuf_path).is_some() {
if self.parsed_files.contains_key(protobuf_path) {
return Ok(());
}

Expand Down
8 changes: 4 additions & 4 deletions ttrpc-codegen/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ trait ToChar {

impl ToI32 for u64 {
fn to_i32(&self) -> ParserResult<i32> {
if *self <= i32::max_value() as u64 {
if *self <= i32::MAX as u64 {
Ok(*self as i32)
} else {
Err(ParserError::IntegerOverflow)
Expand All @@ -116,7 +116,7 @@ impl ToI32 for u64 {

impl ToI32 for i64 {
fn to_i32(&self) -> ParserResult<i32> {
if *self <= i32::max_value() as i64 && *self >= i32::min_value() as i64 {
if *self <= i32::MAX as i64 && *self >= i32::MIN as i64 {
Ok(*self as i32)
} else {
Err(ParserError::IntegerOverflow)
Expand All @@ -126,7 +126,7 @@ impl ToI32 for i64 {

impl ToI64 for u64 {
fn to_i64(&self) -> Result<i64, ParserError> {
if *self <= i64::max_value() as u64 {
if *self <= i64::MAX as u64 {
Ok(*self as i64)
} else {
Err(ParserError::IntegerOverflow)
Expand Down Expand Up @@ -1378,7 +1378,7 @@ impl<'a> Parser<'a> {
let from = self.next_field_number()?;
let to = if self.next_ident_if_eq("to")? {
if self.next_ident_if_eq("max")? {
i32::max_value()
i32::MAX
} else {
self.next_field_number()?
}
Expand Down
Loading