11a0216d1Sopenharmony_ci//! This is just a sample of what FFI using this crate can look like.
21a0216d1Sopenharmony_ci
31a0216d1Sopenharmony_ci#![cfg_attr(not(io_safety_is_in_std), allow(unused_imports))]
41a0216d1Sopenharmony_ci#![allow(missing_docs)]
51a0216d1Sopenharmony_ci
61a0216d1Sopenharmony_ci#[cfg(any(unix, target_os = "wasi"))]
71a0216d1Sopenharmony_ciuse crate::{BorrowedFd, OwnedFd};
81a0216d1Sopenharmony_ci#[cfg(windows)]
91a0216d1Sopenharmony_ciuse crate::{BorrowedHandle, HandleOrInvalid};
101a0216d1Sopenharmony_ci
111a0216d1Sopenharmony_ci#[cfg(any(unix, target_os = "wasi"))]
121a0216d1Sopenharmony_ciuse libc::{c_char, c_int, c_void, size_t, ssize_t};
131a0216d1Sopenharmony_ci#[cfg(windows)]
141a0216d1Sopenharmony_ciuse {
151a0216d1Sopenharmony_ci    core::ffi::c_void,
161a0216d1Sopenharmony_ci    windows_sys::core::PCWSTR,
171a0216d1Sopenharmony_ci    windows_sys::Win32::Foundation::BOOL,
181a0216d1Sopenharmony_ci    windows_sys::Win32::Security::SECURITY_ATTRIBUTES,
191a0216d1Sopenharmony_ci    windows_sys::Win32::Storage::FileSystem::{
201a0216d1Sopenharmony_ci        FILE_ACCESS_FLAGS, FILE_CREATION_DISPOSITION, FILE_FLAGS_AND_ATTRIBUTES, FILE_SHARE_MODE,
211a0216d1Sopenharmony_ci    },
221a0216d1Sopenharmony_ci    windows_sys::Win32::System::IO::OVERLAPPED,
231a0216d1Sopenharmony_ci};
241a0216d1Sopenharmony_ci
251a0216d1Sopenharmony_ci// Declare a few FFI functions ourselves, to show off the FFI ergonomics.
261a0216d1Sopenharmony_ci#[cfg(all(io_safety_is_in_std, any(unix, target_os = "wasi")))]
271a0216d1Sopenharmony_ciextern "C" {
281a0216d1Sopenharmony_ci    pub fn open(pathname: *const c_char, flags: c_int, ...) -> Option<OwnedFd>;
291a0216d1Sopenharmony_ci}
301a0216d1Sopenharmony_ci#[cfg(any(unix, target_os = "wasi"))]
311a0216d1Sopenharmony_ciextern "C" {
321a0216d1Sopenharmony_ci    pub fn read(fd: BorrowedFd<'_>, ptr: *mut c_void, size: size_t) -> ssize_t;
331a0216d1Sopenharmony_ci    pub fn write(fd: BorrowedFd<'_>, ptr: *const c_void, size: size_t) -> ssize_t;
341a0216d1Sopenharmony_ci}
351a0216d1Sopenharmony_ci#[cfg(any(unix, target_os = "wasi"))]
361a0216d1Sopenharmony_cipub use libc::{O_CLOEXEC, O_CREAT, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY};
371a0216d1Sopenharmony_ci
381a0216d1Sopenharmony_ci// The Windows analogs of the above. Note the use of [`HandleOrInvalid`] as
391a0216d1Sopenharmony_ci// the return type for `CreateFileW`, since that function is defined to return
401a0216d1Sopenharmony_ci// [`INVALID_HANDLE_VALUE`] on error instead of null.
411a0216d1Sopenharmony_ci#[cfg(windows)]
421a0216d1Sopenharmony_ciextern "system" {
431a0216d1Sopenharmony_ci    pub fn CreateFileW(
441a0216d1Sopenharmony_ci        lpfilename: PCWSTR,
451a0216d1Sopenharmony_ci        dwdesiredaccess: FILE_ACCESS_FLAGS,
461a0216d1Sopenharmony_ci        dwsharemode: FILE_SHARE_MODE,
471a0216d1Sopenharmony_ci        lpsecurityattributes: *const SECURITY_ATTRIBUTES,
481a0216d1Sopenharmony_ci        dwcreationdisposition: FILE_CREATION_DISPOSITION,
491a0216d1Sopenharmony_ci        dwflagsandattributes: FILE_FLAGS_AND_ATTRIBUTES,
501a0216d1Sopenharmony_ci        htemplatefile: HANDLE,
511a0216d1Sopenharmony_ci    ) -> HandleOrInvalid;
521a0216d1Sopenharmony_ci    pub fn ReadFile(
531a0216d1Sopenharmony_ci        hfile: BorrowedHandle<'_>,
541a0216d1Sopenharmony_ci        lpbuffer: *mut c_void,
551a0216d1Sopenharmony_ci        nnumberofbytestoread: u32,
561a0216d1Sopenharmony_ci        lpnumberofbytesread: *mut u32,
571a0216d1Sopenharmony_ci        lpoverlapped: *mut OVERLAPPED,
581a0216d1Sopenharmony_ci    ) -> BOOL;
591a0216d1Sopenharmony_ci    pub fn WriteFile(
601a0216d1Sopenharmony_ci        hfile: BorrowedHandle<'_>,
611a0216d1Sopenharmony_ci        lpbuffer: *const c_void,
621a0216d1Sopenharmony_ci        nnumberofbytestowrite: u32,
631a0216d1Sopenharmony_ci        lpnumberofbyteswritten: *mut u32,
641a0216d1Sopenharmony_ci        lpoverlapped: *mut OVERLAPPED,
651a0216d1Sopenharmony_ci    ) -> BOOL;
661a0216d1Sopenharmony_ci}
671a0216d1Sopenharmony_ci
681a0216d1Sopenharmony_ci#[cfg(windows)]
691a0216d1Sopenharmony_cipub use {
701a0216d1Sopenharmony_ci    windows_sys::Win32::Foundation::HANDLE,
711a0216d1Sopenharmony_ci    windows_sys::Win32::Storage::FileSystem::{
721a0216d1Sopenharmony_ci        CREATE_ALWAYS, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, FILE_GENERIC_READ, FILE_GENERIC_WRITE,
731a0216d1Sopenharmony_ci        OPEN_EXISTING,
741a0216d1Sopenharmony_ci    },
751a0216d1Sopenharmony_ci};
76