ableos/kernel/src/handle.rs

67 lines
1.4 KiB
Rust
Raw Normal View History

2023-06-13 06:00:11 -05:00
//! A handle module
2023-04-26 14:33:40 -05:00
use {
crate::arch::hardware_random_u64,
core::fmt::{self, Formatter},
};
2023-06-13 06:00:11 -05:00
/// An operating system handle without permissions attached
2023-04-12 13:08:07 -05:00
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
pub struct OSHandle {
2023-06-13 06:00:11 -05:00
id: u64,
2023-04-12 13:08:07 -05:00
}
2023-04-07 16:44:33 -05:00
2023-04-12 13:08:07 -05:00
impl OSHandle {
2023-06-13 06:00:11 -05:00
/// turn a u64 into an OSHandle
2023-04-12 13:08:07 -05:00
pub fn new_from_u64(id: u64) -> Self {
Self { id }
}
2023-06-13 06:00:11 -05:00
/// Generate a new OSHandle using the HAL random function
2023-04-12 13:08:07 -05:00
pub fn random_new() -> Self {
Self {
id: hardware_random_u64(),
}
}
}
2023-06-13 06:00:11 -05:00
/// A handle for resources
2023-04-12 13:08:07 -05:00
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
2023-04-07 16:44:33 -05:00
pub struct Handle {
2023-04-26 14:33:40 -05:00
id: OSHandle,
2023-07-10 22:54:05 -05:00
// TODO: Update this to be indexes into the caps
2023-04-07 16:44:33 -05:00
perms: Permissions,
}
impl Handle {
2023-06-13 06:00:11 -05:00
/// make a new handle
2023-04-07 16:44:33 -05:00
pub fn new() -> Handle {
Handle {
2023-04-26 14:33:40 -05:00
id: OSHandle::random_new(),
2023-04-07 16:44:33 -05:00
perms: Permissions::new(),
}
}
2023-06-13 06:00:11 -05:00
/// represent the os handle as a u64
2023-04-12 13:08:07 -05:00
pub fn as_u64(&self) -> u64 {
self.id.id
}
2023-04-07 16:44:33 -05:00
}
impl fmt::Display for Handle {
fn fmt(&self, w: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
2023-05-23 05:16:14 -05:00
write!(w, "{:?}", self.id)?;
2023-04-07 16:44:33 -05:00
Ok(())
}
}
2023-04-12 13:08:07 -05:00
#[derive(PartialEq, Hash, Eq, Debug, Clone, Copy)]
2023-06-13 06:00:11 -05:00
struct Permissions {
2023-04-26 14:33:40 -05:00
edit_children: bool,
2023-04-07 16:44:33 -05:00
edit_attributes: bool,
}
impl Permissions {
2023-06-13 06:00:11 -05:00
fn new() -> Self {
2023-04-07 16:44:33 -05:00
Self {
2023-04-26 14:33:40 -05:00
edit_children: true,
2023-04-07 16:44:33 -05:00
edit_attributes: true,
}
}
}