1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

use core::convert::TryFrom;
use core::ptr::NonNull;

use crate::exception::Exception;

cfg_if::cfg_if! {
    if #[cfg(feature = "no-std")] {
        use alloc::format;
    } else {
        use std::format;
    }
}

#[inline]
pub fn not_null<T>(
    field: &str,
    ptr: Option<NonNull<T>>,
    exception: &crate::inout::OutPtr<Exception>,
) -> Option<NonNull<T>> {
    if ptr.is_none() {
        if let Some(exception) = exception.as_ptr() {
            unsafe {
                *exception.as_ptr() = Exception::try_from(&*format!("{} must not be null", field))
                    .unwrap()
                    .into_raw()
            };
        }
    }
    ptr
}

#[macro_export]
macro_rules! try_not_null {
    ($path:expr, $exception:expr) => {
        match $crate::macros::not_null(stringify!($path), $path, $exception) {
            Some(path) => path,
            None => return $crate::nullable::null(),
        }
    };

    ($path:expr, $exception:expr, $fallback:expr) => {
        match $crate::macros::not_null(stringify!($path), $path, $exception) {
            Some(v) => v,
            None => {
                return $fallback;
            }
        }
    };
}

#[macro_export]
macro_rules! try_as_ref {
    ($arc:expr, $exception:expr) => {
        match $arc.as_ref() {
            Some(r) => r,
            None => {
                return $crate::exception::throw_message(
                    &*format!("{} must not be null", stringify!($arc)),
                    $exception,
                );
            }
        }
    };
}

#[macro_export]
macro_rules! try_as_mut_ref {
    ($thing:expr, $exception:expr, $fallback:expr) => {
        match $thing.as_mut_ref() {
            Some(v) => v,
            None => {
                let _: $crate::nullable::Nullable<()> = $crate::exception::throw_message(
                    &*format!("{} must not be null", stringify!($thing)),
                    $exception,
                );
                return $fallback;
            }
        }
    };

    ($thing:expr, $exception:expr) => {
        match $thing.as_mut_ref() {
            Some(v) => v,
            None => {
                return $crate::exception::throw_message(
                    &*format!("{} must not be null", stringify!($thing)),
                    $exception,
                );
            }
        }
    };
}

#[macro_export]
macro_rules! try_into_arc {
    ($arc:expr, $exception:expr) => {
        match $arc {
            None => {
                return $crate::exception::throw_message(
                    &*format!("{} must not be null", stringify!($arc)),
                    $exception,
                );
            }
            Some(arc) => std::sync::Arc::from_raw(arc.as_ptr() as *const _),
        }
    };

    ($arc:expr, $exception:expr, $fallback:expr) => {
        match $arc {
            None => {
                let _: $crate::nullable::Nullable<()> = $crate::exception::throw_message(
                    &*format!("{} must not be null", stringify!($arc)),
                    $exception,
                );
                return $fallback;
            }
            Some(arc) => std::sync::Arc::from_raw(arc.as_ptr() as *const _),
        }
    };
}

#[macro_export]
macro_rules! try_as_arc {
    ($inout:expr, $exception:expr) => {
        match $inout.as_arc() {
            None => {
                return $crate::exception::throw_message(
                    &*format!("{} must not be null", stringify!($arc)),
                    $exception,
                );
            }
            Some(arc) => arc,
        }
    };

    ($inout:expr, $exception:expr, $fallback:expr) => {
        match $inout.as_arc() {
            None => {
                let _: $crate::nullable::Nullable<()> = $crate::exception::throw_message(
                    &*format!("{} must not be null", stringify!($arc)),
                    $exception,
                );
                return $fallback;
            }
            Some(arc) => arc,
        }
    };
}

#[macro_export]
macro_rules! try_as_str {
    ($ptr:expr, $exception:expr) => {
        match $crate::macros::not_null(stringify!($ptr), $ptr.as_ptr(), $exception) {
            Some(ptr) => match unsafe { std::ffi::CStr::from_ptr(ptr.as_ptr()).to_str() } {
                Ok(v) => v,
                Err(e) => return throw(e, $exception),
            },
            None => return $crate::nullable::null(),
        }
    };
}