opaque-pointer 0.10.1

Generic functions to work with opaque pointers when use FFI to expose Rust structs
Documentation
use crate::error::PointerError;
#[cfg(all(feature = "std", feature = "lender"))]
use crate::lender;

#[inline]
pub fn not_null_pointer<T>(pointer: *const T) -> Result<(), PointerError> {
    if pointer.is_null() {
        log::error!("Using a NULL pointer as an opaque pointer to Rust's data");
        return Err(PointerError::Null);
    }

    Ok(())
}

#[inline]
#[cfg(all(feature = "std", feature = "lender"))]
pub fn lent_pointer<T: 'static>(pointer: *const T) -> Result<(), PointerError> {
    match lender::lent_type_of(pointer) {
        Some(type_id) if type_id != std::any::TypeId::of::<T>() => {
            log::error!(
                "Using a pointer with a different type as an opaque pointer to Rust's data"
            );
            Err(PointerError::InvalidType)
        }
        None => {
            log::error!("Using an invalid pointer as an opaque pointer to Rust's data");
            Err(PointerError::Invalid)
        }
        _ => Ok(()),
    }
}