#![no_std]
//! 导出 `pconst` 中的常量和数据结构， 定义Alien OS的错误类型

use pconst::time::TimeVal;
pub use pconst::*;

pub type AlienError = LinuxErrno;
pub type AlienResult<T> = Result<T, AlienError>;

#[derive(Copy, Clone, Debug, Eq, PartialOrd, PartialEq, Hash, Ord)]
pub struct DeviceId {
    major: u32,
    minor: u32,
}

impl DeviceId {
    pub fn new(major: u32, minor: u32) -> Self {
        Self { major, minor }
    }
    pub fn major(&self) -> u32 {
        self.major
    }
    pub fn minor(&self) -> u32 {
        self.minor
    }
    pub fn id(&self) -> u64 {
        ((self.major as u64) << 32) | (self.minor as u64)
    }
}

impl From<u64> for DeviceId {
    fn from(id: u64) -> Self {
        Self {
            major: (id >> 32) as u32,
            minor: (id & 0xffffffff) as u32,
        }
    }
}

pub const AT_FDCWD: isize = -100isize;

const USEC_PER_SEC: usize = 1000_000;

pub trait FromUsize {
    fn from_usize(value: usize) -> Self;
}

impl FromUsize for TimeVal {
    fn from_usize(value: usize) -> Self {
        Self {
            tv_sec: value / USEC_PER_SEC,
            tv_usec: value % USEC_PER_SEC,
        }
    }
}
