helenos_ui/
lib.rs

1#![allow(non_camel_case_types)]
2#![allow(non_upper_case_globals)]
3#![doc = include_str!("../README.md")]
4
5#[cfg(target_os = "helenos")]
6pub use libc::{errno_t, sysarg_t};
7
8#[cfg(target_os = "helenos")]
9include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
10
11pub mod util {
12    #[cfg(target_os = "helenos")]
13    use super::errno_t;
14    #[cfg(not(target_os = "helenos"))]
15    type errno_t = i32;
16
17    use std::convert::Infallible;
18
19    pub trait IntoError {
20        type Error: std::error::Error;
21        fn into_error(self) -> Result<(), Self::Error>;
22    }
23
24    impl IntoError for errno_t {
25        type Error = std::io::Error;
26
27        fn into_error(self) -> Result<(), std::io::Error> {
28            if self == 0 {
29                Ok(())
30            } else {
31                Err(std::io::Error::from_raw_os_error(self))
32            }
33        }
34    }
35
36    impl IntoError for () {
37        type Error = Infallible;
38
39        fn into_error(self) -> Result<(), Infallible> {
40            Ok(())
41        }
42    }
43
44    pub fn pointer_init<T, E: IntoError, F: FnOnce(*mut T) -> E>(fun: F) -> Result<T, E::Error> {
45        let mut uninit = std::mem::MaybeUninit::uninit();
46        fun(uninit.as_mut_ptr()).into_error()?;
47        Ok(unsafe { uninit.assume_init() })
48    }
49}