memfd/
errors.rs

1//! Error handling.
2use std::fmt;
3
4/// Enumeration of errors possible in this library
5#[derive(Debug)]
6pub enum Error {
7    /// Cannot create the memfd
8    Create(std::io::Error),
9    /// Cannot add new seals to the memfd
10    AddSeals(std::io::Error),
11    /// Cannot read the seals of a memfd
12    GetSeals(std::io::Error),
13}
14
15impl std::error::Error for Error {
16    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
17        match self {
18            Error::Create(e) | Error::AddSeals(e) | Error::GetSeals(e) => Some(e),
19        }
20    }
21}
22
23impl fmt::Display for Error {
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        f.write_str(match self {
26            Error::Create(_) => "cannot create a memfd",
27            Error::AddSeals(_) => "cannot add seals to the memfd",
28            Error::GetSeals(_) => "cannot read seals for a memfd",
29        })
30    }
31}
32
33#[cfg(test)]
34#[test]
35fn error_send_sync() {
36    fn assert_error<E: std::error::Error + Send + Sync + fmt::Display + fmt::Debug + 'static>() {}
37    assert_error::<Error>();
38}