@@ -27,7 +27,7 @@ pub type Result<T> = std::result::Result<T, Error>;
2727#[ non_exhaustive]
2828pub struct Error {
2929 /// The type of error that occurred.
30- pub kind : Arc < ErrorKind > ,
30+ pub kind : ErrorKind ,
3131 labels : Vec < String > ,
3232}
3333
@@ -63,7 +63,7 @@ impl Error {
6363
6464 /// Whether this error is an "ns not found" error or not.
6565 pub ( crate ) fn is_ns_not_found ( & self ) -> bool {
66- matches ! ( self . kind. as_ref ( ) , ErrorKind :: CommandError ( err) if err. code == 26 )
66+ matches ! ( self . kind, ErrorKind :: CommandError ( ref err) if err. code == 26 )
6767 }
6868
6969 /// Whether a read operation should be retried if this error occurs.
@@ -109,7 +109,7 @@ impl Error {
109109 /// Whether an error originated from the server.
110110 pub ( crate ) fn is_server_error ( & self ) -> bool {
111111 matches ! (
112- self . kind. as_ref ( ) ,
112+ self . kind,
113113 ErrorKind :: AuthenticationError { .. }
114114 | ErrorKind :: BulkWriteError ( _)
115115 | ErrorKind :: CommandError ( _)
@@ -119,13 +119,13 @@ impl Error {
119119
120120 /// Returns the labels for this error.
121121 pub fn labels ( & self ) -> & [ String ] {
122- match self . kind . as_ref ( ) {
123- ErrorKind :: CommandError ( err) => & err. labels ,
124- ErrorKind :: WriteError ( err) => match err {
122+ match self . kind {
123+ ErrorKind :: CommandError ( ref err) => & err. labels ,
124+ ErrorKind :: WriteError ( ref err) => match err {
125125 WriteFailure :: WriteError ( _) => & self . labels ,
126- WriteFailure :: WriteConcernError ( err) => & err. labels ,
126+ WriteFailure :: WriteConcernError ( ref err) => & err. labels ,
127127 } ,
128- ErrorKind :: BulkWriteError ( err) => match err. write_concern_error {
128+ ErrorKind :: BulkWriteError ( ref err) => match err. write_concern_error {
129129 Some ( ref err) => & err. labels ,
130130 None => & self . labels ,
131131 } ,
@@ -143,24 +143,24 @@ impl Error {
143143 /// Returns a copy of this Error with the specified label added.
144144 pub ( crate ) fn with_label < T : AsRef < str > > ( mut self , label : T ) -> Self {
145145 let label = label. as_ref ( ) . to_string ( ) ;
146- match self . kind . as_ref ( ) {
147- ErrorKind :: CommandError ( err) => {
146+ match self . kind {
147+ ErrorKind :: CommandError ( ref err) => {
148148 let mut err = err. clone ( ) ;
149149 err. labels . push ( label) ;
150150 ErrorKind :: CommandError ( err) . into ( )
151151 }
152- ErrorKind :: WriteError ( err) => match err {
152+ ErrorKind :: WriteError ( ref err) => match err {
153153 WriteFailure :: WriteError ( _) => {
154154 self . labels . push ( label) ;
155155 self
156156 }
157- WriteFailure :: WriteConcernError ( err) => {
157+ WriteFailure :: WriteConcernError ( ref err) => {
158158 let mut err = err. clone ( ) ;
159159 err. labels . push ( label) ;
160160 ErrorKind :: WriteError ( WriteFailure :: WriteConcernError ( err) ) . into ( )
161161 }
162162 } ,
163- ErrorKind :: BulkWriteError ( err) => match err. write_concern_error {
163+ ErrorKind :: BulkWriteError ( ref err) => match err. write_concern_error {
164164 Some ( ref write_concern_error) => {
165165 let mut err = err. clone ( ) ;
166166 let mut write_concern_error = write_concern_error. clone ( ) ;
@@ -187,14 +187,38 @@ where
187187{
188188 fn from ( err : E ) -> Self {
189189 Self {
190- kind : Arc :: new ( err. into ( ) ) ,
190+ kind : err. into ( ) ,
191191 labels : Vec :: new ( ) ,
192192 }
193193 }
194194}
195195
196+ impl From < bson:: de:: Error > for ErrorKind {
197+ fn from ( err : bson:: de:: Error ) -> Self {
198+ Self :: BsonDecode ( Arc :: new ( err) )
199+ }
200+ }
201+
202+ impl From < bson:: ser:: Error > for ErrorKind {
203+ fn from ( err : bson:: ser:: Error ) -> Self {
204+ Self :: BsonEncode ( Arc :: new ( err) )
205+ }
206+ }
207+
208+ impl From < std:: io:: Error > for ErrorKind {
209+ fn from ( err : std:: io:: Error ) -> Self {
210+ Self :: Io ( Arc :: new ( err) )
211+ }
212+ }
213+
214+ impl From < std:: io:: ErrorKind > for ErrorKind {
215+ fn from ( err : std:: io:: ErrorKind ) -> Self {
216+ Self :: Io ( Arc :: new ( err. into ( ) ) )
217+ }
218+ }
219+
196220impl std:: ops:: Deref for Error {
197- type Target = Arc < ErrorKind > ;
221+ type Target = ErrorKind ;
198222
199223 fn deref ( & self ) -> & Self :: Target {
200224 & self . kind
@@ -203,7 +227,7 @@ impl std::ops::Deref for Error {
203227
204228/// The types of errors that can occur.
205229#[ allow( missing_docs) ]
206- #[ derive( Debug , Error ) ]
230+ #[ derive( Clone , Debug , Error ) ]
207231#[ non_exhaustive]
208232pub enum ErrorKind {
209233 /// Wrapper around [`std::net::AddrParseError`](https://doc.rust-lang.org/std/net/struct.AddrParseError.html).
@@ -215,10 +239,6 @@ pub enum ErrorKind {
215239 #[ non_exhaustive]
216240 ArgumentError { message : String } ,
217241
218- #[ cfg( feature = "async-std-runtime" ) ]
219- #[ error( "{0}" ) ]
220- AsyncStdTimeout ( #[ from] async_std:: future:: TimeoutError ) ,
221-
222242 /// An error occurred while the [`Client`](../struct.Client.html) attempted to authenticate a
223243 /// connection.
224244 #[ error( "{message}" ) ]
@@ -227,11 +247,11 @@ pub enum ErrorKind {
227247
228248 /// Wrapper around `bson::de::Error`.
229249 #[ error( "{0}" ) ]
230- BsonDecode ( # [ from ] crate :: bson:: de:: Error ) ,
250+ BsonDecode ( Arc < crate :: bson:: de:: Error > ) ,
231251
232252 /// Wrapper around `bson::ser::Error`.
233253 #[ error( "{0}" ) ]
234- BsonEncode ( # [ from ] crate :: bson:: ser:: Error ) ,
254+ BsonEncode ( Arc < crate :: bson:: ser:: Error > ) ,
235255
236256 /// An error occurred when trying to execute a write operation consisting of multiple writes.
237257 #[ error( "An error occurred when trying to execute a write operation: {0:?}" ) ]
@@ -262,7 +282,7 @@ pub enum ErrorKind {
262282
263283 /// Wrapper around [`std::io::Error`](https://doc.rust-lang.org/std/io/struct.Error.html).
264284 #[ error( "{0}" ) ]
265- Io ( # [ from ] std:: io:: Error ) ,
285+ Io ( Arc < std:: io:: Error > ) ,
266286
267287 #[ error( "No DNS results for domain {0}" ) ]
268288 NoDnsResults ( StreamAddress ) ,
@@ -301,11 +321,6 @@ pub enum ErrorKind {
301321 #[ non_exhaustive]
302322 SrvLookupError { message : String } ,
303323
304- /// A timeout occurred before a Tokio task could be completed.
305- #[ cfg( feature = "tokio-runtime" ) ]
306- #[ error( "{0}" ) ]
307- TokioTimeoutElapsed ( #[ from] tokio:: time:: error:: Elapsed ) ,
308-
309324 #[ error( "{0}" ) ]
310325 RustlsConfig ( #[ from] rustls:: TLSError ) ,
311326
@@ -561,7 +576,7 @@ impl WriteFailure {
561576/// Translates ErrorKind::BulkWriteError cases to ErrorKind::WriteErrors, leaving all other errors
562577/// untouched.
563578pub ( crate ) fn convert_bulk_errors ( error : Error ) -> Error {
564- match * error. kind {
579+ match error. kind {
565580 ErrorKind :: BulkWriteError ( ref bulk_failure) => {
566581 match WriteFailure :: from_bulk_failure ( bulk_failure. clone ( ) ) {
567582 Ok ( failure) => ErrorKind :: WriteError ( failure) . into ( ) ,
0 commit comments