pyo3 0.30.1

Bindings to Python interpreter
Documentation
use core::ptr::NonNull;

use crate::ffi::{PY_SSIZE_T_MAX, Py_ssize_t};
use crate::platform::prelude::*;

#[cfg(wip_feature_std)]
macro_rules! pyo3_exception {
    ($doc: expr, $name: ident, $base: ty) => {
        #[doc = $doc]
        #[repr(transparent)]
        pub struct $name($crate::PyAny);

        $crate::impl_exception_boilerplate!($name);

        $crate::create_exception_type_object!(pyo3_runtime, $name, $base, Some($doc));
    };
}

/// Convert an usize index into a Py_ssize_t index, clamping overflow to
/// PY_SSIZE_T_MAX.
pub(crate) fn get_ssize_index(index: usize) -> Py_ssize_t {
    index.min(PY_SSIZE_T_MAX as usize) as Py_ssize_t
}

// TODO: use Box::into_non_null when stabilized
pub(crate) fn box_into_non_null<T>(b: Box<T>) -> NonNull<T> {
    NonNull::from(Box::leak(b))
}

/// Replacement for the unstable `<*mut [T; N]>::as_mut_ptr` method, which avoids
/// possibility of type getting lost from using e.g. `.cast()` to change array
/// type to point to the data.
pub(crate) const fn array_ptr_as_mut<T, const N: usize>(ptr: *mut [T; N]) -> *mut T {
    ptr.cast()
}