-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement IntoRawFd and modernize the codebase slightly
- Loading branch information
Showing
6 changed files
with
145 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,46 @@ | ||
//! Error handling. | ||
use std::fmt; | ||
|
||
/// Enumeration of errors possible in this library | ||
#[derive(thiserror::Error, Debug)] | ||
#[derive(Debug)] | ||
pub enum Error { | ||
/// Cannot convert the `name` argument to a C String! | ||
#[error("cannot convert `name` to a C string")] | ||
NameCStringConversion(#[source] std::ffi::NulError), | ||
NameCStringConversion(std::ffi::NulError), | ||
/// Cannot create the memfd | ||
#[error("cannot create memfd")] | ||
Create(#[source] std::io::Error), | ||
Create(std::io::Error), | ||
/// Cannot add new seals to the memfd | ||
#[error("cannot add seals to the memfd")] | ||
AddSeals(#[source] std::io::Error), | ||
AddSeals(std::io::Error), | ||
/// Cannot read the seals of a memfd | ||
#[error("cannot read seals for a memfd")] | ||
GetSeals(#[source] std::io::Error), | ||
GetSeals(std::io::Error), | ||
} | ||
|
||
impl std::error::Error for Error { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
use Error::*; | ||
match self { | ||
NameCStringConversion(ref e) => Some(e), | ||
Create(ref e) => Some(e), | ||
AddSeals(ref e) => Some(e), | ||
GetSeals(ref e) => Some(e), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
use Error::*; | ||
f.write_str(match self { | ||
NameCStringConversion(_) => "cannot convert `name` to a C string", | ||
Create(_) => "cannot create a memfd", | ||
AddSeals(_) => "cannot add seals to the memfd", | ||
GetSeals(_) => "cannot read seals for a memfd", | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
#[test] | ||
fn error_send_sync() { | ||
fn assert_error<E: std::error::Error + Send + Sync + 'static>() {} | ||
fn assert_error<E: std::error::Error + Send + Sync + fmt::Display + fmt::Debug + 'static>() {} | ||
assert_error::<Error>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
/* from <sys/memfd.h> */ | ||
|
||
pub const MFD_CLOEXEC: u32 = 1; | ||
pub const MFD_ALLOW_SEALING: u32 = 2; | ||
pub const MFD_HUGETLB: u32 = 4; | ||
pub(super) const MFD_CLOEXEC: u32 = 1; | ||
pub(super) const MFD_ALLOW_SEALING: u32 = 2; | ||
pub(super) const MFD_HUGETLB: u32 = 4; | ||
|
||
/* from <asm-generic/hugetlb_encode.h> */ | ||
|
||
pub const MFD_HUGE_SHIFT: u32 = 26; | ||
pub(super) const MFD_HUGE_SHIFT: u32 = 26; | ||
|
||
pub const MFD_HUGE_64KB: u32 = 16 << MFD_HUGE_SHIFT; | ||
pub const MFD_HUGE_512KB: u32 = 19 << MFD_HUGE_SHIFT; | ||
pub const MFD_HUGE_1MB: u32 = 20 << MFD_HUGE_SHIFT; | ||
pub const MFD_HUGE_2MB: u32 = 21 << MFD_HUGE_SHIFT; | ||
pub const MFD_HUGE_8MB: u32 = 23 << MFD_HUGE_SHIFT; | ||
pub const MFD_HUGE_16MB: u32 = 24 << MFD_HUGE_SHIFT; | ||
pub const MFD_HUGE_256MB: u32 = 28 << MFD_HUGE_SHIFT; | ||
pub const MFD_HUGE_1GB: u32 = 30 << MFD_HUGE_SHIFT; | ||
pub const MFD_HUGE_2GB: u32 = 31 << MFD_HUGE_SHIFT; | ||
pub const MFD_HUGE_16GB: u32 = 34 << MFD_HUGE_SHIFT; | ||
pub(super) const MFD_HUGE_64KB: u32 = 16 << MFD_HUGE_SHIFT; | ||
pub(super) const MFD_HUGE_512KB: u32 = 19 << MFD_HUGE_SHIFT; | ||
pub(super) const MFD_HUGE_1MB: u32 = 20 << MFD_HUGE_SHIFT; | ||
pub(super) const MFD_HUGE_2MB: u32 = 21 << MFD_HUGE_SHIFT; | ||
pub(super) const MFD_HUGE_8MB: u32 = 23 << MFD_HUGE_SHIFT; | ||
pub(super) const MFD_HUGE_16MB: u32 = 24 << MFD_HUGE_SHIFT; | ||
pub(super) const MFD_HUGE_256MB: u32 = 28 << MFD_HUGE_SHIFT; | ||
pub(super) const MFD_HUGE_1GB: u32 = 30 << MFD_HUGE_SHIFT; | ||
pub(super) const MFD_HUGE_2GB: u32 = 31 << MFD_HUGE_SHIFT; | ||
pub(super) const MFD_HUGE_16GB: u32 = 34 << MFD_HUGE_SHIFT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters