// From https://github.com/bytecodealliance/wasi/blob/main/src/lib_generated.rs
// This file is automatically generated, DO NOT EDIT
//
// To regenerate this file run the `crates/witx-bindgen` command
use core::fmt;
use core::mem::MaybeUninit;
pub type Size = usize;
pub type Filesize = u64;
pub type Timestamp = u64;
#[repr(transparent)]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Clockid(u32);
impl Clockid {
pub fn name(&self) -> &'static str {
match self.0 {
0 => "REALTIME",
1 => "MONOTONIC",
2 => "PROCESS_CPUTIME_ID",
3 => "THREAD_CPUTIME_ID",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
pub fn message(&self) -> &'static str {
match self.0 {
0 => {
"The clock measuring real time. Time value zero corresponds with
1970-01-01T00:00:00Z."
}
1 => {
"The store-wide monotonic clock, which is defined as a clock measuring
real time, whose value cannot be adjusted and which cannot have negative
clock jumps. The epoch of this clock is undefined. The absolute time
value of this clock therefore has no meaning."
}
2 => "The CPU-time clock associated with the current process.",
3 => "The CPU-time clock associated with the current thread.",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
}
impl fmt::Debug for Clockid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Clockid")
.field("code", &self.0)
.field("name", &self.name())
.field("message", &self.message())
.finish()
}
}
#[repr(transparent)]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Errno(u16);
/// No error occurred. System call completed successfully.
pub const ERRNO_SUCCESS: Errno = Errno(0);
impl Errno {
pub fn new(x: u16) -> Self {
Self(x)
}
pub const fn raw(&self) -> u16 {
self.0
}
pub fn name(&self) -> &'static str {
match self.0 {
0 => "SUCCESS",
1 => "2BIG",
2 => "ACCES",
3 => "ADDRINUSE",
4 => "ADDRNOTAVAIL",
5 => "AFNOSUPPORT",
6 => "AGAIN",
7 => "ALREADY",
8 => "BADF",
9 => "BADMSG",
10 => "BUSY",
11 => "CANCELED",
12 => "CHILD",
13 => "CONNABORTED",
14 => "CONNREFUSED",
15 => "CONNRESET",
16 => "DEADLK",
17 => "DESTADDRREQ",
18 => "DOM",
19 => "DQUOT",
20 => "EXIST",
21 => "FAULT",
22 => "FBIG",
23 => "HOSTUNREACH",
24 => "IDRM",
25 => "ILSEQ",
26 => "INPROGRESS",
27 => "INTR",
28 => "INVAL",
29 => "IO",
30 => "ISCONN",
31 => "ISDIR",
32 => "LOOP",
33 => "MFILE",
34 => "MLINK",
35 => "MSGSIZE",
36 => "MULTIHOP",
37 => "NAMETOOLONG",
38 => "NETDOWN",
39 => "NETRESET",
40 => "NETUNREACH",
41 => "NFILE",
42 => "NOBUFS",
43 => "NODEV",
44 => "NOENT",
45 => "NOEXEC",
46 => "NOLCK",
47 => "NOLINK",
48 => "NOMEM",
49 => "NOMSG",
50 => "NOPROTOOPT",
51 => "NOSPC",
52 => "NOSYS",
53 => "NOTCONN",
54 => "NOTDIR",
55 => "NOTEMPTY",
56 => "NOTRECOVERABLE",
57 => "NOTSOCK",
58 => "NOTSUP",
59 => "NOTTY",
60 => "NXIO",
61 => "OVERFLOW",
62 => "OWNERDEAD",
63 => "PERM",
64 => "PIPE",
65 => "PROTO",
66 => "PROTONOSUPPORT",
67 => "PROTOTYPE",
68 => "RANGE",
69 => "ROFS",
70 => "SPIPE",
71 => "SRCH",
72 => "STALE",
73 => "TIMEDOUT",
74 => "TXTBSY",
75 => "XDEV",
76 => "NOTCAPABLE",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
pub fn message(&self) -> &'static str {
match self.0 {
0 => "No error occurred. System call completed successfully.",
1 => "Argument list too long.",
2 => "Permission denied.",
3 => "Address in use.",
4 => "Address not available.",
5 => "Address family not supported.",
6 => "Resource unavailable, or operation would block.",
7 => "Connection already in progress.",
8 => "Bad file descriptor.",
9 => "Bad message.",
10 => "Device or resource busy.",
11 => "Operation canceled.",
12 => "No child processes.",
13 => "Connection aborted.",
14 => "Connection refused.",
15 => "Connection reset.",
16 => "Resource deadlock would occur.",
17 => "Destination address required.",
18 => "Mathematics argument out of domain of function.",
19 => "Reserved.",
20 => "File exists.",
21 => "Bad address.",
22 => "File too large.",
23 => "Host is unreachable.",
24 => "Identifier removed.",
25 => "Illegal byte sequence.",
26 => "Operation in progress.",
27 => "Interrupted function.",
28 => "Invalid argument.",
29 => "I/O error.",
30 => "Socket is connected.",
31 => "Is a directory.",
32 => "Too many levels of symbolic links.",
33 => "File descriptor value too large.",
34 => "Too many links.",
35 => "Message too large.",
36 => "Reserved.",
37 => "Filename too long.",
38 => "Network is down.",
39 => "Connection aborted by network.",
40 => "Network unreachable.",
41 => "Too many files open in system.",
42 => "No buffer space available.",
43 => "No such device.",
44 => "No such file or directory.",
45 => "Executable file format error.",
46 => "No locks available.",
47 => "Reserved.",
48 => "Not enough space.",
49 => "No message of the desired type.",
50 => "Protocol not available.",
51 => "No space left on device.",
52 => "Function not supported.",
53 => "The socket is not connected.",
54 => "Not a directory or a symbolic link to a directory.",
55 => "Directory not empty.",
56 => "State not recoverable.",
57 => "Not a socket.",
58 => "Not supported, or operation not supported on socket.",
59 => "Inappropriate I/O control operation.",
60 => "No such device or address.",
61 => "Value too large to be stored in data type.",
62 => "Previous owner died.",
63 => "Operation not permitted.",
64 => "Broken pipe.",
65 => "Protocol error.",
66 => "Protocol not supported.",
67 => "Protocol wrong type for socket.",
68 => "Result too large.",
69 => "Read-only file system.",
70 => "Invalid seek.",
71 => "No such process.",
72 => "Reserved.",
73 => "Connection timed out.",
74 => "Text file busy.",
75 => "Cross-device link.",
76 => "Extension: Capabilities insufficient.",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
}
impl fmt::Debug for Errno {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Errno")
.field("code", &self.0)
.field("name", &self.name())
.field("message", &self.message())
.finish()
}
}
impl fmt::Display for Errno {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} (error {})", self.name(), self.0)
}
}
pub type Rights = u64;
pub type Fd = u32;
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Iovec {
/// The address of the buffer to be filled.
pub buf: *mut u8,
/// The length of the buffer to be filled.
pub buf_len: Size,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Ciovec {
/// The address of the buffer to be written.
pub buf: *const u8,
/// The length of the buffer to be written.
pub buf_len: Size,
}
#[repr(transparent)]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Whence(u8);
impl Whence {
pub fn name(&self) -> &'static str {
match self.0 {
0 => "SET",
1 => "CUR",
2 => "END",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
pub fn message(&self) -> &'static str {
match self.0 {
0 => "Seek relative to start-of-file.",
1 => "Seek relative to current position.",
2 => "Seek relative to end-of-file.",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
}
impl fmt::Debug for Whence {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Whence")
.field("code", &self.0)
.field("name", &self.name())
.field("message", &self.message())
.finish()
}
}
pub type Dircookie = u64;
pub type Dirnamlen = u32;
pub type Inode = u64;
#[repr(transparent)]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Filetype(u8);
impl Filetype {
pub fn name(&self) -> &'static str {
match self.0 {
0 => "UNKNOWN",
1 => "BLOCK_DEVICE",
2 => "CHARACTER_DEVICE",
3 => "DIRECTORY",
4 => "REGULAR_FILE",
5 => "SOCKET_DGRAM",
6 => "SOCKET_STREAM",
7 => "SYMBOLIC_LINK",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
pub fn message(&self) -> &'static str {
match self.0 {0 => "The type of the file descriptor or file is unknown or is different from any of the other types specified.",1 => "The file descriptor or file refers to a block device inode.",2 => "The file descriptor or file refers to a character device inode.",3 => "The file descriptor or file refers to a directory inode.",4 => "The file descriptor or file refers to a regular file inode.",5 => "The file descriptor or file refers to a datagram socket.",6 => "The file descriptor or file refers to a byte-stream socket.",7 => "The file refers to a symbolic link inode.",_ => unsafe { core::hint::unreachable_unchecked() },}
}
}
impl fmt::Debug for Filetype {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Filetype")
.field("code", &self.0)
.field("name", &self.name())
.field("message", &self.message())
.finish()
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Dirent {
/// The offset of the next directory entry stored in this directory.
pub d_next: Dircookie,
/// The serial number of the file referred to by this directory entry.
pub d_ino: Inode,
/// The length of the name of the directory entry.
pub d_namlen: Dirnamlen,
/// The type of the file referred to by this directory entry.
pub d_type: Filetype,
}
#[repr(transparent)]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Advice(u8);
impl Advice {
pub fn name(&self) -> &'static str {
match self.0 {
0 => "NORMAL",
1 => "SEQUENTIAL",
2 => "RANDOM",
3 => "WILLNEED",
4 => "DONTNEED",
5 => "NOREUSE",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
pub fn message(&self) -> &'static str {
match self.0 {0 => "The application has no advice to give on its behavior with respect to the specified data.",1 => "The application expects to access the specified data sequentially from lower offsets to higher offsets.",2 => "The application expects to access the specified data in a random order.",3 => "The application expects to access the specified data in the near future.",4 => "The application expects that it will not access the specified data in the near future.",5 => "The application expects to access the specified data once and then not reuse it thereafter.",_ => unsafe { core::hint::unreachable_unchecked() },}
}
}
impl fmt::Debug for Advice {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Advice")
.field("code", &self.0)
.field("name", &self.name())
.field("message", &self.message())
.finish()
}
}
/// File descriptor flags
///
/// Available flags are:
/// [FDFLAGS_APPEND], [FDFLAGS_DSYNC], [FDFLAGS_NONBLOCK], [FDFLAGS_RSYNC], [FDFLAGS_SYNC]
pub type Fdflags = u16;
/// Append mode: Data written to the file is always appended to the file's end.
pub const FDFLAGS_APPEND: Fdflags = 1 << 0;
/// Write according to synchronized I/O data integrity completion. Only the data stored in the file is synchronized.
pub const FDFLAGS_DSYNC: Fdflags = 1 << 1;
/// Non-blocking mode.
pub const FDFLAGS_NONBLOCK: Fdflags = 1 << 2;
/// Synchronized read I/O operations.
pub const FDFLAGS_RSYNC: Fdflags = 1 << 3;
/// Write according to synchronized I/O file integrity completion. In
/// addition to synchronizing the data stored in the file, the implementation
/// may also synchronously update the file's metadata.
pub const FDFLAGS_SYNC: Fdflags = 1 << 4;
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Fdstat {
/// File type.
pub fs_filetype: Filetype,
/// File descriptor flags.
pub fs_flags: Fdflags,
/// Rights that apply to this file descriptor.
pub fs_rights_base: Rights,
/// Maximum set of rights that may be installed on new file descriptors that
/// are created through this file descriptor, e.g., through `path_open`.
pub fs_rights_inheriting: Rights,
}
pub type Device = u64;
pub type Linkcount = u64;
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Filestat {
/// Device ID of device containing the file.
pub dev: Device,
/// File serial number.
pub ino: Inode,
/// File type.
pub filetype: Filetype,
/// Number of hard links to the file.
pub nlink: Linkcount,
/// For regular files, the file size in bytes. For symbolic links, the length in bytes of the pathname contained in the symbolic link.
pub size: Filesize,
/// Last data access timestamp.
pub atim: Timestamp,
/// Last data modification timestamp.
pub mtim: Timestamp,
/// Last file status change timestamp.
pub ctim: Timestamp,
}
pub type Userdata = u64;
#[repr(transparent)]
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Eventtype(u8);
/// The time value of clock `subscription_clock::id` has
/// reached timestamp `subscription_clock::timeout`.
pub const EVENTTYPE_CLOCK: Eventtype = Eventtype(0);
/// File descriptor `subscription_fd_readwrite::file_descriptor` has data
/// available for reading. This event always triggers for regular files.
pub const EVENTTYPE_FD_READ: Eventtype = Eventtype(1);
/// File descriptor `subscription_fd_readwrite::file_descriptor` has capacity
/// available for writing. This event always triggers for regular files.
pub const EVENTTYPE_FD_WRITE: Eventtype = Eventtype(2);
impl Eventtype {
pub const fn raw(&self) -> u8 {
self.0
}
pub fn name(&self) -> &'static str {
match self.0 {
0 => "CLOCK",
1 => "FD_READ",
2 => "FD_WRITE",
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
pub fn message(&self) -> &'static str {
match self.0 {
0 => {
"The time value of clock `subscription_clock::id` has
reached timestamp `subscription_clock::timeout`."
}
1 => {
"File descriptor `subscription_fd_readwrite::file_descriptor` has data
available for reading. This event always triggers for regular files."
}
2 => {
"File descriptor `subscription_fd_readwrite::file_descriptor` has capacity
available for writing. This event always triggers for regular files."
}
_ => unsafe { core::hint::unreachable_unchecked() },
}
}
}
impl fmt::Debug for Eventtype {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Eventtype")
.field("code", &self.0)
.field("name", &self.name())
.field("message", &self.message())
.finish()
}
}
pub type Eventrwflags = u16;
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct EventFdReadwrite {
/// The number of bytes available for reading or writing.
pub nbytes: Filesize,
/// The state of the file descriptor.
pub flags: Eventrwflags,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Event {
/// User-provided value that got attached to `subscription::userdata`.
pub userdata: Userdata,
/// If non-zero, an error that occurred while processing the subscription request.
pub error: Errno,
/// The type of event that occured
pub type_: Eventtype,
/// The contents of the event, if it is an `eventtype::fd_read` or
/// `eventtype::fd_write`. `eventtype::clock` events ignore this field.
pub fd_readwrite: EventFdReadwrite,
}
pub type Subclockflags = u16;
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct SubscriptionClock {
/// The clock against which to compare the timestamp.
pub id: Clockid,
/// The absolute or relative timestamp.
pub timeout: Timestamp,
/// The amount of time that the implementation may wait additionally
/// to coalesce with other events.
pub precision: Timestamp,
/// Flags specifying whether the timeout is absolute or relative
pub flags: Subclockflags,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct SubscriptionFdReadwrite {
/// The file descriptor on which to wait for it to become ready for reading or writing.
pub file_descriptor: Fd,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union SubscriptionUU {
pub clock: SubscriptionClock,
pub fd_read: SubscriptionFdReadwrite,
pub fd_write: SubscriptionFdReadwrite,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct SubscriptionU {
pub tag: u8,
pub u: SubscriptionUU,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Subscription {
/// User-provided value that is attached to the subscription in the
/// implementation and returned through `event::userdata`.
pub userdata: Userdata,
/// The type of the event to which to subscribe, and its contents
pub u: SubscriptionU,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct PrestatDir {
/// The length of the directory name for use with `fd_prestat_dir_name`.
pub pr_name_len: Size,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub union PrestatU {
pub dir: PrestatDir,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Prestat {
pub tag: u8,
pub u: PrestatU,
}
/// Get the attributes of a file descriptor.
/// Note: This returns similar flags to `fsync(fd, F_GETFL)` in POSIX, as well as additional fields.
///
/// ## Return
///
/// The buffer where the file descriptor's attributes are stored.
pub fn fd_fdstat_get(fd: Fd) -> Result<Fdstat, Errno> {
let mut rp0 = MaybeUninit::<Fdstat>::uninit();
unsafe {
let ret = wasi_snapshot_preview1::fd_fdstat_get(fd as i32, rp0.as_mut_ptr() as i32);
match ret {
0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Fdstat)),
_ => Err(Errno(ret as u16)),
}
}
}
/// Adjust the flags associated with a file descriptor.
/// Note: This is similar to `fcntl(fd, F_SETFL, flags)` in POSIX.
///
/// ## Parameters
///
/// * `flags` - The desired values of the file descriptor flags.
pub fn fd_fdstat_set_flags(fd: Fd, flags: Fdflags) -> Result<(), Errno> {
unsafe {
let ret = wasi_snapshot_preview1::fd_fdstat_set_flags(fd as i32, flags as i32);
match ret {
0 => Ok(()),
_ => Err(Errno(ret as u16)),
}
}
}
/// Concurrently poll for the occurrence of a set of events.
///
/// ## Parameters
///
/// * `in_` - The events to which to subscribe.
/// * `out` - The events that have occurred.
/// * `nsubscriptions` - Both the number of subscriptions and events.
///
/// ## Return
///
/// The number of events stored.
pub fn poll_oneoff(
in_: *const Subscription,
out: *mut Event,
nsubscriptions: Size,
) -> Result<Size, Errno> {
let mut rp0 = MaybeUninit::<Size>::uninit();
unsafe {
let ret = wasi_snapshot_preview1::poll_oneoff(
in_ as i32,
out as i32,
nsubscriptions as i32,
rp0.as_mut_ptr() as i32,
);
match ret {
0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size)),
_ => Err(Errno(ret as u16)),
}
}
}
pub mod wasi_snapshot_preview1 {
#[link(wasm_import_module = "wasi_snapshot_preview1")]
extern "C" {
/// Get the attributes of a file descriptor.
/// Note: This returns similar flags to `fsync(fd, F_GETFL)` in POSIX, as well as additional fields.
pub fn fd_fdstat_get(arg0: i32, arg1: i32) -> i32;
/// Adjust the flags associated with a file descriptor.
/// Note: This is similar to `fcntl(fd, F_SETFL, flags)` in POSIX.
pub fn fd_fdstat_set_flags(arg0: i32, arg1: i32) -> i32;
/// Concurrently poll for the occurrence of a set of events.
pub fn poll_oneoff(arg0: i32, arg1: i32, arg2: i32, arg3: i32) -> i32;
}
}