nstd_sys/os/unix/
io.rs

1//! Provides functionality for working with input & output on Unix platforms.
2pub(crate) mod stdio;
3use crate::{core::result::NSTDResult, NSTDUInt};
4use core::ffi::c_int;
5use libc::{
6    EACCES, EAGAIN, EBADF, ECONNRESET, EINTR, EINVAL, EISDIR, ENETDOWN, ENETUNREACH, ENOMEM,
7    ENOTCONN, EPIPE, ESPIPE, ETIMEDOUT, EWOULDBLOCK,
8};
9use nstdapi::nstdapi;
10
11/// An error type for Unix I/O operations.
12#[nstdapi]
13#[derive(Clone, Copy, PartialEq, Eq)]
14#[allow(non_camel_case_types)]
15pub enum NSTDUnixIOError {
16    /// No error occurred.
17    NSTD_UNIX_IO_ERROR_NONE,
18    /// An unknown error occurred.
19    NSTD_UNIX_IO_ERROR_UNKNOWN,
20    /// An entity, such as a file, was not found.
21    NSTD_UNIX_IO_ERROR_NOT_FOUND,
22    /// Permission was denied.
23    NSTD_UNIX_IO_ERROR_PERMISSION_DENIED,
24    /// The connection was reset by a remote server.
25    NSTD_UNIX_IO_ERROR_CONNECTION_RESET,
26    /// There is no connection.
27    NSTD_UNIX_IO_ERROR_NO_CONNECTION,
28    /// A seek operation failed because the file descriptor provided refers to a pipe, FIFO, or
29    /// socket object.
30    NSTD_UNIX_IO_ERROR_INVALID_SEEK,
31    /// The operation failed because a pipe was closed.
32    NSTD_UNIX_IO_ERROR_BROKEN_PIPE,
33    /// The operation needs to block to complete.
34    NSTD_UNIX_IO_ERROR_BLOCKING,
35    /// A pathname was expected to refer to a regular file, but a directory was found.
36    NSTD_UNIX_IO_ERROR_IS_DIR,
37    /// Some input parameter was incorrect.
38    NSTD_UNIX_IO_ERROR_INVALID_INPUT,
39    /// Some input/output data had an incorrect format.
40    NSTD_UNIX_IO_ERROR_INVALID_DATA,
41    /// The I/O operation's timeout expired, causing it to be canceled.
42    NSTD_UNIX_IO_ERROR_TIMED_OUT,
43    /// The operation was interrupted.
44    NSTD_UNIX_IO_ERROR_INTERRUPTED,
45    /// A reader unexpectedly reached the end of a file.
46    NSTD_UNIX_IO_ERROR_UNEXPECTED_EOF,
47    /// An operation could not be completed, because it failed to allocate enough memory.
48    NSTD_UNIX_IO_ERROR_OUT_OF_MEMORY,
49}
50impl NSTDUnixIOError {
51    /// Retrieves the last system error and turns it into an `NSTDUnixIOError`.
52    #[allow(unused)]
53    fn last() -> Self {
54        #[allow(trivial_numeric_casts, unreachable_patterns)]
55        match errno::errno().0 as c_int {
56            0 => Self::NSTD_UNIX_IO_ERROR_NONE,
57            EBADF => Self::NSTD_UNIX_IO_ERROR_NOT_FOUND,
58            EACCES => Self::NSTD_UNIX_IO_ERROR_PERMISSION_DENIED,
59            ECONNRESET => Self::NSTD_UNIX_IO_ERROR_CONNECTION_RESET,
60            ENETDOWN | ENETUNREACH | ENOTCONN => Self::NSTD_UNIX_IO_ERROR_NO_CONNECTION,
61            ESPIPE => Self::NSTD_UNIX_IO_ERROR_INVALID_SEEK,
62            EPIPE => Self::NSTD_UNIX_IO_ERROR_BROKEN_PIPE,
63            EAGAIN | EWOULDBLOCK => Self::NSTD_UNIX_IO_ERROR_BLOCKING,
64            EISDIR => Self::NSTD_UNIX_IO_ERROR_IS_DIR,
65            EINVAL => Self::NSTD_UNIX_IO_ERROR_INVALID_INPUT,
66            ETIMEDOUT => Self::NSTD_UNIX_IO_ERROR_TIMED_OUT,
67            EINTR => Self::NSTD_UNIX_IO_ERROR_INTERRUPTED,
68            ENOMEM => Self::NSTD_UNIX_IO_ERROR_OUT_OF_MEMORY,
69            _ => Self::NSTD_UNIX_IO_ERROR_UNKNOWN,
70        }
71    }
72}
73
74/// A result type that yields an [`NSTDUInt`] representing the number of bytes read or written by a
75/// Unix I/O operation on success and a Unix I/O operation error code on failure.
76pub type NSTDUnixIOResult = NSTDResult<NSTDUInt, NSTDUnixIOError>;
77
78/// Represents a raw Unix file descriptor.
79pub type NSTDUnixFileDescriptor = c_int;