safa_abi/ffi/ptr.rs
1use crate::ffi::NotZeroable;
2
3/// Represents a non-null raw pointer, similar to [`core::ptr::NonNull`], but it instead doesn't guarantee that the pointer is valid,
4/// instead IT SHOULD BE valid, additional checks should be performed before using it.
5///
6/// The purpose is for use alongside [`crate::ffi::option::OptZero`].
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8#[repr(transparent)]
9pub struct FFINonNull<T: ?Sized>(pub(crate) *mut T);
10
11impl<T: ?Sized> FFINonNull<T> {
12 /// Creates a new instance of [`FFINonNull`] if the pointer is not null otherwise returns [`None`].
13 pub const fn new(ptr: *mut T) -> Option<Self> {
14 if ptr.is_null() { None } else { Some(Self(ptr)) }
15 }
16
17 /// Creates a new instance of [`FFINonNull`] without checking if the pointer is null.
18 /// # Safety
19 /// The caller must ensure that the pointer is not null.
20 pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
21 debug_assert!(
22 !ptr.is_null(),
23 "attempt to create an instance of FFINonNull with a null value"
24 );
25 Self(ptr)
26 }
27 /// Returns the raw pointer stored in this [`FFINonNull`] instance.
28 pub const fn as_ptr(&self) -> *mut T {
29 self.0
30 }
31}
32
33impl<T: ?Sized> NotZeroable for FFINonNull<T> {
34 fn is_zero(&self) -> bool {
35 self.0.is_null()
36 }
37}