Remove options and rename struct
This commit is contained in:
parent
dadbc22b2e
commit
718b0fd02f
|
@ -74,8 +74,8 @@ pub unsafe extern "C" fn PyInit_string_sum() -> *mut PyObject {
|
||||||
|
|
||||||
let wrapped_sum_as_string = PyMethodDef {
|
let wrapped_sum_as_string = PyMethodDef {
|
||||||
ml_name: "sum_as_string\0".as_ptr() as *const c_char,
|
ml_name: "sum_as_string\0".as_ptr() as *const c_char,
|
||||||
ml_meth: MlMeth {
|
ml_meth: PyMethodDefPointer {
|
||||||
_PyCFunctionFast: Some(sum_as_string)
|
_PyCFunctionFast: sum_as_string
|
||||||
},
|
},
|
||||||
ml_flags: METH_FASTCALL,
|
ml_flags: METH_FASTCALL,
|
||||||
ml_doc: "returns the sum of two integers as a string\0".as_ptr() as *const c_char,
|
ml_doc: "returns the sum of two integers as a string\0".as_ptr() as *const c_char,
|
||||||
|
|
|
@ -113,8 +113,8 @@
|
||||||
//!
|
//!
|
||||||
//! let wrapped_sum_as_string = PyMethodDef {
|
//! let wrapped_sum_as_string = PyMethodDef {
|
||||||
//! ml_name: "sum_as_string\0".as_ptr() as *const c_char,
|
//! ml_name: "sum_as_string\0".as_ptr() as *const c_char,
|
||||||
//! ml_meth: MlMeth {
|
//! ml_meth: PyMethodDefPointer {
|
||||||
//! _PyCFunctionFast: Some(sum_as_string)
|
//! _PyCFunctionFast: sum_as_string
|
||||||
//! },
|
//! },
|
||||||
//! ml_flags: METH_FASTCALL,
|
//! ml_flags: METH_FASTCALL,
|
||||||
//! ml_doc: "returns the sum of two integers as a string\0".as_ptr() as *const c_char,
|
//! ml_doc: "returns the sum of two integers as a string\0".as_ptr() as *const c_char,
|
||||||
|
|
|
@ -78,14 +78,14 @@ extern "C" {
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct PyMethodDef {
|
pub struct PyMethodDef {
|
||||||
pub ml_name: *const c_char,
|
pub ml_name: *const c_char,
|
||||||
pub ml_meth: MlMeth,
|
pub ml_meth: PyMethodDefPointer,
|
||||||
pub ml_flags: c_int,
|
pub ml_flags: c_int,
|
||||||
pub ml_doc: *const c_char,
|
pub ml_doc: *const c_char,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Function types used to implement Python callables.
|
/// Function types used to implement Python callables.
|
||||||
///
|
///
|
||||||
/// This union must be accompanied by the correct [ml_flags](PyMethodDef::ml_flags),
|
/// This function pointer must be accompanied by the correct [ml_flags](PyMethodDef::ml_flags),
|
||||||
/// otherwise the behavior is undefined.
|
/// otherwise the behavior is undefined.
|
||||||
///
|
///
|
||||||
/// See the [Python C API documentation][1] for more information.
|
/// See the [Python C API documentation][1] for more information.
|
||||||
|
@ -93,34 +93,29 @@ pub struct PyMethodDef {
|
||||||
/// [1]: https://docs.python.org/3/c-api/structures.html#implementing-functions-and-methods
|
/// [1]: https://docs.python.org/3/c-api/structures.html#implementing-functions-and-methods
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub union MlMeth {
|
pub union PyMethodDefPointer {
|
||||||
/// This variant corresponds with [`METH_VARARGS`] *or* [`METH_NOARGS`] *or* [`METH_O`].
|
/// This variant corresponds with [`METH_VARARGS`] *or* [`METH_NOARGS`] *or* [`METH_O`].
|
||||||
pub PyCFunction: Option<PyCFunction>,
|
pub PyCFunction: PyCFunction,
|
||||||
|
|
||||||
/// This variant corresponds with [`METH_VARARGS`] | [`METH_KEYWORDS`].
|
/// This variant corresponds with [`METH_VARARGS`] | [`METH_KEYWORDS`].
|
||||||
pub PyCFunctionWithKeywords: Option<PyCFunctionWithKeywords>,
|
pub PyCFunctionWithKeywords: PyCFunctionWithKeywords,
|
||||||
|
|
||||||
/// This variant corresponds with [`METH_FASTCALL`].
|
/// This variant corresponds with [`METH_FASTCALL`].
|
||||||
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
|
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
|
||||||
pub _PyCFunctionFast: Option<_PyCFunctionFast>,
|
pub _PyCFunctionFast: _PyCFunctionFast,
|
||||||
|
|
||||||
/// This variant corresponds with [`METH_FASTCALL`] | [`METH_KEYWORDS`].
|
/// This variant corresponds with [`METH_FASTCALL`] | [`METH_KEYWORDS`].
|
||||||
#[cfg(not(Py_LIMITED_API))]
|
#[cfg(not(Py_LIMITED_API))]
|
||||||
pub _PyCFunctionFastWithKeywords: Option<_PyCFunctionFastWithKeywords>,
|
pub _PyCFunctionFastWithKeywords: _PyCFunctionFastWithKeywords,
|
||||||
|
|
||||||
/// This variant corresponds with [`METH_METHOD`] | [`METH_FASTCALL`] | [`METH_KEYWORDS`].
|
/// This variant corresponds with [`METH_METHOD`] | [`METH_FASTCALL`] | [`METH_KEYWORDS`].
|
||||||
#[cfg(all(Py_3_9, not(Py_LIMITED_API)))]
|
#[cfg(all(Py_3_9, not(Py_LIMITED_API)))]
|
||||||
pub PyCMethod: Option<PyCMethod>,
|
pub PyCMethod: PyCMethod,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: This can be a const assert on Rust 1.57
|
// TODO: This can be a const assert on Rust 1.57
|
||||||
const _: () = [()][mem::size_of::<MlMeth>() - mem::size_of::<Option<extern "C" fn()>>()];
|
const _: () =
|
||||||
|
[()][mem::size_of::<PyMethodDefPointer>() - mem::size_of::<Option<extern "C" fn()>>()];
|
||||||
impl Default for PyMethodDef {
|
|
||||||
fn default() -> PyMethodDef {
|
|
||||||
unsafe { mem::zeroed() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#[cfg_attr(PyPy, link_name = "PyPyCFunction_New")]
|
#[cfg_attr(PyPy, link_name = "PyPyCFunction_New")]
|
||||||
|
|
|
@ -163,15 +163,15 @@ impl PyMethodDef {
|
||||||
/// Convert `PyMethodDef` to Python method definition struct `ffi::PyMethodDef`
|
/// Convert `PyMethodDef` to Python method definition struct `ffi::PyMethodDef`
|
||||||
pub(crate) fn as_method_def(&self) -> Result<ffi::PyMethodDef, NulByteInString> {
|
pub(crate) fn as_method_def(&self) -> Result<ffi::PyMethodDef, NulByteInString> {
|
||||||
let meth = match self.ml_meth {
|
let meth = match self.ml_meth {
|
||||||
PyMethodType::PyCFunction(meth) => ffi::MlMeth {
|
PyMethodType::PyCFunction(meth) => ffi::PyMethodDefPointer {
|
||||||
PyCFunction: Some(meth.0),
|
PyCFunction: meth.0,
|
||||||
},
|
},
|
||||||
PyMethodType::PyCFunctionWithKeywords(meth) => ffi::MlMeth {
|
PyMethodType::PyCFunctionWithKeywords(meth) => ffi::PyMethodDefPointer {
|
||||||
PyCFunctionWithKeywords: Some(meth.0),
|
PyCFunctionWithKeywords: meth.0,
|
||||||
},
|
},
|
||||||
#[cfg(not(Py_LIMITED_API))]
|
#[cfg(not(Py_LIMITED_API))]
|
||||||
PyMethodType::PyCFunctionFastWithKeywords(meth) => ffi::MlMeth {
|
PyMethodType::PyCFunctionFastWithKeywords(meth) => ffi::PyMethodDefPointer {
|
||||||
_PyCFunctionFastWithKeywords: Some(meth.0),
|
_PyCFunctionFastWithKeywords: meth.0,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue