oscall 0.1.0

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
//! The core types: errors and handles.

mod common;

use oscall::{AsHandle, Error, ErrorKind, OwnedHandle};

#[test]
fn error() {
    let err = Error::from(ErrorKind::NotFound);
    assert_eq!(err.kind(), ErrorKind::NotFound);
    assert_eq!(err, ErrorKind::NotFound);
    assert_eq!(Error::from_raw_os_error(err.raw_os_error()), err);
    assert!(err.to_string().starts_with("entity not found (os error "));

    // The code is the system's own: std reads it the same way.
    let io = std::io::Error::from_raw_os_error(err.raw_os_error());
    assert_eq!(io.kind(), std::io::ErrorKind::NotFound);
}

#[test]
fn handle() {
    let dir = common::tmp_dir("core-handle");
    let file = std::fs::File::create(dir.join("file")).unwrap();
    let borrowed = common::handle_of(&file);
    assert_eq!(borrowed.as_handle().as_raw(), borrowed.as_raw());

    // An owned handle closes what it owns, and only that.
    #[cfg(windows)]
    let raw = std::os::windows::io::IntoRawHandle::into_raw_handle(file);
    #[cfg(not(windows))]
    let raw = std::os::fd::IntoRawFd::into_raw_fd(file);
    // SAFETY: `raw` is open and its ownership was just released by std.
    let owned = unsafe { OwnedHandle::from_raw(raw) };
    assert_eq!(owned.as_raw(), raw);
    assert_eq!(owned.as_handle().as_raw(), raw);
    let raw = owned.into_raw();
    // SAFETY: as above, ownership came back from `into_raw`.
    drop(unsafe { OwnedHandle::from_raw(raw) });
    std::fs::remove_file(dir.join("file")).unwrap();
}