You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
chacha20stream/src/ffi/error.rs

111 lines
1.6 KiB

//! FFI errors
use super::*;
//TODO: Rework the error handling/reporting here.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Copy)]
#[repr(C)]
pub enum CErr
{
Success = 0,
InvalidFile,
/// Unexpected null pointer
NullPointer,
/// Internal SSL error
SslError,
/// I/O error
IO,
Panic = -1,
}
impl CErr
{
#[inline] pub fn is_error(&self) -> bool
{
*self != Self::Success
}
#[inline] pub fn is_success(&self) -> bool
{
*self == Self::Success
}
}
impl Default for CErr
{
#[inline]
fn default() -> Self
{
Self::Success
}
}
impl From<CErr> for i32
{
fn from(from: CErr) -> Self
{
from as i32
}
}
impl<T> From<CErr> for *mut T
{
fn from(from: CErr) -> Self
{
if from.is_error() { ptr::null_mut() }
else { panic!("invalid conversion of successful operation to non-null output pointer") }
}
}
/// Null check a pointer. If it's not null, dereference it.
#[macro_export] macro_rules! nullchk {
(move $ptr:expr) => {
{
let ptr = $ptr;
if ptr.is_null() {
return From::from(CErr::NullPointer);
} else {
unsafe {
*ptr
}
}
}
};
(ref $ptr:expr) => {
{
let ptr = $ptr;
if ptr.is_null() {
return From::from(CErr::NullPointer);
} else {
unsafe {
& *ptr
}
}
}
};
(ref mut $ptr:expr) => {
{
let ptr = $ptr;
if ptr.is_null() {
return From::from(CErr::NullPointer);
} else {
unsafe {
&mut *ptr
}
}
}
};
($ptr:expr) => {
{
let ptr = $ptr;
if ptr.is_null() {
return From::from(CErr::NullPointer);
} else {
ptr
}
}
}
}