Usage of std::sys
std::sys is the OS abstraction layer beneath the high-level module.
std(high-level)
-> sys dispatcher
-> sys/linux or sys/macos
-> syscall
Basic Conventions
- Most functions return raw syscall values.
>= 0success,< 0failure (-errno).- In high-level app code, prefer using
std::sys,std::net,std::timeoverstd::envwhenever possible.
1. File Reading Example (std::sys::fs)
import("std::sys::fs");
fun main() {
var fd: i64 = open("/etc/hosts", 0, 0);
if (fd < 0) {
return;
}
var buf: array<u8, 256>;
var n: i64 = read(fd, &buf[0], 256);
close(fd);
}
2. Socket Example (std::sys::socket)
import("std::sys::socket");
fun main() {
var fd: i64 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0) {
return;
}
shutdown(fd, SHUT_RDWR);
}
3. Memory Example (std::sys::memory)
import("std::sys::memory");
fun main() {
var p: ptr<u8> = sys_alloc(4096);
if (p == null) {
return;
}
sys_free(p, 4096);
}
Dispatcher Module
std::sys::socketstd::sys::fsstd::sys::envstd::sys::memorystd::sys::processstd::sys::timestd::sys::tty
#[target(os="linux")], #[target(os="macos")] branches are used only inside the dispatcher.
