Move pthread::Errno to separate module.

This commit is contained in:
4lDO2
2024-09-07 12:46:16 +02:00
parent e73b891e77
commit 4bd0d2a1ef
21 changed files with 77 additions and 80 deletions
+34
View File
@@ -0,0 +1,34 @@
use crate::platform::types::c_int;
/// Positive error codes (EINVAL, not -EINVAL).
#[derive(Debug, Eq, PartialEq)]
// TODO: Move to a more generic place.
pub struct Errno(pub c_int);
#[cfg(target_os = "redox")]
impl From<syscall::Error> for Errno {
fn from(value: syscall::Error) -> Self {
Errno(value.errno)
}
}
#[cfg(target_os = "redox")]
impl From<Errno> for syscall::Error {
fn from(value: Errno) -> Self {
syscall::Error::new(value.0)
}
}
pub trait ResultExt<T> {
fn or_minus_one_errno(self) -> T;
}
impl<T: From<i8>> ResultExt<T> for Result<T, Errno> {
fn or_minus_one_errno(self) -> T {
match self {
Self::Ok(v) => v,
Self::Err(Errno(errno)) => unsafe {
crate::platform::ERRNO.set(errno);
T::from(-1)
},
}
}
}