File_system/Fundamentals/Identifiers/
File.rs

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
52
53
54
55
56
57
58
59
60
61
62
63
/// File identifier inner type
///
/// This is the inner/raw type of [`File_identifier_type`].
/// It size is the half of the `target_pointer_width` :
/// - 16 bits on 32 bits systems
/// - 32 bits on 64 bits systems
#[cfg(target_pointer_width = "32")]
pub type File_identifier_inner_type = u16;
#[cfg(target_pointer_width = "64")]
pub type File_identifier_inner_type = u32;

/// File identifier type
///
/// This type is used to identify an opened file in a file system.
/// This is similar to a file descriptor in Unix-like systems.
/// It is a wrapper around [`File_identifier_inner_type`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[repr(transparent)]
pub struct File_identifier_type(File_identifier_inner_type);

impl File_identifier_type {
    pub const Size_bits: u8 = core::mem::size_of::<File_identifier_inner_type>() as u8 * 8;

    pub const Standard_in: File_identifier_type = File_identifier_type::New(0);
    pub const Standard_out: File_identifier_type = File_identifier_type::New(1);
    pub const Standard_error: File_identifier_type = File_identifier_type::New(2);

    pub const Minimum: File_identifier_type = File_identifier_type::New(3);
    pub const Maximum: File_identifier_type =
        File_identifier_type::New(File_identifier_inner_type::MAX);

    pub const fn New(Identifier: File_identifier_inner_type) -> Self {
        Self(Identifier)
    }

    pub const fn Into_inner(self) -> File_identifier_inner_type {
        self.0
    }
}

impl From<File_identifier_inner_type> for File_identifier_type {
    fn from(Internal_file_identifier: File_identifier_inner_type) -> Self {
        File_identifier_type(Internal_file_identifier)
    }
}

impl From<File_identifier_type> for File_identifier_inner_type {
    fn from(Internal_file_identifier: File_identifier_type) -> Self {
        Internal_file_identifier.0
    }
}

#[cfg(test)]
mod Tests {
    use super::*;

    #[test]
    fn Test_file_identifier() {
        let Identifier = File_identifier_type::from(0x1234);
        assert_eq!(Identifier, File_identifier_type::New(0x1234));
        assert_eq!(File_identifier_inner_type::from(Identifier), 0x1234);
    }
}