feat(error): add Error::cause2 and Error::into_cause
				
					
				
			- The `cause2` method adds a `'static` bound, allowing to downcast the error type. - The `into_cause` method converts an `Error` into its optional cause. Closes #1542
This commit is contained in:
		
				
					committed by
					
						 Sean McArthur
						Sean McArthur
					
				
			
			
				
	
			
			
			
						parent
						
							b7a0c2d596
						
					
				
				
					commit
					bc5e22f580
				
			
							
								
								
									
										13
									
								
								src/error.rs
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								src/error.rs
									
									
									
									
									
								
							| @@ -125,6 +125,19 @@ impl Error { | |||||||
|         self.inner.kind == Kind::Closed |         self.inner.kind == Kind::Closed | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /// Returns the error's cause. | ||||||
|  |     ///  | ||||||
|  |     /// This is identical to `Error::cause` except that it provides extra | ||||||
|  |     /// bounds required to be able to downcast the error. | ||||||
|  |     pub fn cause2(&self) -> Option<&(StdError + 'static + Sync + Send)> { | ||||||
|  |         self.inner.cause.as_ref().map(|e| &**e) | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     /// Consumes the error, returning its cause. | ||||||
|  |     pub fn into_cause(self) -> Option<Box<StdError + Sync + Send>> { | ||||||
|  |         self.inner.cause | ||||||
|  |     } | ||||||
|  |  | ||||||
|     pub(crate) fn new(kind: Kind, cause: Option<Cause>) -> Error { |     pub(crate) fn new(kind: Kind, cause: Option<Cause>) -> Error { | ||||||
|         Error { |         Error { | ||||||
|             inner: Box::new(ErrorImpl { |             inner: Box::new(ErrorImpl { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user