io/mio_util/
notify_error.rs

1use mio_misc::channel;
2use std::{any, error, fmt, io};
3
4pub enum NotifyError<T> {
5    Io(io::Error),
6    Full(T),
7    Closed(Option<T>),
8    NotificationQueueFull,
9}
10
11impl<M> fmt::Debug for NotifyError<M> {
12    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
13        match *self {
14            NotifyError::Io(ref e) => {
15                write!(fmt, "NotifyError::Io({:?})", e)
16            }
17            NotifyError::Full(..) => {
18                write!(fmt, "NotifyError::Full(..)")
19            }
20            NotifyError::Closed(..) => {
21                write!(fmt, "NotifyError::Closed(..)")
22            }
23            NotifyError::NotificationQueueFull => {
24                write!(fmt, "NotifyError::NotificationQueueFull")
25            }
26        }
27    }
28}
29
30impl<M> fmt::Display for NotifyError<M> {
31    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
32        match *self {
33            NotifyError::Io(ref e) => {
34                write!(fmt, "IO error: {}", e)
35            }
36            NotifyError::Full(..) => write!(fmt, "Full"),
37            NotifyError::Closed(..) => write!(fmt, "Closed"),
38            NotifyError::NotificationQueueFull => {
39                write!(fmt, "Notification queue is full")
40            }
41        }
42    }
43}
44
45impl<M: any::Any> error::Error for NotifyError<M> {
46    fn description(&self) -> &str {
47        match *self {
48            NotifyError::Io(ref err) => err.description(),
49            NotifyError::Closed(..) => "The receiving end has hung up",
50            NotifyError::Full(..) => "Queue is full",
51            NotifyError::NotificationQueueFull => "Notification queue is full",
52        }
53    }
54
55    fn cause(&self) -> Option<&dyn error::Error> {
56        match *self {
57            NotifyError::Io(ref err) => Some(err),
58            _ => None,
59        }
60    }
61}
62
63impl<M> From<channel::TrySendError<M>> for NotifyError<M> {
64    fn from(src: channel::TrySendError<M>) -> NotifyError<M> {
65        match src {
66            channel::TrySendError::Io(e) => NotifyError::Io(e),
67            channel::TrySendError::Full(v) => NotifyError::Full(v),
68            channel::TrySendError::Disconnected(v) => {
69                NotifyError::Closed(Some(v))
70            }
71            channel::TrySendError::NotificationQueueFull => {
72                NotifyError::NotificationQueueFull
73            }
74        }
75    }
76}