use spin::Lazy; use spin::{Mutex}; use crate::interp::objects::{Object,HandleTarget, TARGETS}; use { crate::arch::hardware_random_u64, alloc::vec::Vec, core::fmt::{self, Formatter}, }; use alloc::{ collections::BTreeMap, }; #[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)] pub struct OSHandle { pub id: u64, } impl OSHandle { pub fn new_from_u64(id: u64) -> Self { Self { id } } pub fn random_new() -> Self { Self { id: hardware_random_u64(), } } } #[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)] pub struct Handle { pub id: OSHandle, pub perms: Permissions, pub r#ref: usize, } impl Handle { pub fn new(r#ref: usize) -> Handle { Handle { id: OSHandle::random_new(), perms: Permissions::new(), r#ref, } } pub fn as_u64(&self) -> u64 { self.id.id } pub fn get FnOnce(Option<&'a mut HandleTarget>) -> R>(&self, f: F) -> R { let l = TARGETS; let mut olock = l.lock(); let a = olock.get_mut(self.r#ref).and_then(|a| a.as_mut()); return f(a); } } impl fmt::Display for Handle { fn fmt(&self, w: &mut Formatter<'_>) -> Result<(), core::fmt::Error> { write!(w, "{:?}", self.id); Ok(()) } } #[derive(PartialEq, Hash, Eq, Debug, Clone, Copy)] pub struct Permissions { edit_children: bool, edit_attributes: bool, } impl Permissions { pub fn new() -> Self { Self { edit_children: true, edit_attributes: true, } } } pub const GLOBALS: Lazy>> = Lazy::new(|| { let mut globals = BTreeMap::new(); Mutex::new(globals) });