wasmer_wasix/state/
types.rs

1// TODO: review allow..
2use cfg_if::cfg_if;
3
4/// types for use in the WASI filesystem
5#[cfg(feature = "enable-serde")]
6use serde::{Deserialize, Serialize};
7
8cfg_if! {
9    if #[cfg(feature = "host-fs")] {
10        pub use virtual_fs::host_fs::{Stderr, Stdin, Stdout};
11    } else {
12        pub use virtual_fs::mem_fs::{Stderr, Stdin, Stdout};
13    }
14}
15
16#[derive(Debug, Clone)]
17#[allow(clippy::enum_variant_names)]
18pub enum PollEvent {
19    /// Data available to read
20    PollIn = 1,
21    /// Data available to write (will still block if data is greater than space available unless
22    /// the fd is configured to not block)
23    PollOut = 2,
24    /// Something didn't work. ignored as input
25    PollError = 4,
26    /// Connection closed. ignored as input
27    PollHangUp = 8,
28    /// Invalid request. ignored as input
29    PollInvalid = 16,
30}
31
32impl PollEvent {
33    fn from_i16(raw_num: i16) -> Option<PollEvent> {
34        Some(match raw_num {
35            1 => PollEvent::PollIn,
36            2 => PollEvent::PollOut,
37            4 => PollEvent::PollError,
38            8 => PollEvent::PollHangUp,
39            16 => PollEvent::PollInvalid,
40            _ => return None,
41        })
42    }
43}
44
45#[derive(Debug, Clone)]
46pub struct PollEventBuilder {
47    inner: PollEventSet,
48}
49
50pub type PollEventSet = i16;
51
52#[derive(Debug)]
53pub struct PollEventIter {
54    pes: PollEventSet,
55    i: usize,
56}
57
58impl Iterator for PollEventIter {
59    type Item = PollEvent;
60
61    fn next(&mut self) -> Option<Self::Item> {
62        if self.pes == 0 || self.i > 15 {
63            None
64        } else {
65            while self.i < 16 {
66                let result = PollEvent::from_i16(self.pes & (1 << self.i));
67                self.pes &= !(1 << self.i);
68                self.i += 1;
69                if let Some(r) = result {
70                    return Some(r);
71                }
72            }
73            unreachable!("Internal logic error in PollEventIter");
74        }
75    }
76}
77
78pub fn iterate_poll_events(pes: PollEventSet) -> PollEventIter {
79    PollEventIter { pes, i: 0 }
80}
81
82#[allow(dead_code)]
83impl PollEventBuilder {
84    pub fn new() -> PollEventBuilder {
85        PollEventBuilder { inner: 0 }
86    }
87
88    pub fn add(mut self, event: PollEvent) -> PollEventBuilder {
89        self.inner |= event as PollEventSet;
90        self
91    }
92
93    pub fn build(self) -> PollEventSet {
94        self.inner
95    }
96}
97
98#[allow(dead_code)]
99pub trait WasiPath {}
100
101/*
102TODO: Think about using this
103trait WasiFdBacking: std::fmt::Debug {
104    fn get_stat(&self) -> &Filestat;
105    fn get_stat_mut(&mut self) -> &mut Filestat;
106    fn is_preopened(&self) -> bool;
107    fn get_name(&self) -> &str;
108}
109*/