witx_codegen/rust/
header.rs

1use std::io::Write;
2
3use super::*;
4
5impl RustGenerator {
6    pub fn header<T: Write>(w: &mut PrettyWriter<T>) -> Result<(), Error> {
7        w.write_lines(
8            "
9//
10// This file was automatically generated by witx-codegen - Do not edit manually.
11//",
12        )?;
13        w.write_lines(
14            "
15#[derive(Debug, Copy, Clone, Eq, PartialEq)]
16pub enum Error {
17    WasiError(i32),
18}
19impl std::error::Error for Error {}
20impl std::fmt::Display for Error {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            Error::WasiError(e) => write!(f, \"Wasi error {}\", e),
24        }
25    }
26}
27
28pub type WasiHandle = i32;
29pub type Char8 = u8;
30pub type Char32 = u32;
31pub type WasiPtr<T> = *const T;
32pub type WasiMutPtr<T> = *mut T;
33pub type WasiStringBytesPtr = WasiPtr<Char8>;
34
35#[repr(C)]
36#[derive(Copy, Clone, Debug)]
37pub struct WasiSlice<T> {
38    ptr: WasiPtr<T>,
39    len: usize,
40}
41
42#[repr(C)]
43#[derive(Copy, Clone, Debug)]
44pub struct WasiMutSlice<T> {
45    ptr: WasiMutPtr<T>,
46    len: usize,
47}
48
49impl<T> WasiSlice<T> {
50    pub fn as_slice(&self) -> &[T] {
51        unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
52    }
53
54    pub fn from_slice(&self, slice: &[T]) -> Self {
55        WasiSlice {
56            ptr: slice.as_ptr() as _,
57            len: slice.len(),
58        }
59    }
60}
61
62impl<T> WasiMutSlice<T> {
63    pub fn as_slice(&self) -> &[T] {
64        unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
65    }
66
67    pub fn as_mut_slice(&self) -> &mut [T] {
68        unsafe { std::slice::from_raw_parts_mut(self.ptr, self.len) }
69    }
70
71    pub fn from_slice(&self, slice: &[T]) -> Self {
72        WasiMutSlice {
73            ptr: slice.as_ptr() as _,
74            len: slice.len(),
75        }
76    }
77
78    pub fn from_mut_slice(&self, slice: &mut [T]) -> Self {
79        WasiMutSlice {
80            ptr: slice.as_mut_ptr(),
81            len: slice.len(),
82        }
83    }
84}
85
86#[repr(C)]
87#[derive(Copy, Clone, Debug)]
88pub struct WasiString {
89    ptr: WasiStringBytesPtr,
90    len: usize,
91}
92
93impl<T: AsRef<str>> From<T> for WasiString {
94    fn from(s: T) -> Self {
95        let s = s.as_ref();
96        WasiString {
97            ptr: s.as_ptr() as _,
98            len: s.len(),
99        }
100    }
101}
102
103impl WasiString {
104    pub fn as_str(&self) -> Result<&str, std::str::Utf8Error> {
105        std::str::from_utf8(unsafe { std::slice::from_raw_parts(self.ptr, self.len) })
106    }
107
108    pub fn as_slice(&self) -> &[u8] {
109        unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
110    }
111
112    pub fn from_slice(&self, slice: &[u8]) -> Self {
113        WasiString {
114            ptr: slice.as_ptr() as _,
115            len: slice.len(),
116        }
117    }
118}
119",
120        )?;
121        w.eob()?;
122        Ok(())
123    }
124}