Skip to content

Commit c8f89a8

Browse files
committed
RUST-679 Remove inner Arc from Error
fix sync test
1 parent e16b038 commit c8f89a8

File tree

29 files changed

+99
-108
lines changed

29 files changed

+99
-108
lines changed

src/client/executor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl Client {
108108

109109
// Retryable writes are only supported by storage engines with document-level
110110
// locking, so users need to disable retryable writes if using mmapv1.
111-
if let ErrorKind::CommandError(ref err) = err.kind.as_ref() {
111+
if let ErrorKind::CommandError(ref err) = err.kind {
112112
if err.code == 20 && err.message.starts_with("Transaction numbers") {
113113
let mut err = err.clone();
114114
err.message = "This MongoDB deployment does not support retryable writes. \

src/client/options/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ async fn run_test(test_file: TestFile) {
317317
match ClientOptions::parse(&test_case.uri)
318318
.await
319319
.as_ref()
320-
.map_err(|e| e.as_ref())
320+
.map_err(|e| &e.kind)
321321
{
322322
Ok(_) => panic!("expected {}", expected_type),
323323
Err(ErrorKind::ArgumentError { .. }) => {}
@@ -342,7 +342,7 @@ async fn run_connection_string_spec_tests() {
342342
async fn parse_uri(option: &str, suggestion: Option<&str>) {
343343
match ClientOptionsParser::parse(&format!("mongodb://host:27017/?{}=test", option))
344344
.as_ref()
345-
.map_err(|e| e.as_ref())
345+
.map_err(|e| &e.kind)
346346
{
347347
Ok(_) => panic!("expected error for option {}", option),
348348
Err(ErrorKind::ArgumentError { message, .. }) => {

src/cmap/mod.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,11 @@ impl ConnectionPool {
156156
});
157157
}
158158
Err(ref e) => {
159-
let failure_reason =
160-
if let ErrorKind::WaitQueueTimeoutError { .. } = e.kind.as_ref() {
161-
ConnectionCheckoutFailedReason::Timeout
162-
} else {
163-
ConnectionCheckoutFailedReason::ConnectionError
164-
};
159+
let failure_reason = if let ErrorKind::WaitQueueTimeoutError { .. } = e.kind {
160+
ConnectionCheckoutFailedReason::Timeout
161+
} else {
162+
ConnectionCheckoutFailedReason::ConnectionError
163+
};
165164

166165
self.emit_event(|handler| {
167166
handler.handle_connection_checkout_failed_event(ConnectionCheckoutFailedEvent {

src/cmap/test/file.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub struct Error {
9797

9898
impl Error {
9999
pub fn assert_matches(&self, error: &crate::error::Error, description: &str) {
100-
match error.kind.as_ref() {
100+
match error.kind {
101101
ErrorKind::WaitQueueTimeoutError { .. } => {
102102
assert_eq!(self.type_, "WaitQueueTimeoutError", "{}", description);
103103
}

src/coll/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ where
437437
}
438438
}
439439
}
440-
Err(e) => match e.kind.as_ref() {
440+
Err(e) => match e.kind {
441441
ErrorKind::BulkWriteError(failure) => {
442442
let failure_ref =
443443
cumulative_failure.get_or_insert_with(BulkWriteFailure::new);

src/concern/test.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ async fn inconsistent_write_concern_rejected() {
107107
)
108108
.await
109109
.expect_err("insert should fail");
110-
assert!(matches!(
111-
error.kind.as_ref(),
112-
ErrorKind::ArgumentError { .. }
113-
));
110+
assert!(matches!(error.kind, ErrorKind::ArgumentError { .. }));
114111

115112
let coll = db.collection(function_name!());
116113
let wc = WriteConcern {
@@ -123,10 +120,7 @@ async fn inconsistent_write_concern_rejected() {
123120
.insert_one(doc! {}, options)
124121
.await
125122
.expect_err("insert should fail");
126-
assert!(matches!(
127-
error.kind.as_ref(),
128-
ErrorKind::ArgumentError { .. }
129-
));
123+
assert!(matches!(error.kind, ErrorKind::ArgumentError { .. }));
130124
}
131125

132126
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
@@ -148,8 +142,5 @@ async fn unacknowledged_write_concern_rejected() {
148142
)
149143
.await
150144
.expect_err("insert should fail");
151-
assert!(matches!(
152-
error.kind.as_ref(),
153-
ErrorKind::ArgumentError { .. }
154-
));
145+
assert!(matches!(error.kind, ErrorKind::ArgumentError { .. }));
155146
}

src/cursor/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub(super) trait GetMoreProviderResult {
137137
match self.as_ref() {
138138
Ok(res) => res.exhausted,
139139
Err(e) => {
140-
matches!(e.kind.as_ref(), ErrorKind::CommandError(e) if e.code == 43 || e.code == 237)
140+
matches!(e.kind, ErrorKind::CommandError(ref e) if e.code == 43 || e.code == 237)
141141
}
142142
}
143143
}

src/error.rs

+44-29
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub type Result<T> = std::result::Result<T, Error>;
2828
#[non_exhaustive]
2929
pub struct Error {
3030
/// The type of error that occurred.
31-
pub kind: Arc<ErrorKind>,
31+
pub kind: ErrorKind,
3232
labels: Vec<String>,
3333
}
3434

@@ -64,7 +64,7 @@ impl Error {
6464

6565
/// Whether this error is an "ns not found" error or not.
6666
pub(crate) fn is_ns_not_found(&self) -> bool {
67-
matches!(self.kind.as_ref(), ErrorKind::CommandError(err) if err.code == 26)
67+
matches!(self.kind, ErrorKind::CommandError(ref err) if err.code == 26)
6868
}
6969

7070
/// Whether a read operation should be retried if this error occurs.
@@ -110,7 +110,7 @@ impl Error {
110110
/// Whether an error originated from the server.
111111
pub(crate) fn is_server_error(&self) -> bool {
112112
matches!(
113-
self.kind.as_ref(),
113+
self.kind,
114114
ErrorKind::AuthenticationError { .. }
115115
| ErrorKind::BulkWriteError(_)
116116
| ErrorKind::CommandError(_)
@@ -120,13 +120,13 @@ impl Error {
120120

121121
/// Returns the labels for this error.
122122
pub fn labels(&self) -> &[String] {
123-
match self.kind.as_ref() {
124-
ErrorKind::CommandError(err) => &err.labels,
125-
ErrorKind::WriteError(err) => match err {
123+
match self.kind {
124+
ErrorKind::CommandError(ref err) => &err.labels,
125+
ErrorKind::WriteError(ref err) => match err {
126126
WriteFailure::WriteError(_) => &self.labels,
127-
WriteFailure::WriteConcernError(err) => &err.labels,
127+
WriteFailure::WriteConcernError(ref err) => &err.labels,
128128
},
129-
ErrorKind::BulkWriteError(err) => match err.write_concern_error {
129+
ErrorKind::BulkWriteError(ref err) => match err.write_concern_error {
130130
Some(ref err) => &err.labels,
131131
None => &self.labels,
132132
},
@@ -144,24 +144,24 @@ impl Error {
144144
/// Returns a copy of this Error with the specified label added.
145145
pub(crate) fn with_label<T: AsRef<str>>(mut self, label: T) -> Self {
146146
let label = label.as_ref().to_string();
147-
match self.kind.as_ref() {
148-
ErrorKind::CommandError(err) => {
147+
match self.kind {
148+
ErrorKind::CommandError(ref err) => {
149149
let mut err = err.clone();
150150
err.labels.push(label);
151151
ErrorKind::CommandError(err).into()
152152
}
153-
ErrorKind::WriteError(err) => match err {
153+
ErrorKind::WriteError(ref err) => match err {
154154
WriteFailure::WriteError(_) => {
155155
self.labels.push(label);
156156
self
157157
}
158-
WriteFailure::WriteConcernError(err) => {
158+
WriteFailure::WriteConcernError(ref err) => {
159159
let mut err = err.clone();
160160
err.labels.push(label);
161161
ErrorKind::WriteError(WriteFailure::WriteConcernError(err)).into()
162162
}
163163
},
164-
ErrorKind::BulkWriteError(err) => match err.write_concern_error {
164+
ErrorKind::BulkWriteError(ref err) => match err.write_concern_error {
165165
Some(ref write_concern_error) => {
166166
let mut err = err.clone();
167167
let mut write_concern_error = write_concern_error.clone();
@@ -188,14 +188,38 @@ where
188188
{
189189
fn from(err: E) -> Self {
190190
Self {
191-
kind: Arc::new(err.into()),
191+
kind: err.into(),
192192
labels: Vec::new(),
193193
}
194194
}
195195
}
196196

197+
impl From<bson::de::Error> for ErrorKind {
198+
fn from(err: bson::de::Error) -> Self {
199+
Self::BsonDecode(Arc::new(err))
200+
}
201+
}
202+
203+
impl From<bson::ser::Error> for ErrorKind {
204+
fn from(err: bson::ser::Error) -> Self {
205+
Self::BsonEncode(Arc::new(err))
206+
}
207+
}
208+
209+
impl From<std::io::Error> for ErrorKind {
210+
fn from(err: std::io::Error) -> Self {
211+
Self::Io(Arc::new(err))
212+
}
213+
}
214+
215+
impl From<std::io::ErrorKind> for ErrorKind {
216+
fn from(err: std::io::ErrorKind) -> Self {
217+
Self::Io(Arc::new(err.into()))
218+
}
219+
}
220+
197221
impl std::ops::Deref for Error {
198-
type Target = Arc<ErrorKind>;
222+
type Target = ErrorKind;
199223

200224
fn deref(&self) -> &Self::Target {
201225
&self.kind
@@ -204,7 +228,7 @@ impl std::ops::Deref for Error {
204228

205229
/// The types of errors that can occur.
206230
#[allow(missing_docs)]
207-
#[derive(Debug, Error)]
231+
#[derive(Clone, Debug, Error)]
208232
#[non_exhaustive]
209233
pub enum ErrorKind {
210234
/// Wrapper around [`std::net::AddrParseError`](https://doc.rust-lang.org/std/net/struct.AddrParseError.html).
@@ -216,10 +240,6 @@ pub enum ErrorKind {
216240
#[non_exhaustive]
217241
ArgumentError { message: String },
218242

219-
#[cfg(feature = "async-std-runtime")]
220-
#[error("{0}")]
221-
AsyncStdTimeout(#[from] async_std::future::TimeoutError),
222-
223243
/// An error occurred while the [`Client`](../struct.Client.html) attempted to authenticate a
224244
/// connection.
225245
#[error("{message}")]
@@ -228,11 +248,11 @@ pub enum ErrorKind {
228248

229249
/// Wrapper around `bson::de::Error`.
230250
#[error("{0}")]
231-
BsonDecode(#[from] crate::bson::de::Error),
251+
BsonDecode(Arc<crate::bson::de::Error>),
232252

233253
/// Wrapper around `bson::ser::Error`.
234254
#[error("{0}")]
235-
BsonEncode(#[from] crate::bson::ser::Error),
255+
BsonEncode(Arc<crate::bson::ser::Error>),
236256

237257
/// An error occurred when trying to execute a write operation consisting of multiple writes.
238258
#[error("An error occurred when trying to execute a write operation: {0:?}")]
@@ -263,7 +283,7 @@ pub enum ErrorKind {
263283

264284
/// Wrapper around [`std::io::Error`](https://doc.rust-lang.org/std/io/struct.Error.html).
265285
#[error("{0}")]
266-
Io(#[from] std::io::Error),
286+
Io(Arc<std::io::Error>),
267287

268288
#[error("No DNS results for domain {0}")]
269289
NoDnsResults(StreamAddress),
@@ -305,11 +325,6 @@ pub enum ErrorKind {
305325
#[non_exhaustive]
306326
SrvLookupError { message: String },
307327

308-
/// A timeout occurred before a Tokio task could be completed.
309-
#[cfg(feature = "tokio-runtime")]
310-
#[error("{0}")]
311-
TokioTimeoutElapsed(#[from] tokio::time::error::Elapsed),
312-
313328
#[error("{0}")]
314329
RustlsConfig(#[from] rustls::TLSError),
315330

@@ -565,7 +580,7 @@ impl WriteFailure {
565580
/// Translates ErrorKind::BulkWriteError cases to ErrorKind::WriteErrors, leaving all other errors
566581
/// untouched.
567582
pub(crate) fn convert_bulk_errors(error: Error) -> Error {
568-
match *error.kind {
583+
match error.kind {
569584
ErrorKind::BulkWriteError(ref bulk_failure) => {
570585
match WriteFailure::from_bulk_failure(bulk_failure.clone()) {
571586
Ok(failure) => ErrorKind::WriteError(failure).into(),

src/operation/aggregate/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ async fn handle_write_concern_error() {
310310
let error = aggregate
311311
.handle_response(response)
312312
.expect_err("should get wc error");
313-
match *error.kind {
313+
match error.kind {
314314
ErrorKind::WriteError(WriteFailure::WriteConcernError(_)) => {}
315315
ref e => panic!("should have gotten WriteConcernError, got {:?} instead", e),
316316
}

src/operation/count/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async fn handle_response_no_n() {
9696
let response = CommandResponse::with_document(doc! { "ok" : 1 });
9797

9898
let result = count_op.handle_response(response);
99-
match result.as_ref().map_err(|e| e.as_ref()) {
99+
match result.as_ref().map_err(|e| &e.kind) {
100100
Err(ErrorKind::ResponseError { .. }) => {}
101101
other => panic!("expected response error, but got {:?}", other),
102102
}

src/operation/create/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ async fn handle_write_concern_error() {
102102
let result = op.handle_response(response);
103103
assert!(result.is_err());
104104

105-
match *result.unwrap_err().kind {
105+
match result.unwrap_err().kind {
106106
ErrorKind::WriteError(WriteFailure::WriteConcernError(ref wc_err)) => {
107107
assert_eq!(wc_err.code, 100);
108108
assert_eq!(wc_err.code_name, "hello world");

src/operation/delete/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async fn handle_write_failure() {
143143
});
144144
let write_error_result = op.handle_response(write_error_response);
145145
assert!(write_error_result.is_err());
146-
match *write_error_result.unwrap_err().kind {
146+
match write_error_result.unwrap_err().kind {
147147
ErrorKind::WriteError(WriteFailure::WriteError(ref error)) => {
148148
let expected_err = WriteError {
149149
code: 1234,
@@ -181,7 +181,7 @@ async fn handle_write_concern_failure() {
181181
let wc_error_result = op.handle_response(wc_error_response);
182182
assert!(wc_error_result.is_err());
183183

184-
match *wc_error_result.unwrap_err().kind {
184+
match wc_error_result.unwrap_err().kind {
185185
ErrorKind::WriteError(WriteFailure::WriteConcernError(ref wc_error)) => {
186186
let expected_wc_err = WriteConcernError {
187187
code: 456,

src/operation/distinct/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ async fn handle_response_no_values() {
144144
});
145145

146146
let result = distinct_op.handle_response(response);
147-
match result.as_ref().map_err(|e| e.as_ref()) {
147+
match result.as_ref().map_err(|e| &e.kind) {
148148
Err(ErrorKind::ResponseError { .. }) => {}
149149
other => panic!("expected response error, but got {:?}", other),
150150
}

src/operation/drop_collection/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ async fn handle_write_concern_error() {
8080
let result = op.handle_response(response);
8181
assert!(result.is_err());
8282

83-
match *result.unwrap_err().kind {
83+
match result.unwrap_err().kind {
8484
ErrorKind::WriteError(WriteFailure::WriteConcernError(ref wc_err)) => {
8585
assert_eq!(wc_err.code, 100);
8686
assert_eq!(wc_err.code_name, "hello world");

src/operation/drop_database/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async fn handle_write_concern_error() {
7878
let result = op.handle_response(response);
7979
assert!(result.is_err());
8080

81-
match *result.unwrap_err().kind {
81+
match result.unwrap_err().kind {
8282
ErrorKind::WriteError(WriteFailure::WriteConcernError(ref wc_err)) => {
8383
assert_eq!(wc_err.code, 100);
8484
assert_eq!(wc_err.code_name, "hello world");

src/operation/insert/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ async fn handle_write_failure() {
193193
let write_error_result = fixtures.op.handle_response(write_error_response);
194194
assert!(write_error_result.is_err());
195195

196-
match *write_error_result.unwrap_err().kind {
196+
match write_error_result.unwrap_err().kind {
197197
ErrorKind::BulkWriteError(ref error) => {
198198
let write_errors = error.write_errors.clone().unwrap();
199199
assert_eq!(write_errors.len(), 1);

src/operation/list_databases/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async fn handle_response_no_databases() {
140140
});
141141

142142
let result = list_databases_op.handle_response(response);
143-
match result.as_ref().map_err(|e| e.as_ref()) {
143+
match result.as_ref().map_err(|e| &e.kind) {
144144
Err(ErrorKind::ResponseError { .. }) => {}
145145
other => panic!("expected response error, but got {:?}", other),
146146
}

src/operation/update/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ async fn handle_write_failure() {
227227
});
228228
let write_error_result = op.handle_response(write_error_response);
229229
assert!(write_error_result.is_err());
230-
match *write_error_result.unwrap_err().kind {
230+
match write_error_result.unwrap_err().kind {
231231
ErrorKind::WriteError(WriteFailure::WriteError(ref error)) => {
232232
let expected_err = WriteError {
233233
code: 1234,
@@ -266,7 +266,7 @@ async fn handle_write_concern_failure() {
266266
let wc_error_result = op.handle_response(wc_error_response);
267267
assert!(wc_error_result.is_err());
268268

269-
match *wc_error_result.unwrap_err().kind {
269+
match wc_error_result.unwrap_err().kind {
270270
ErrorKind::WriteError(WriteFailure::WriteConcernError(ref wc_error)) => {
271271
let expected_wc_err = WriteConcernError {
272272
code: 456,

0 commit comments

Comments
 (0)