1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::marker::PhantomData;

pub use twizzler_abi::object::Protections;
use twizzler_abi::{object::ObjID, syscall::ObjectMapError};

use crate::object::Object;

bitflags::bitflags! {
    /// Flags to pass to object initialization routines.
    pub struct ObjectInitFlags: u32 {
    }
}

/// Possible errors from initializing an object handle.
#[derive(Debug, Copy, Clone)]
pub enum ObjectInitError {
    /// The ID isn't valid.
    InvalidId,
    /// There are not enough memory slots.
    OutOfSlots,
    /// The mapping failed.
    MappingFailed,
    /// The requested protections are invalid.
    InvalidProtections,
    /// The object doesn't exist.
    ObjectNotFound,
}

impl From<ObjectMapError> for ObjectInitError {
    fn from(x: ObjectMapError) -> Self {
        match x {
            ObjectMapError::ObjectNotFound => ObjectInitError::ObjectNotFound,
            ObjectMapError::InvalidProtections => ObjectInitError::InvalidProtections,
            _ => ObjectInitError::MappingFailed,
        }
    }
}

impl<T> Object<T> {
    /// Initialize an object handle from an object ID.
    pub fn init_id(
        id: ObjID,
        prot: Protections,
        _flags: ObjectInitFlags,
    ) -> Result<Self, ObjectInitError> {
        Ok(Self {
            slot: crate::slot::get(id, prot)?,
            _pd: PhantomData,
        })
    }
}