1use crate::{
2 ffi, ffi_ptr_ext::FfiPtrExt, types::any::PyAnyMethods, Borrowed, Bound, PyAny, PyTypeInfo,
3 Python,
4};
5
6#[repr(transparent)]
11pub struct PyEllipsis(PyAny);
12
13pyobject_native_type_named!(PyEllipsis);
14
15impl PyEllipsis {
16 #[inline]
18 pub fn get(py: Python<'_>) -> Borrowed<'_, '_, PyEllipsis> {
19 unsafe {
21 ffi::Py_Ellipsis()
22 .assume_borrowed_unchecked(py)
23 .cast_unchecked()
24 }
25 }
26}
27
28unsafe impl PyTypeInfo for PyEllipsis {
29 const NAME: &'static str = "ellipsis";
30
31 const MODULE: Option<&'static str> = None;
32
33 fn type_object_raw(_py: Python<'_>) -> *mut ffi::PyTypeObject {
34 unsafe { ffi::Py_TYPE(ffi::Py_Ellipsis()) }
35 }
36
37 #[inline]
38 fn is_type_of(object: &Bound<'_, PyAny>) -> bool {
39 Self::is_exact_type_of(object)
41 }
42
43 #[inline]
44 fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool {
45 object.is(&**Self::get(object.py()))
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use crate::types::any::PyAnyMethods;
52 use crate::types::{PyDict, PyEllipsis};
53 use crate::{PyTypeInfo, Python};
54
55 #[test]
56 fn test_ellipsis_is_itself() {
57 Python::attach(|py| {
58 assert!(PyEllipsis::get(py).is_instance_of::<PyEllipsis>());
59 assert!(PyEllipsis::get(py).is_exact_instance_of::<PyEllipsis>());
60 })
61 }
62
63 #[test]
64 fn test_ellipsis_type_object_consistent() {
65 Python::attach(|py| {
66 assert!(PyEllipsis::get(py)
67 .get_type()
68 .is(PyEllipsis::type_object(py)));
69 })
70 }
71
72 #[test]
73 fn test_dict_is_not_ellipsis() {
74 Python::attach(|py| {
75 assert!(PyDict::new(py).cast::<PyEllipsis>().is_err());
76 })
77 }
78}