Skip to content

Commit 878b566

Browse files
authored
Merge pull request #131 from Tim-Zhang/make-utils-private
Make unnecessarily public functions/mod private
2 parents adb5b01 + 3240339 commit 878b566

File tree

17 files changed

+62
-68
lines changed

17 files changed

+62
-68
lines changed

example/async-server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use std::sync::Arc;
1313
use log::LevelFilter;
1414

1515
use protocols::r#async::{agent, agent_ttrpc, health, health_ttrpc, types};
16-
use ttrpc::asynchronous::server::*;
16+
use ttrpc::asynchronous::Server;
1717
use ttrpc::error::{Error, Result};
18-
use ttrpc::ttrpc::{Code, Status};
18+
use ttrpc::proto::{Code, Status};
1919

2020
use async_trait::async_trait;
2121
use tokio::signal::unix::{signal, SignalKind};

example/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ mod protocols;
1616

1717
use protocols::sync::{agent, agent_ttrpc, health, health_ttrpc};
1818
use std::thread;
19-
use ttrpc::client::Client;
2019
use ttrpc::context::{self, Context};
20+
use ttrpc::Client;
2121

2222
fn main() {
2323
let c = Client::connect("unix://@/tmp/1").unwrap();

example/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use std::thread;
2323

2424
use protocols::sync::{agent, agent_ttrpc, health, health_ttrpc, types};
2525
use ttrpc::error::{Error, Result};
26-
use ttrpc::server::*;
27-
use ttrpc::ttrpc::{Code, Status};
26+
use ttrpc::proto::{Code, Status};
27+
use ttrpc::Server;
2828

2929
struct HealthService;
3030
impl health_ttrpc::Health for HealthService {

src/asynchronous/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::sync::{Arc, Mutex};
1111

1212
use crate::common::{client_connect, MESSAGE_TYPE_RESPONSE};
1313
use crate::error::{Error, Result};
14-
use crate::ttrpc::{Code, Request, Response};
14+
use crate::proto::{Code, Request, Response};
1515

1616
use crate::asynchronous::stream::{receive, to_req_buf};
1717
use crate::r#async::utils;

src/asynchronous/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55

66
//! Server and client in async mode (alias r#async).
77
8-
pub mod client;
9-
pub mod server;
10-
pub mod stream;
8+
mod client;
9+
mod server;
10+
mod stream;
1111
#[macro_use]
12-
pub mod utils;
12+
#[doc(hidden)]
13+
mod utils;
1314
mod unix_incoming;
1415

1516
#[doc(inline)]
1617
pub use crate::r#async::client::Client;
1718
#[doc(inline)]
1819
pub use crate::r#async::server::Server;
1920
#[doc(inline)]
20-
pub use crate::r#async::utils::{convert_response_to_buf, MethodHandler, TtrpcContext};
21+
pub use utils::{MethodHandler, TtrpcContext};

src/asynchronous/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use crate::asynchronous::unix_incoming::UnixIncoming;
1616
use crate::common::{self, Domain, MESSAGE_TYPE_REQUEST};
1717
use crate::context;
1818
use crate::error::{get_status, Error, Result};
19+
use crate::proto::{Code, Status};
1920
use crate::r#async::{MethodHandler, TtrpcContext};
20-
use crate::ttrpc::{Code, Status};
2121
use crate::MessageHeader;
2222
use futures::stream::Stream;
2323
use futures::StreamExt as _;
@@ -145,7 +145,7 @@ impl Server {
145145
}
146146
}
147147

148-
pub async fn do_start<I, S>(&mut self, mut incoming: I) -> Result<()>
148+
async fn do_start<I, S>(&mut self, mut incoming: I) -> Result<()>
149149
where
150150
I: Stream<Item = std::io::Result<S>> + Unpin + Send + 'static + AsRawFd,
151151
S: AsyncRead + AsyncWrite + AsRawFd + Send + 'static,

src/asynchronous/stream.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use byteorder::{BigEndian, ByteOrder};
77

88
use crate::common::{MESSAGE_HEADER_LENGTH, MESSAGE_LENGTH_MAX, MESSAGE_TYPE_RESPONSE};
99
use crate::error::{get_rpc_status, sock_error_msg, Error, Result};
10+
use crate::proto::{Code, Response, Status};
1011
use crate::r#async::utils;
11-
use crate::ttrpc::{Code, Response, Status};
1212
use crate::MessageHeader;
1313
use protobuf::Message;
1414
use tokio::io::AsyncReadExt;
@@ -51,7 +51,7 @@ where
5151
Ok(mh)
5252
}
5353

54-
pub async fn receive<T>(reader: &mut T) -> Result<(MessageHeader, Vec<u8>)>
54+
pub(crate) async fn receive<T>(reader: &mut T) -> Result<(MessageHeader, Vec<u8>)>
5555
where
5656
T: AsyncReadExt + std::marker::Unpin,
5757
{
@@ -94,15 +94,15 @@ fn header_to_buf(mh: MessageHeader) -> Vec<u8> {
9494
buf
9595
}
9696

97-
pub fn to_req_buf(stream_id: u32, mut body: Vec<u8>) -> Vec<u8> {
97+
pub(crate) fn to_req_buf(stream_id: u32, mut body: Vec<u8>) -> Vec<u8> {
9898
let header = utils::get_request_header_from_body(stream_id, &body);
9999
let mut buf = header_to_buf(header);
100100
buf.append(&mut body);
101101

102102
buf
103103
}
104104

105-
pub fn to_res_buf(stream_id: u32, mut body: Vec<u8>) -> Vec<u8> {
105+
pub(crate) fn to_res_buf(stream_id: u32, mut body: Vec<u8>) -> Vec<u8> {
106106
let header = utils::get_response_header_from_body(stream_id, &body);
107107
let mut buf = header_to_buf(header);
108108
buf.append(&mut body);
@@ -119,7 +119,7 @@ fn get_response_body(res: &Response) -> Result<Vec<u8>> {
119119
Ok(buf)
120120
}
121121

122-
pub async fn respond(
122+
pub(crate) async fn respond(
123123
tx: tokio::sync::mpsc::Sender<Vec<u8>>,
124124
stream_id: u32,
125125
body: Vec<u8>,
@@ -131,7 +131,7 @@ pub async fn respond(
131131
.map_err(err_to_others_err!(e, "Send packet to sender error "))
132132
}
133133

134-
pub async fn respond_with_status(
134+
pub(crate) async fn respond_with_status(
135135
tx: tokio::sync::mpsc::Sender<Vec<u8>>,
136136
stream_id: u32,
137137
status: Status,

src/asynchronous/utils.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//
55

66
use crate::common::{MessageHeader, MESSAGE_TYPE_REQUEST, MESSAGE_TYPE_RESPONSE};
7-
use crate::error::{get_status, Error, Result};
8-
use crate::ttrpc::{Code, Request, Response, Status};
7+
use crate::error::{get_status, Result};
8+
use crate::proto::{Code, Request, Status};
99
use async_trait::async_trait;
1010
use protobuf::{CodedInputStream, Message};
1111
use std::collections::HashMap;
@@ -47,7 +47,11 @@ macro_rules! async_request_handler {
4747
},
4848
}
4949

50-
let buf = ::ttrpc::r#async::convert_response_to_buf(res)?;
50+
let mut buf = Vec::with_capacity(res.compute_size() as usize);
51+
let mut s = protobuf::CodedOutputStream::vec(&mut buf);
52+
res.write_to(&mut s).map_err(ttrpc::err_to_others!(e, ""))?;
53+
s.flush().map_err(ttrpc::err_to_others!(e, ""))?;
54+
5155
return Ok(($ctx.mh.stream_id, buf));
5256
};
5357
}
@@ -95,16 +99,7 @@ pub struct TtrpcContext {
9599
pub timeout_nano: i64,
96100
}
97101

98-
pub fn convert_response_to_buf(res: Response) -> Result<Vec<u8>> {
99-
let mut buf = Vec::with_capacity(res.compute_size() as usize);
100-
let mut s = protobuf::CodedOutputStream::vec(&mut buf);
101-
res.write_to(&mut s).map_err(err_to_others_err!(e, ""))?;
102-
s.flush().map_err(err_to_others_err!(e, ""))?;
103-
104-
Ok(buf)
105-
}
106-
107-
pub fn get_response_header_from_body(stream_id: u32, body: &[u8]) -> MessageHeader {
102+
pub(crate) fn get_response_header_from_body(stream_id: u32, body: &[u8]) -> MessageHeader {
108103
MessageHeader {
109104
length: body.len() as u32,
110105
stream_id,
@@ -113,7 +108,7 @@ pub fn get_response_header_from_body(stream_id: u32, body: &[u8]) -> MessageHead
113108
}
114109
}
115110

116-
pub fn get_request_header_from_body(stream_id: u32, body: &[u8]) -> MessageHeader {
111+
pub(crate) fn get_request_header_from_body(stream_id: u32, body: &[u8]) -> MessageHeader {
117112
MessageHeader {
118113
length: body.len() as u32,
119114
stream_id,
@@ -122,7 +117,7 @@ pub fn get_request_header_from_body(stream_id: u32, body: &[u8]) -> MessageHeade
122117
}
123118
}
124119

125-
pub fn new_unix_stream_from_raw_fd(fd: RawFd) -> UnixStream {
120+
pub(crate) fn new_unix_stream_from_raw_fd(fd: RawFd) -> UnixStream {
126121
let std_stream: std::os::unix::net::UnixStream;
127122
unsafe {
128123
std_stream = std::os::unix::net::UnixStream::from_raw_fd(fd);
@@ -133,7 +128,7 @@ pub fn new_unix_stream_from_raw_fd(fd: RawFd) -> UnixStream {
133128
UnixStream::from_std(std_stream).unwrap()
134129
}
135130

136-
pub fn body_to_request(body: &[u8]) -> StdResult<Request, Status> {
131+
pub(crate) fn body_to_request(body: &[u8]) -> StdResult<Request, Status> {
137132
let mut req = Request::new();
138133
let merge_result;
139134
{
@@ -150,6 +145,6 @@ pub fn body_to_request(body: &[u8]) -> StdResult<Request, Status> {
150145
Ok(req)
151146
}
152147

153-
pub fn get_path(service: &str, method: &str) -> String {
148+
pub(crate) fn get_path(service: &str, method: &str) -> String {
154149
format!("/{}/{}", service, method)
155150
}

src/common.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
//! Common functions and macros.
77
8-
#![allow(unused_macros)]
9-
108
use crate::error::{Error, Result};
119
#[cfg(any(feature = "async", not(target_os = "linux")))]
1210
use nix::fcntl::FdFlag;

src/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44
//
55

6-
use crate::ttrpc::KeyValue;
6+
use crate::proto::KeyValue;
77
use std::collections::HashMap;
88

99
#[derive(Clone, Default, Debug)]
@@ -78,7 +78,7 @@ pub fn to_pb(kvs: HashMap<String, Vec<String>>) -> protobuf::RepeatedField<KeyVa
7878
#[cfg(test)]
7979
mod tests {
8080
use crate::context;
81-
use crate::ttrpc::KeyValue;
81+
use crate::proto::KeyValue;
8282

8383
#[test]
8484
fn test_metadata() {

0 commit comments

Comments
 (0)