File_system/Fundamentals/
Entry.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::Type_type;

use super::{Inode_type, Size_type};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Entry_type {
    Inode: Inode_type,
    Name: String,
    Type: Type_type,
    Size: Size_type,
}

impl Entry_type {
    pub fn New(Inode: Inode_type, Name: String, Type: Type_type, Size: Size_type) -> Self {
        Self {
            Inode,
            Name,
            Type,
            Size,
        }
    }

    pub fn Get_inode(&self) -> Inode_type {
        self.Inode
    }

    pub fn Get_name(&self) -> &String {
        &self.Name
    }

    pub fn Get_type(&self) -> Type_type {
        self.Type
    }

    pub fn Get_size(&self) -> Size_type {
        self.Size
    }

    pub fn Set_inode(&mut self, Inode: Inode_type) {
        self.Inode = Inode;
    }

    pub fn Set_name(&mut self, Name: String) {
        self.Name = Name;
    }

    pub fn Set_type(&mut self, Type: Type_type) {
        self.Type = Type;
    }

    pub fn Set_size(&mut self, Size: Size_type) {
        self.Size = Size;
    }
}

impl AsMut<[u8]> for Entry_type {
    fn as_mut(&mut self) -> &mut [u8] {
        unsafe {
            core::slice::from_raw_parts_mut(
                self as *mut Entry_type as *mut u8,
                core::mem::size_of::<Entry_type>(),
            )
        }
    }
}

impl TryFrom<&mut [u8]> for &mut Entry_type {
    type Error = ();

    fn try_from(Value: &mut [u8]) -> Result<Self, Self::Error> {
        if Value.len() != core::mem::size_of::<Entry_type>() {
            return Err(());
        }
        if Value.as_ptr() as usize % core::mem::align_of::<Entry_type>() != 0 {
            return Err(());
        }

        #[allow(clippy::transmute_ptr_to_ref)]
        Ok(unsafe { core::mem::transmute::<*mut u8, &mut Entry_type>(Value.as_mut_ptr()) })
    }
}