Commit 17fd5e70 authored by Julien Férard's avatar Julien Férard

Extract result.rs of error.rs to write "result::demux" instead of "Error::demux"

parent fc69b531
use super::error::{Error, Result};
use super::result::{self, Result};
pub unsafe fn syscall0(mut a: usize) -> Result<usize> {
asm!("int 0x80"
......@@ -7,7 +7,7 @@ pub unsafe fn syscall0(mut a: usize) -> Result<usize> {
: "memory"
: "intel", "volatile");
Error::demux(a)
result::demux(a)
}
pub unsafe fn syscall1(mut a: usize, b: usize) -> Result<usize> {
......@@ -17,7 +17,7 @@ pub unsafe fn syscall1(mut a: usize, b: usize) -> Result<usize> {
: "memory"
: "intel", "volatile");
Error::demux(a)
result::demux(a)
}
// Clobbers all registers - special for clone
......@@ -29,7 +29,7 @@ pub unsafe fn syscall1_clobber(mut a: usize, b: usize) -> Result<usize> {
"r9", "r10", "r11", "r12", "r13", "r14", "r15"
: "intel", "volatile");
Error::demux(a)
result::demux(a)
}
pub unsafe fn syscall2(mut a: usize, b: usize, c: usize) -> Result<usize> {
......@@ -39,7 +39,7 @@ pub unsafe fn syscall2(mut a: usize, b: usize, c: usize) -> Result<usize> {
: "memory"
: "intel", "volatile");
Error::demux(a)
result::demux(a)
}
pub unsafe fn syscall3(mut a: usize, b: usize, c: usize, d: usize) -> Result<usize> {
......@@ -49,7 +49,7 @@ pub unsafe fn syscall3(mut a: usize, b: usize, c: usize, d: usize) -> Result<usi
: "memory"
: "intel", "volatile");
Error::demux(a)
result::demux(a)
}
pub unsafe fn syscall4(mut a: usize, b: usize, c: usize, d: usize, e: usize) -> Result<usize> {
......@@ -59,7 +59,7 @@ pub unsafe fn syscall4(mut a: usize, b: usize, c: usize, d: usize, e: usize) ->
: "memory"
: "intel", "volatile");
Error::demux(a)
result::demux(a)
}
pub unsafe fn syscall5(mut a: usize, b: usize, c: usize, d: usize, e: usize, f: usize)
......@@ -70,5 +70,5 @@ pub unsafe fn syscall5(mut a: usize, b: usize, c: usize, d: usize, e: usize, f:
: "memory"
: "intel", "volatile");
Error::demux(a)
result::demux(a)
}
use super::arch::*;
use super::data::{SigAction, Stat, StatVfs, TimeSpec};
use super::error::Result;
use super::result::Result;
use super::number::*;
use core::{mem, ptr};
......
use core::{fmt, result};
use core::fmt;
use core::result;
/// An error returned by the kernel, identified by an `errno`.
#[derive(Eq, PartialEq)]
......@@ -6,9 +7,6 @@ pub struct Error {
pub errno: i32,
}
/// A `Result` specialization for results in the kernel.
pub type Result<T> = result::Result<T, Error>;
impl Error {
/// Creates a new `Error`.
///
......@@ -21,52 +19,6 @@ impl Error {
Error { errno: errno }
}
/// Error multiplexer: encodes a `Result` into an `usize`. Returns `n` if
/// result is `Ok(n)`, else `-error.errno` if result is `Err(error)`:
///
/// ```
/// use syscall::*;
/// assert_eq!(5 as usize, Error::mux(Ok(5)));
/// assert_eq!(0 as usize, Error::mux(Ok(0)));
/// assert_eq!(-132i32 as usize, Error::mux(Ok(-132i32 as usize)));
/// assert_eq!(-EIO as usize, Error::mux(Err(Error::new(EIO))));
/// ```
///
/// * `Ok`s are mapped to the numbers between `0` and
/// `2^[bits] - 132` inclusive.
/// * `Err`s are mapped to the numbers between `2^[bits] - 131`
/// and `2^[bits] - 1` inclusive.
///
/// # Panics
/// Panics if `Ok` value is greater than `2^[bits] - 132`.
pub fn mux(result: Result<usize>) -> usize {
match result {
Ok(value) if value <= MAX_OK_VALUE => value,
Ok(_) => panic!("Huge Ok value can't be multiplexed."),
Err(error) => -error.errno as usize,
}
}
/// Error demultiplexer: decodes an `usize` into an `Result`. Returns
/// `Err(Error::new(-value))` if value (as i32) is in range 1..=131,
/// else `Ok(value)`.
///
/// ```
/// use syscall::*;
/// assert_eq!(Ok(5), Error::demux(5));
/// assert_eq!(Ok(0), Error::demux(0));
/// assert_eq!(Err(Error::new(EIO)), Error::demux(-EIO as usize));
/// ```
pub fn demux(value: usize) -> Result<usize> {
let errno = -(value as i32);
if errno >= 1 && errno <= MAX_ERRNO {
Err(Error::new(errno))
} else {
Ok(value)
}
}
/// Returns a text representation of this error.
///
/// ```
......@@ -490,10 +442,7 @@ pub const ENOTRECOVERABLE: i32 = 131;
const MIN_ERRNO: i32 = EPERM;
/// Max error no
const MAX_ERRNO: i32 = ENOTRECOVERABLE;
/// Max ok value
const MAX_OK_VALUE: usize = -(MAX_ERRNO + 1) as usize;
pub const MAX_ERRNO: i32 = ENOTRECOVERABLE;
/// Error texts
#[deprecated(note = "use error_str function")]
......@@ -670,10 +619,4 @@ mod tests {
fn big_errno() {
let _ = Error::new(200);
}
#[test]
#[should_panic]
fn huge_value() {
let _ = Error::mux(Ok(-131i32 as usize));
}
}
\ No newline at end of file
......@@ -8,6 +8,7 @@ pub use self::arch::*;
pub use self::call::*;
pub use self::data::*;
pub use self::error::*;
pub use self::result::*;
pub use self::flag::*;
pub use self::io::*;
pub use self::number::*;
......@@ -34,6 +35,9 @@ pub mod data;
/// All errors that can be generated by a system call
pub mod error;
/// Results that can be generated by a system call
pub mod result;
/// Flags used as an argument to many system calls
pub mod flag;
......
use core::result;
use error::{Error, MAX_ERRNO};
/// Max ok value
const MAX_OK_VALUE: usize = -(MAX_ERRNO + 1) as usize;
/// A `Result` specialization for results in the kernel.
pub type Result<T> = result::Result<T, Error>;
/// Result multiplexer: encodes a `Result` into an `usize`. Returns `n` if
/// result is `Ok(n)`, else `-error.errno` if result is `Err(error)`:
///
/// ```
/// use syscall::*;
/// assert_eq!(5 as usize, mux(Ok(5)));
/// assert_eq!(0 as usize, mux(Ok(0)));
/// assert_eq!(-132i32 as usize, mux(Ok(-132i32 as usize)));
/// assert_eq!(-EIO as usize, mux(Err(Error::new(EIO))));
/// ```
///
/// * `Ok`s are mapped to the numbers between `0` and
/// `2^[bits] - 132` inclusive.
/// * `Err`s are mapped to the numbers between `2^[bits] - 131`
/// and `2^[bits] - 1` inclusive.
///
/// # Panics
/// Panics if `Ok` value is greater than `2^[bits] - 132`.
pub fn mux(result: Result<usize>) -> usize {
match result {
Ok(value) if value <= MAX_OK_VALUE => value,
Ok(_) => panic!("Huge Ok value can't be multiplexed."),
Err(error) => -error.errno as usize,
}
}
/// Result demultiplexer: decodes an `usize` into an `Result`. Returns
/// `Err(Error::new(-value))` if value (as i32) is in range 1..=131,
/// else `Ok(value)`.
///
/// ```
/// use syscall::*;
/// assert_eq!(Ok(5), demux(5));
/// assert_eq!(Ok(0), demux(0));
/// assert_eq!(Err(Error::new(EIO)), demux(-EIO as usize));
/// ```
pub fn demux(value: usize) -> Result<usize> {
let errno = -(value as i32);
if errno >= 1 && errno <= MAX_ERRNO {
Err(Error::new(errno))
} else {
Ok(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic]
fn mux_huge_value() {
let _ = mux(Ok(-131i32 as usize));
}
}
......@@ -2,6 +2,7 @@ use core::{mem, slice};
use data::*;
use error::*;
use result::{self, Result};
use number::*;
pub trait Scheme {
......@@ -44,7 +45,7 @@ pub trait Scheme {
_ => Err(Error::new(ENOSYS))
};
packet.a = Error::mux(res);
packet.a = result::mux(res);
}
/* Scheme operations */
......
......@@ -2,6 +2,7 @@ use core::{mem, slice};
use data::*;
use error::*;
use result::{self, Result};
use number::*;
pub trait SchemeBlock {
......@@ -44,7 +45,7 @@ pub trait SchemeBlock {
_ => Err(Error::new(ENOSYS))
};
res.transpose().map(Error::mux)
res.transpose().map(result::mux)
}
/* Scheme operations */
......
......@@ -2,6 +2,7 @@ use core::{mem, slice};
use data::*;
use error::*;
use result::{self, Result};
use number::*;
pub trait SchemeBlockMut {
......@@ -44,7 +45,7 @@ pub trait SchemeBlockMut {
_ => Err(Error::new(ENOSYS))
};
res.transpose().map(Error::mux)
res.transpose().map(result::mux)
}
/* Scheme operations */
......
......@@ -2,6 +2,7 @@ use core::{mem, slice};
use data::*;
use error::*;
use result::{self, Result};
use number::*;
pub trait SchemeMut {
......@@ -44,7 +45,7 @@ pub trait SchemeMut {
_ => Err(Error::new(ENOSYS))
};
packet.a = Error::mux(res);
packet.a = result::mux(res);
}
/* Scheme operations */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment