/
none.rs
88 lines (72 loc) · 2.02 KB
/
none.rs
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
use crate::{ffi, IntoPy, PyAny, PyObject, PyTypeInfo, Python, ToPyObject};
/// Represents the Python `None` object.
#[repr(transparent)]
pub struct PyNone(PyAny);
pyobject_native_type_named!(PyNone);
pyobject_native_type_extract!(PyNone);
impl PyNone {
/// Returns the `None` object.
#[inline]
pub fn get(py: Python<'_>) -> &PyNone {
unsafe { py.from_borrowed_ptr(ffi::Py_None()) }
}
}
unsafe impl PyTypeInfo for PyNone {
const NAME: &'static str = "NoneType";
const MODULE: Option<&'static str> = None;
type AsRefTarget = PyNone;
fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
unsafe { ffi::Py_TYPE(ffi::Py_None()) }
}
#[inline]
fn is_type_of(object: &PyAny) -> bool {
// NoneType is not usable as a base type
Self::is_exact_type_of(object)
}
#[inline]
fn is_exact_type_of(object: &PyAny) -> bool {
object.is_none()
}
}
/// `()` is converted to Python `None`.
impl ToPyObject for () {
fn to_object(&self, py: Python<'_>) -> PyObject {
PyNone::get(py).into()
}
}
impl IntoPy<PyObject> for () {
#[inline]
fn into_py(self, py: Python<'_>) -> PyObject {
PyNone::get(py).into()
}
}
#[cfg(test)]
mod tests {
use crate::types::{PyDict, PyNone};
use crate::{IntoPy, PyObject, Python, ToPyObject};
#[test]
fn test_none_is_none() {
Python::with_gil(|py| {
assert!(PyNone::get(py).downcast::<PyNone>().unwrap().is_none());
})
}
#[test]
fn test_unit_to_object_is_none() {
Python::with_gil(|py| {
assert!(().to_object(py).downcast::<PyNone>(py).is_ok());
})
}
#[test]
fn test_unit_into_py_is_none() {
Python::with_gil(|py| {
let obj: PyObject = ().into_py(py);
assert!(obj.downcast::<PyNone>(py).is_ok());
})
}
#[test]
fn test_dict_is_not_none() {
Python::with_gil(|py| {
assert!(PyDict::new(py).downcast::<PyNone>().is_err());
})
}
}