Skip to content

Commit affc0d5

Browse files
committed
errors: restore GrpcAPI payload compatibility without cfg(clippy) split
Signed-off-by: Michael Ingley <46031485+mingley@users.noreply.github.com>
1 parent 9d9b680 commit affc0d5

File tree

11 files changed

+82
-16
lines changed

11 files changed

+82
-16
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ authors = ["The TiKV Project Authors"]
77
repository = "https://github.com/tikv/client-rust"
88
description = "The Rust language implementation of TiKV client."
99
edition = "2021"
10+
rust-version = "1.93.0"
1011

1112
[features]
1213
default = ["prometheus"]

examples/raw.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.
22

33
#![type_length_limit = "8165158"]
4+
#![allow(clippy::result_large_err)]
45

56
mod common;
67

rust-toolchain.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "1.84.1"
2+
channel = "1.93.0"
33
components = ["rustfmt", "clippy"]

src/common/errors.rs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub enum Error {
6868
/// Wraps a `reqwest::Error`.
6969
/// Wraps a `grpcio::Error`.
7070
#[error("gRPC api error: {0}")]
71-
GrpcAPI(#[from] tonic::Status),
71+
GrpcAPI(tonic::Status),
7272
/// Wraps a `grpcio::Error`.
7373
#[error("url error: {0}")]
7474
Url(#[from] tonic::codegen::http::uri::InvalidUri),
@@ -142,6 +142,12 @@ impl From<ProtoKeyError> for Error {
142142
}
143143
}
144144

145+
impl From<tonic::Status> for Error {
146+
fn from(status: tonic::Status) -> Error {
147+
Error::GrpcAPI(status)
148+
}
149+
}
150+
145151
/// A result holding an [`Error`](enum@Error).
146152
pub type Result<T> = result::Result<T, Error>;
147153

@@ -157,3 +163,62 @@ macro_rules! internal_err {
157163
internal_err!(format!($f, $($arg),+))
158164
});
159165
}
166+
167+
#[cfg(test)]
168+
mod test {
169+
use tonic::Code;
170+
171+
use super::Error;
172+
use super::ProtoKeyError;
173+
use super::ProtoRegionError;
174+
175+
#[test]
176+
fn from_tonic_status_produces_grpc_api_error() {
177+
let status = tonic::Status::new(Code::Unavailable, "network unavailable");
178+
let error: Error = status.clone().into();
179+
180+
let Error::GrpcAPI(actual_status) = error else {
181+
panic!("expected Error::GrpcAPI");
182+
};
183+
assert_eq!(actual_status.code(), status.code());
184+
assert_eq!(actual_status.message(), status.message());
185+
}
186+
187+
#[test]
188+
fn grpc_api_variant_accepts_tonic_status_payload() {
189+
let error = Error::GrpcAPI(tonic::Status::new(Code::DeadlineExceeded, "timeout"));
190+
let Error::GrpcAPI(status) = error else {
191+
panic!("expected Error::GrpcAPI");
192+
};
193+
assert_eq!(status.code(), Code::DeadlineExceeded);
194+
assert_eq!(status.message(), "timeout");
195+
}
196+
197+
#[test]
198+
fn from_proto_region_error_wraps_region_error() {
199+
let region_error = ProtoRegionError {
200+
message: "region stale".to_owned(),
201+
..Default::default()
202+
};
203+
let error: Error = region_error.clone().into();
204+
205+
let Error::RegionError(actual_region_error) = error else {
206+
panic!("expected Error::RegionError");
207+
};
208+
assert_eq!(actual_region_error.message, region_error.message);
209+
}
210+
211+
#[test]
212+
fn from_proto_key_error_wraps_key_error() {
213+
let key_error = ProtoKeyError {
214+
retryable: "retry later".to_owned(),
215+
..Default::default()
216+
};
217+
let error: Error = key_error.clone().into();
218+
219+
let Error::KeyError(actual_key_error) = error else {
220+
panic!("expected Error::KeyError");
221+
};
222+
assert_eq!(actual_key_error.retryable, key_error.retryable);
223+
}
224+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
//! ```
9292
9393
#![allow(clippy::field_reassign_with_default)]
94+
#![allow(clippy::result_large_err)]
9495

9596
pub mod backoff;
9697
#[doc(hidden)]

src/pd/retry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl RetryClientTrait for RetryClient<Cluster> {
184184
cluster
185185
.get_all_stores(self.timeout)
186186
.await
187-
.map(|resp| resp.stores.into_iter().map(Into::into).collect())
187+
.map(|resp| resp.stores)
188188
})
189189
}
190190

src/proto.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
pub use protos::*;
77

8+
// Rust 1.93's clippy::all flags many protobuf-generated wire types as dead code.
9+
// Prior toolchain (1.84.1) did not fail on these generated definitions.
810
#[allow(clippy::doc_lazy_continuation)]
11+
#[allow(dead_code)]
912
mod protos {
1013
include!("generated/mod.rs");
1114
}

src/store/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ macro_rules! impl_request {
4242
.$fun(req)
4343
.await
4444
.map(|r| Box::new(r.into_inner()) as Box<dyn Any>)
45-
.map_err(Error::GrpcAPI)
45+
.map_err(Error::from)
4646
}
4747

4848
fn label(&self) -> &'static str {

src/transaction/requests.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,8 @@ pub fn new_pessimistic_prewrite_request(
241241
let len = mutations.len();
242242
let mut req = new_prewrite_request(mutations, primary_lock, start_version, lock_ttl);
243243
req.for_update_ts = for_update_ts;
244-
req.pessimistic_actions = iter::repeat(PessimisticAction::DoPessimisticCheck.into())
245-
.take(len)
246-
.collect();
244+
req.pessimistic_actions =
245+
iter::repeat_n(PessimisticAction::DoPessimisticCheck.into(), len).collect();
247246
req
248247
}
249248

@@ -343,7 +342,7 @@ impl Shardable for kvrpcpb::CommitRequest {
343342
}
344343

345344
fn apply_shard(&mut self, shard: Self::Shard) {
346-
self.keys = shard.into_iter().map(Into::into).collect();
345+
self.keys = shard;
347346
}
348347

349348
fn apply_store(&mut self, store: &RegionStore) -> Result<()> {
@@ -570,7 +569,7 @@ impl Merge<kvrpcpb::ScanLockResponse> for Collect {
570569
fn merge(&self, input: Vec<Result<kvrpcpb::ScanLockResponse>>) -> Result<Self::Out> {
571570
input
572571
.into_iter()
573-
.flat_map_ok(|mut resp| resp.take_locks().into_iter().map(Into::into))
572+
.flat_map_ok(|mut resp| resp.take_locks().into_iter())
574573
.collect()
575574
}
576575
}

src/transaction/transaction.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,7 @@ impl<PdC: PdClient> Transaction<PdC> {
283283
.retry_multi_region(retry_options.region_backoff)
284284
.merge(Collect)
285285
.plan();
286-
plan.execute()
287-
.await
288-
.map(|r| r.into_iter().map(Into::into).collect())
286+
plan.execute().await.map(|r| r.into_iter().collect())
289287
})
290288
.await
291289
.map(move |pairs| pairs.map(move |pair| pair.truncate_keyspace(keyspace)))
@@ -806,9 +804,7 @@ impl<PdC: PdClient> Transaction<PdC> {
806804
.retry_multi_region(retry_options.region_backoff)
807805
.merge(Collect)
808806
.plan();
809-
plan.execute()
810-
.await
811-
.map(|r| r.into_iter().map(Into::into).collect())
807+
plan.execute().await.map(|r| r.into_iter().collect())
812808
},
813809
)
814810
.await

0 commit comments

Comments
 (0)