pub mod io;
pub mod ops;
pub mod resource;
use crate::{api::resource::AsResource, buf::BufLike};
use io::Io;
use std::{ffi::CString, net::SocketAddr, time::Duration};
#[cfg(unix)]
use std::os::fd::RawFd;
#[cfg(windows)]
use std::os::windows::io::RawHandle;
doc_op! {
short: "A no-op that completes immediately. Useful for waking up the event loop or testing.",
pub fn nop() -> Io<ops::Nop> {
Io::from_op(ops::Nop)
}
}
doc_op! {
short: "Closes a raw file descriptor.",
syscall: "close(2)",
#[cfg(unix)]
pub fn close(fd: RawFd) -> Io<ops::Close> {
Io::from_op(ops::Close::new(fd))
}
}
#[cfg(windows)]
pub fn close(handle: RawHandle, is_socket: bool) -> Io<ops::Close> {
Io::from_op(ops::Close::new(handle, is_socket))
}
doc_op! {
short: "Shuts down the read, write, or both halves of a socket connection.",
syscall: "shutdown(2)",
doc_link: "https://man7.org/linux/man-pages/man2/shutdown.2.html",
pub fn shutdown(res: &impl AsResource, how: i32) -> Io<ops::Shutdown> {
Io::from_op(ops::Shutdown::new(res.as_resource().clone(), how))
}
}
doc_op! {
short: "Issues a timeout that returns after duration seconds.",
pub fn timeout(duration: Duration) -> Io<ops::Timeout> {
Io::from_op(ops::Timeout::new(duration))
}
}
doc_op!(
short: "Create a symlink.",
syscall: "symlinkat(2)",
doc_link: "https://man7.org/linux/man-pages/man2/symlink.2.html",
pub fn symlinkat(dir_res: &impl AsResource, target: CString, linkpath: CString) -> Io<ops::SymlinkAt> {
Io::from_op(ops::SymlinkAt::new(dir_res.as_resource().clone(), target, linkpath))
}
);
doc_op!(
short: "Create a hard-link.",
syscall: "linkat(2)",
doc_link: "https://man7.org/linux/man-pages/man2/linkat.2.html",
pub fn linkat(old_dir_res: &impl AsResource, old_path: CString, new_dir_res: impl AsResource, linkpath: CString) -> Io<ops::LinkAt> {
Io::from_op(ops::LinkAt::new(old_dir_res.as_resource().clone(), old_path, new_dir_res.as_resource().clone(), linkpath))
}
);
doc_op! {
short: "Synchronizes file data to storage.",
syscall: "fsync(2)",
pub fn fsync(res: &impl AsResource) -> Io<ops::Fsync> {
Io::from_op(ops::Fsync::new(res.as_resource().clone()))
}
}
doc_op! {
short: "Writes data from buffer to file descriptor.",
syscall: "write(2)",
doc_link: "https://man7.org/linux/man-pages/man2/write.2.html",
pub fn write<B>(res: &impl AsResource, buf: B) -> Io<ops::Write<B>>
where
B: BufLike + std::marker::Send + Sync
{
Io::from_op(ops::Write::new(res.as_resource().clone(), buf))
}
}
doc_op! {
short: "Writes data from buffer to file descriptor at a specific offset.",
syscall: "pwrite(2)",
doc_link: "https://man7.org/linux/man-pages/man2/pwrite.2.html",
pub fn write_at<B>(res: &impl AsResource, buf: B, offset: i64) -> Io<ops::WriteAt<B>>
where
B: BufLike + std::marker::Send + Sync
{
Io::from_op(ops::WriteAt::new(res.as_resource().clone(), buf, offset))
}
}
doc_op! {
short: "Reads resource into provided buffer.",
syscall: "read(2)",
doc_link: "https://man7.org/linux/man-pages/man2/read.2.html",
pub fn read<B>(res: &impl AsResource, mem: B) -> Io<ops::Read<B>>
where
B: BufLike + std::marker::Send + Sync
{
Io::from_op(ops::Read::new(res.as_resource().clone(), mem))
}
}
doc_op! {
short: "Reads resource into provided buffer with a offset.",
syscall: "pread(2)",
doc_link: "https://man7.org/linux/man-pages/man2/pwrite.2.html",
pub fn read_at<B>(res: &impl AsResource, mem: B, offset: i64) -> Io<ops::ReadAt<B>>
where
B: BufLike + std::marker::Send + Sync
{
Io::from_op(ops::ReadAt::new(res.as_resource().clone(), mem, offset))
}
}
doc_op! {
short: "Truncates a file to a specified length.",
syscall: "ftruncate(2)",
pub fn truncate(res: &impl AsResource, len: u64) -> Io<ops::Truncate> {
Io::from_op(ops::Truncate::new(res.as_resource().clone(), len))
}
}
doc_op! {
short: "Creates a new socket with the specified domain, type, and protocol.",
syscall: "socket(2)",
pub fn socket(domain: libc::c_int, ty: libc::c_int, proto: libc::c_int) -> Io<ops::Socket> {
Io::from_op(ops::Socket::new(domain, ty, proto))
}
}
doc_op! {
short: "Binds a socket to a specific address.",
syscall: "bind(2)",
pub fn bind(resource: &impl AsResource, addr: SocketAddr) -> Io<ops::Bind> {
Io::from_op(ops::Bind::new(resource.as_resource().clone(), addr))
}
}
doc_op! {
short: "Accepts a connection on a listening socket.",
syscall: "accept(2)",
pub fn accept(res: &impl AsResource) -> Io<ops::Accept> {
Io::from_op(ops::Accept::new(res.as_resource().clone()))
}
}
doc_op! {
short: "Accepts a connection on a Unix domain socket.",
syscall: "accept(2)",
pub fn accept_unix(res: &impl AsResource) -> Io<ops::AcceptUnix> {
Io::from_op(ops::AcceptUnix::new(res.as_resource().clone()))
}
}
doc_op! {
short: "Marks a socket as listening for incoming connections.",
syscall: "listen(2)",
pub fn listen(res: &impl AsResource, backlog: i32) -> Io<ops::Listen> {
Io::from_op(ops::Listen::new(res.as_resource().clone(), backlog))
}
}
doc_op! {
short: "Connects a socket to a remote address.",
syscall: "connect(2)",
pub fn connect(res: &impl AsResource, addr: SocketAddr) -> Io<ops::Connect> {
Io::from_op(ops::Connect::new(res.as_resource().clone(), addr))
}
}
doc_op! {
short: "Sends data on a connected socket.",
syscall: "send(2)",
pub fn send<B>(res: &impl AsResource, buf: B, flags: Option<i32>) -> Io<ops::Send<B>>
where
B: BufLike + std::marker::Send + Sync
{
Io::from_op(ops::Send::new(res.as_resource().clone(), buf, flags))
}
}
doc_op! {
short: "Receives data over a socket into provided buffer.",
syscall: "recv(2)",
doc_link: "https://man7.org/linux/man-pages/man2/recv.2.html",
pub fn recv<B>(res: &impl AsResource, buf: B, flags: Option<i32>) -> Io<ops::Recv<B>>
where
B: BufLike + std::marker::Send + Sync
{
Io::from_op(ops::Recv::new(res.as_resource().clone(), buf, flags))
}
}
doc_op! {
short: "Opens a file relative to a directory file descriptor.",
syscall: "openat(2)",
pub fn openat(dir_res: &impl AsResource, path: CString, flags: i32) -> Io<ops::OpenAt> {
Io::from_op(ops::OpenAt::new(dir_res.as_resource().clone(), path, flags))
}
}
doc_op! {
short: "Copies data between file descriptors without copying to userspace (Linux only).",
syscall: "tee(2)",
#[cfg(linux)]
#[cfg_attr(docsrs, doc(cfg(linux)))]
pub fn tee(res_in: &impl AsResource, res_out: impl AsResource, size: u32) -> Io<ops::Tee> {
Io::from_op(ops::Tee::new(res_in.as_resource().clone(), res_out.as_resource().clone(), size))
}
}