use crate::{capi::error as capi_error, error::Error, Handle, Root};
use std::{
fs::File,
os::unix::io::{IntoRawFd, OwnedFd},
};
use libc::c_int;
pub(super) type CReturn = c_int;
pub(super) trait IntoCReturn {
fn into_c_return(self) -> CReturn;
}
impl IntoCReturn for () {
fn into_c_return(self) -> CReturn {
0
}
}
impl IntoCReturn for CReturn {
fn into_c_return(self) -> CReturn {
self
}
}
impl IntoCReturn for isize {
fn into_c_return(self) -> CReturn {
self as _
}
}
impl IntoCReturn for OwnedFd {
fn into_c_return(self) -> CReturn {
self.into_raw_fd()
}
}
impl IntoCReturn for Root {
fn into_c_return(self) -> CReturn {
OwnedFd::from(self).into_c_return()
}
}
impl IntoCReturn for Handle {
fn into_c_return(self) -> CReturn {
OwnedFd::from(self).into_c_return()
}
}
impl IntoCReturn for File {
fn into_c_return(self) -> CReturn {
OwnedFd::from(self).into_c_return()
}
}
impl<V> IntoCReturn for Result<V, Error>
where
V: IntoCReturn,
{
fn into_c_return(self) -> CReturn {
match self {
Ok(ok) => ok.into_c_return(),
Err(err) => capi_error::store_error(err),
}
}
}