Skip to content

Commit

Permalink
Lint to fix clippy's CI errors
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Barman <[email protected]>
  • Loading branch information
lb034582341 committed Jul 14, 2023
1 parent 52700e1 commit c0eb577
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 69 deletions.
7 changes: 4 additions & 3 deletions benchmark/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ use grpcio::GrpcSlice;

fn gen_resp(req: &SimpleRequest) -> SimpleResponse {
let payload = util::new_payload(req.response_size as usize);
let mut resp = SimpleResponse::default();
resp.payload = Some(payload).into();
resp
SimpleResponse {
payload: Some(payload).into(),
..SimpleResponse::default()
}
}

#[derive(Clone)]
Expand Down
25 changes: 13 additions & 12 deletions benchmark/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ impl Server {

pub fn get_stats(&mut self, reset: bool) -> ServerStats {
let sample = self.recorder.cpu_time(reset);

let mut stats = ServerStats::default();
stats.time_elapsed = sample.real_time;
stats.time_user = sample.user_time;
stats.time_system = sample.sys_time;
stats.total_cpu_time = sample.total_cpu;
stats.idle_cpu_time = sample.idle_cpu;
stats
ServerStats {
time_elapsed: sample.real_time,
time_user: sample.user_time,
time_system: sample.sys_time,
total_cpu_time: sample.total_cpu,
idle_cpu_time: sample.idle_cpu,
..ServerStats::default()
}
}

pub fn shutdown(&mut self) -> ShutdownFuture {
Expand All @@ -118,9 +118,10 @@ impl Server {
}

pub fn get_status(&self) -> ServerStatus {
let mut status = ServerStatus::default();
status.port = self.port as i32;
status.cores = util::cpu_num_cores() as i32;
status
ServerStatus {
port: self.port as i32,
cores: util::cpu_num_cores() as i32,
..ServerStatus::default()
}
}
}
16 changes: 9 additions & 7 deletions benchmark/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,15 @@ impl Histogram {
}

pub fn report(&mut self, reset: bool) -> HistogramData {
let mut data = HistogramData::default();
data.count = f64::from(self.count);
data.sum = self.sum;
data.sum_of_squares = self.sum_of_squares;
data.min_seen = self.min;
data.max_seen = self.max;
data.bucket = self.buckets.clone();
let data = HistogramData {
count: f64::from(self.count),
sum: self.sum,
sum_of_squares: self.sum_of_squares,
min_seen: self.min,
max_seen: self.max,
bucket: self.buckets.clone(),
..HistogramData::default()
};
if reset {
self.clear();
}
Expand Down
12 changes: 8 additions & 4 deletions benchmark/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ impl WorkerService for Worker {

info!("receive client mark: {:?}", mark);
let stats = client.get_stats(mark.reset);
let mut status = ClientStatus::default();
status.stats = Some(stats).into();
let status = ClientStatus {
stats: Some(stats).into(),
..ClientStatus::default()
};
sink.send((status, WriteFlags::default())).await?;
}
client.shutdown().await;
Expand All @@ -110,8 +112,10 @@ impl WorkerService for Worker {

fn core_count(&mut self, ctx: RpcContext, _: CoreRequest, sink: UnarySink<CoreResponse>) {
let cpu_count = util::cpu_num_cores();
let mut resp = CoreResponse::default();
resp.cores = cpu_count as i32;
let resp = CoreResponse {
cores: cpu_count as i32,
..CoreResponse::default()
};
ctx.spawn(
sink.success(resp)
.map_err(|e| error!("failed to report cpu count: {:?}", e))
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ struct ServiceGen<'a> {
}

#[cfg(feature = "protobuf-codec")]
fn service_path<'a>(proto: &'a ServiceDescriptorProto, file: &FileDescriptorProto) -> String {
fn service_path(proto: &ServiceDescriptorProto, file: &FileDescriptorProto) -> String {
if file.get_package().is_empty() {
format!("/{}", proto.get_name())
} else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<'a> Iterator for NameSpliter<'a> {
let mut meet_lower = false;
for i in self.pos..self.name.len() {
let c = self.name[i];
if (b'A'..=b'Z').contains(&c) {
if c.is_ascii_uppercase() {
if meet_lower {
// So it should be AaA or aaA
pos = i;
Expand Down
85 changes: 54 additions & 31 deletions interop/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ impl Client {

pub async fn large_unary(&self) -> grpcio::Result<()> {
print!("testing large unary ... ");
let mut req = SimpleRequest::default();
req.response_size = 314_159;
req.payload = Some(util::new_payload(271_828)).into();
let req = SimpleRequest {
response_size: 314_159,
payload: Some(util::new_payload(271_828)).into(),
..SimpleRequest::default()
};
let resp = self.client.unary_call_async(&req)?.await?;
#[cfg(feature = "protobuf-codec")]
assert_eq!(314_159, resp.get_payload().get_body().len());
Expand All @@ -63,8 +65,10 @@ impl Client {
let payload_size = vec![27182usize, 8, 1828, 45904];
let (mut sender, receiver) = self.client.streaming_input_call()?;
for size in payload_size {
let mut req = StreamingInputCallRequest::default();
req.payload = Some(util::new_payload(size)).into();
let req = StreamingInputCallRequest {
payload: Some(util::new_payload(size)).into(),
..StreamingInputCallRequest::default()
};
sender.send((req, WriteFlags::default())).await?;
}
sender.close().await?;
Expand All @@ -76,11 +80,14 @@ impl Client {

pub async fn server_streaming(&self) -> grpcio::Result<()> {
print!("testing server streaming ... ");
let mut req = StreamingOutputCallRequest::default();
let sizes = vec![31415, 9, 2653, 58979];
for size in &sizes {
req.response_parameters.push(util::new_parameters(*size));
}
let req = StreamingOutputCallRequest {
response_parameters: sizes
.iter()
.map(|size| util::new_parameters(*size))
.collect(),
..StreamingOutputCallRequest::default()
};
let mut resp = self.client.streaming_output_call(&req)?;
let mut i = 0;
while let Some(r) = resp.try_next().await? {
Expand All @@ -100,10 +107,11 @@ impl Client {
let (mut sender, mut receiver) = self.client.full_duplex_call()?;
let cases = vec![(31415, 27182), (9, 8), (2653, 1828), (58979, 45904)];
for (resp_size, payload_size) in cases {
let mut req = StreamingOutputCallRequest::default();
req.response_parameters
.push(util::new_parameters(resp_size));
req.payload = Some(util::new_payload(payload_size)).into();
let req = StreamingOutputCallRequest {
response_parameters: vec![util::new_parameters(resp_size)].into(),
payload: Some(util::new_payload(payload_size)).into(),
..StreamingOutputCallRequest::default()
};
sender.send((req, WriteFlags::default())).await?;
let resp = receiver.try_next().await?.unwrap();
#[cfg(feature = "protobuf-codec")]
Expand All @@ -121,9 +129,11 @@ impl Client {
print!("testing custom metadata ... ");

// Step 1: test unary call
let mut req = SimpleRequest::default();
req.response_size = 314159;
req.payload = Some(util::new_payload(271828)).into();
let req = SimpleRequest {
response_size: 314159,
payload: Some(util::new_payload(271828)).into(),
..SimpleRequest::default()
};
let mut resp_call = self
.client
.unary_call_async_opt(&req, CallOption::default().headers(create_test_metadata()))?;
Expand All @@ -136,9 +146,11 @@ impl Client {
assert_eq!(v, b"test_initial_metadata_value");

// Step 2: test full duplex call
let mut req = StreamingOutputCallRequest::default();
req.response_parameters.push(util::new_parameters(314159));
req.payload = Some(util::new_payload(271828)).into();
let req = StreamingOutputCallRequest {
response_parameters: vec![util::new_parameters(314159)].into(),
payload: Some(util::new_payload(271828)).into(),
..StreamingOutputCallRequest::default()
};
let (mut tx, mut rx) = self
.client
.full_duplex_call_opt(CallOption::default().headers(create_test_metadata()))?;
Expand Down Expand Up @@ -184,9 +196,11 @@ impl Client {
pub async fn cancel_after_first_response(&self) -> grpcio::Result<()> {
print!("testing cancel_after_first_response ... ");
let (mut sender, mut receiver) = self.client.full_duplex_call()?;
let mut req = StreamingOutputCallRequest::default();
req.response_parameters.push(util::new_parameters(31415));
req.payload = Some(util::new_payload(27182)).into();
let req = StreamingOutputCallRequest {
response_parameters: vec![util::new_parameters(31415)].into(),
payload: Some(util::new_payload(27182)).into(),
..StreamingOutputCallRequest::default()
};
sender.send((req, WriteFlags::default())).await?;
let resp = receiver.try_next().await?.unwrap();

Expand All @@ -208,8 +222,10 @@ impl Client {
print!("testing timeout_of_sleeping_server ... ");
let opt = CallOption::default().timeout(Duration::from_millis(1));
let (mut sender, mut receiver) = self.client.full_duplex_call_opt(opt)?;
let mut req = StreamingOutputCallRequest::default();
req.payload = Some(util::new_payload(27182)).into();
let req = StreamingOutputCallRequest {
payload: Some(util::new_payload(27182)).into(),
..StreamingOutputCallRequest::default()
};
let _ = sender.send((req, WriteFlags::default())).await;
match receiver.try_next().await {
Err(grpc::Error::RpcFailure(s)) => {
Expand All @@ -225,20 +241,27 @@ impl Client {
pub async fn status_code_and_message(&self) -> grpcio::Result<()> {
print!("testing status_code_and_message ... ");
let error_msg = "test status message";
let mut status = EchoStatus::default();
status.code = 2;
status.message = error_msg.to_owned();
let mut req = SimpleRequest::default();
req.response_status = Some(status.clone()).into();
let status = EchoStatus {
code: 2,
message: error_msg.to_owned(),
..EchoStatus::default()
};

let req = SimpleRequest {
response_status: Some(status.clone()).into(),
..SimpleRequest::default()
};
match self.client.unary_call_async(&req)?.await.unwrap_err() {
grpc::Error::RpcFailure(s) => {
assert_eq!(s.code(), RpcStatusCode::UNKNOWN);
assert_eq!(s.message(), error_msg);
}
e => panic!("expected rpc failure: {:?}", e),
}
let mut req = StreamingOutputCallRequest::default();
req.response_status = Some(status).into();
let req = StreamingOutputCallRequest {
response_status: Some(status).into(),
..StreamingOutputCallRequest::default()
};
let (mut sender, mut receiver) = self.client.full_duplex_call()?;
let _ = sender.send((req, WriteFlags::default())).await;
match receiver.try_next().await {
Expand Down
18 changes: 12 additions & 6 deletions interop/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ impl TestService for InteropTestService {
return;
}
let resp_size = req.response_size;
let mut resp = SimpleResponse::default();
resp.payload = Some(util::new_payload(resp_size as usize)).into();
let resp = SimpleResponse {
payload: Some(util::new_payload(resp_size as usize)).into(),
..SimpleResponse::default()
};
let f = sink
.success(resp)
.map_err(|e| panic!("failed to send response: {:?}", e))
Expand All @@ -100,8 +102,10 @@ impl TestService for InteropTestService {
) {
let f = async move {
for param in req.response_parameters.into_iter() {
let mut resp = StreamingOutputCallResponse::default();
resp.payload = Some(util::new_payload(param.size as usize)).into();
let resp = StreamingOutputCallResponse {
payload: Some(util::new_payload(param.size as usize)).into(),
..StreamingOutputCallResponse::default()
};
sink.send((resp, WriteFlags::default())).await?;
}
sink.close().await?;
Expand All @@ -128,8 +132,10 @@ impl TestService for InteropTestService {
s += req.payload.body.len();
}

let mut resp = StreamingInputCallResponse::default();
resp.aggregated_payload_size = s as i32;
let resp = StreamingInputCallResponse {
aggregated_payload_size: s as i32,
..StreamingInputCallResponse::default()
};
sink.success(resp).await
}
.map_err(|e| match e {
Expand Down
2 changes: 1 addition & 1 deletion proto/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use crate::proto::protobuf::google::rpc::Status;
use crate::google::rpc::Status;
use grpcio::{
ChannelCredentials, ChannelCredentialsBuilder, ServerCredentials, ServerCredentialsBuilder,
};
Expand Down
6 changes: 3 additions & 3 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ fn normalize_key(key: &str, binary: bool) -> Result<Cow<'_, str>> {
let mut is_upper_case = false;
for b in key.as_bytes() {
let b = *b;
if (b'A'..=b'Z').contains(&b) {
if b.is_ascii_uppercase() {
is_upper_case = true;
continue;
} else if (b'a'..=b'z').contains(&b)
|| (b'0'..=b'9').contains(&b)
} else if b.is_ascii_lowercase()
|| b.is_ascii_digit()
|| b == b'_'
|| b == b'-'
|| b == b'.'
Expand Down

0 comments on commit c0eb577

Please sign in to comment.