Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. use std::{io, rc::Rc};
  2.  
  3. type RawErr = Rc<Box<dyn std::error::Error>>;
  4.  
  5. fn io_interrupted() -> RawErr {
  6. Rc::new(Box::new(io::Error::from(io::ErrorKind::Interrupted)))
  7. }
  8. fn io_other() -> RawErr {
  9. Rc::new(Box::new(io::Error::from(io::ErrorKind::UnexpectedEof)))
  10. }
  11. fn fmt() -> RawErr {
  12. Rc::new(Box::new(std::fmt::Error))
  13. }
  14.  
  15. fn downcast(r: &RawErr) -> &str {
  16. match r.downcast_ref::<io::Error>() {
  17. Some(r) if r.kind() == io::ErrorKind::Interrupted => "io-interrupted",
  18. Some(_) => "io-other",
  19. None => "unknown",
  20. }
  21. }
  22.  
  23. fn main() {
  24. assert_eq!(downcast(&io_interrupted()), "io-interrupted");
  25. assert_eq!(downcast(&io_other()), "io-other");
  26. assert_eq!(downcast(&fmt()), "unknown");
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement