Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::{io, rc::Rc};
- type RawErr = Rc<Box<dyn std::error::Error>>;
- fn io_interrupted() -> RawErr {
- Rc::new(Box::new(io::Error::from(io::ErrorKind::Interrupted)))
- }
- fn io_other() -> RawErr {
- Rc::new(Box::new(io::Error::from(io::ErrorKind::UnexpectedEof)))
- }
- fn fmt() -> RawErr {
- Rc::new(Box::new(std::fmt::Error))
- }
- fn downcast(r: &RawErr) -> &str {
- match r.downcast_ref::<io::Error>() {
- Some(r) if r.kind() == io::ErrorKind::Interrupted => "io-interrupted",
- Some(_) => "io-other",
- None => "unknown",
- }
- }
- fn main() {
- assert_eq!(downcast(&io_interrupted()), "io-interrupted");
- assert_eq!(downcast(&io_other()), "io-other");
- assert_eq!(downcast(&fmt()), "unknown");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement