kona_common/
types.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
//! This module contains the local types for the `kona-common` crate.

/// File descriptors available to the `client` within the FPVM kernel.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileDescriptor {
    /// Read-only standard input stream.
    StdIn,
    /// Write-only standaard output stream.
    StdOut,
    /// Write-only standard error stream.
    StdErr,
    /// Read-only. Used to read the status of pre-image hinting.
    HintRead,
    /// Write-only. Used to provide pre-image hints
    HintWrite,
    /// Read-only. Used to read pre-images.
    PreimageRead,
    /// Write-only. Used to request pre-images.
    PreimageWrite,
    /// Other file descriptor.
    Wildcard(usize),
}

impl From<FileDescriptor> for usize {
    fn from(fd: FileDescriptor) -> Self {
        match fd {
            FileDescriptor::StdIn => 0,
            FileDescriptor::StdOut => 1,
            FileDescriptor::StdErr => 2,
            FileDescriptor::HintRead => 3,
            FileDescriptor::HintWrite => 4,
            FileDescriptor::PreimageRead => 5,
            FileDescriptor::PreimageWrite => 6,
            FileDescriptor::Wildcard(value) => value,
        }
    }
}

impl From<FileDescriptor> for i32 {
    fn from(fd: FileDescriptor) -> Self {
        usize::from(fd) as Self
    }
}

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

    #[test]
    fn test_file_descriptor_into_usize() {
        assert_eq!(usize::from(FileDescriptor::StdIn), 0);
        assert_eq!(usize::from(FileDescriptor::StdOut), 1);
        assert_eq!(usize::from(FileDescriptor::StdErr), 2);
        assert_eq!(usize::from(FileDescriptor::HintRead), 3);
        assert_eq!(usize::from(FileDescriptor::HintWrite), 4);
        assert_eq!(usize::from(FileDescriptor::PreimageRead), 5);
        assert_eq!(usize::from(FileDescriptor::PreimageWrite), 6);
        assert_eq!(usize::from(FileDescriptor::Wildcard(7)), 7);
    }

    #[test]
    fn test_file_descriptor_into_i32() {
        assert_eq!(i32::from(FileDescriptor::StdIn), 0);
        assert_eq!(i32::from(FileDescriptor::StdOut), 1);
        assert_eq!(i32::from(FileDescriptor::StdErr), 2);
        assert_eq!(i32::from(FileDescriptor::HintRead), 3);
        assert_eq!(i32::from(FileDescriptor::HintWrite), 4);
        assert_eq!(i32::from(FileDescriptor::PreimageRead), 5);
        assert_eq!(i32::from(FileDescriptor::PreimageWrite), 6);
        assert_eq!(i32::from(FileDescriptor::Wildcard(7)), 7);
    }
}