chainerror/booksrc/tutorial6.md

19 lines
487 B
Markdown
Raw Normal View History

2018-12-20 16:37:08 +01:00
# Downcast the Errors
2018-12-20 15:14:21 +01:00
2018-12-21 13:50:08 +01:00
`std::error::Error` comes with some helper methods to get to the original object of the
`&(dyn Error + 'static)` returned by `.source()`.
~~~rust,ignore
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>
~~~
This is how it looks like, when using those:
2018-12-20 15:14:21 +01:00
2018-12-20 14:52:06 +01:00
~~~rust
2019-03-04 11:38:35 +01:00
{{#include ../examples/tutorial6.rs}}
2018-12-20 14:52:06 +01:00
# #[allow(dead_code)]
# mod chainerror {
{{#includecomment ../src/lib.rs}}
# }
~~~