kernel/object/
mod.rs

1//! Kernel object management system
2//! 
3//! This module provides a unified abstraction for all kernel-managed resources
4//! including files, pipes, devices, and other IPC mechanisms.
5
6pub mod capability;
7pub mod introspection;
8pub mod handle;
9
10use alloc::{sync::Arc, vec::Vec};
11use crate::fs::FileObject;
12use crate::ipc::pipe::PipeObject;
13use capability::{StreamOps, CloneOps};
14
15/// Unified representation of all kernel-managed resources
16pub enum KernelObject {
17    File(Arc<dyn FileObject>),
18    Pipe(Arc<dyn PipeObject>),
19    // Future variants will be added here:
20    // CharDevice(Arc<dyn CharDevice>),
21    // Socket(Arc<dyn SocketObject>),
22}
23
24impl KernelObject {
25    /// Create a KernelObject from a FileObject
26    pub fn from_file_object(file_object: Arc<dyn FileObject>) -> Self {
27        KernelObject::File(file_object)
28    }
29    
30    /// Create a KernelObject from a PipeObject
31    pub fn from_pipe_object(pipe_object: Arc<dyn PipeObject>) -> Self {
32        KernelObject::Pipe(pipe_object)
33    }
34    
35    /// Try to get StreamOps capability
36    pub fn as_stream(&self) -> Option<&dyn StreamOps> {
37        match self {
38            KernelObject::File(file_object) => {
39                // FileObject automatically implements StreamOps
40                let stream_ops: &dyn StreamOps = file_object.as_ref();
41                Some(stream_ops)
42            }
43            KernelObject::Pipe(pipe_object) => {
44                // PipeObject automatically implements StreamOps
45                let stream_ops: &dyn StreamOps = pipe_object.as_ref();
46                Some(stream_ops)
47            }
48        }
49    }
50    
51    /// Try to get FileObject that provides file-like operations and stream capabilities
52    pub fn as_file(&self) -> Option<&dyn FileObject> {
53        match self {
54            KernelObject::File(file_object) => {
55                // FileObject automatically implements StreamOps
56                let file_ops: &dyn FileObject = file_object.as_ref();
57                Some(file_ops)
58            }
59            KernelObject::Pipe(_) => {
60                // Pipes don't provide file operations
61                None
62            }
63        }
64    }
65    
66    /// Try to get PipeObject that provides pipe-specific operations
67    pub fn as_pipe(&self) -> Option<&dyn PipeObject> {
68        match self {
69            KernelObject::File(_) => {
70                // Files don't provide pipe operations
71                None
72            }
73            KernelObject::Pipe(pipe_object) => {
74                let pipe_ops: &dyn PipeObject = pipe_object.as_ref();
75                Some(pipe_ops)
76            }
77        }
78    }
79    
80    /// Try to get CloneOps capability
81    pub fn as_cloneable(&self) -> Option<&dyn CloneOps> {
82        match self {
83            KernelObject::File(_) => {
84                None // Files do not implement CloneOps, use Arc::clone directly
85            }
86            KernelObject::Pipe(pipe_object) => {
87                // Check if PipeObject implements CloneOps
88                let cloneable: &dyn CloneOps = pipe_object.as_ref();
89                Some(cloneable)
90            }
91        }
92    }
93}
94
95impl Clone for KernelObject {
96    fn clone(&self) -> Self {
97        // Try to use CloneOps capability first
98        if let Some(cloneable) = self.as_cloneable() {
99            cloneable.custom_clone()
100        } else {
101            // Default: Use Arc::clone for direct cloning
102            match self {
103                KernelObject::File(file_object) => {
104                    KernelObject::File(Arc::clone(file_object))
105                }
106                KernelObject::Pipe(pipe_object) => {
107                    KernelObject::Pipe(Arc::clone(pipe_object))
108                }
109            }
110        }
111    }
112}
113
114#[cfg(test)]
115mod tests;