pyo3_stub_gen/stub_type/
builtins.rs1use crate::stub_type::*;
4use std::{
5 borrow::Cow,
6 ffi::{OsStr, OsString},
7 path::PathBuf,
8 rc::Rc,
9 sync::Arc,
10 time::SystemTime,
11};
12
13use chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
14
15macro_rules! impl_builtin {
16 ($ty:ty, $pytype:expr) => {
17 impl PyStubType for $ty {
18 fn type_output() -> TypeInfo {
19 TypeInfo::builtin($pytype)
20 }
21 }
22 };
23}
24
25macro_rules! impl_with_module {
26 ($ty:ty, $pytype:expr, $module:expr) => {
27 impl PyStubType for $ty {
28 fn type_output() -> TypeInfo {
29 TypeInfo::with_module($pytype, $module.into())
30 }
31 }
32 };
33}
34
35impl PyStubType for () {
37 fn type_output() -> TypeInfo {
38 TypeInfo::none()
39 }
40}
41impl_builtin!(bool, "bool");
42impl_builtin!(u8, "int");
43impl_builtin!(u16, "int");
44impl_builtin!(u32, "int");
45impl_builtin!(u64, "int");
46impl_builtin!(u128, "int");
47impl_builtin!(usize, "int");
48impl_builtin!(i8, "int");
49impl_builtin!(i16, "int");
50impl_builtin!(i32, "int");
51impl_builtin!(i64, "int");
52impl_builtin!(i128, "int");
53impl_builtin!(isize, "int");
54impl_builtin!(f32, "float");
55impl_builtin!(f64, "float");
56impl_builtin!(num_complex::Complex32, "complex");
57impl_builtin!(num_complex::Complex64, "complex");
58
59impl_builtin!(char, "str");
60impl_builtin!(&str, "str");
61impl_builtin!(OsStr, "str");
62impl_builtin!(String, "str");
63impl_builtin!(OsString, "str");
64impl_builtin!(Cow<'_, str>, "str");
65impl_builtin!(Cow<'_, OsStr>, "str");
66impl_builtin!(Cow<'_, [u8]>, "bytes");
67
68impl PyStubType for PathBuf {
69 fn type_output() -> TypeInfo {
70 TypeInfo::with_module("pathlib.Path", "pathlib".into())
71 }
72 fn type_input() -> TypeInfo {
73 TypeInfo::builtin("str")
74 | TypeInfo::with_module("os.PathLike", "os".into())
75 | TypeInfo::with_module("pathlib.Path", "pathlib".into())
76 }
77}
78
79impl<Tz: TimeZone> PyStubType for DateTime<Tz> {
80 fn type_output() -> TypeInfo {
81 TypeInfo::with_module("datetime.datetime", "datetime".into())
82 }
83}
84
85impl_with_module!(SystemTime, "datetime.datetime", "datetime");
86impl_with_module!(NaiveDateTime, "datetime.datetime", "datetime");
87impl_with_module!(NaiveDate, "datetime.date", "datetime");
88impl_with_module!(NaiveTime, "datetime.time", "datetime");
89impl_with_module!(FixedOffset, "datetime.tzinfo", "datetime");
90impl_with_module!(Utc, "datetime.tzinfo", "datetime");
91impl_with_module!(std::time::Duration, "datetime.timedelta", "datetime");
92impl_with_module!(chrono::Duration, "datetime.timedelta", "datetime");
93
94impl<T: PyStubType> PyStubType for &T {
95 fn type_input() -> TypeInfo {
96 T::type_input()
97 }
98 fn type_output() -> TypeInfo {
99 T::type_output()
100 }
101}
102
103impl<T: PyStubType> PyStubType for Rc<T> {
104 fn type_input() -> TypeInfo {
105 T::type_input()
106 }
107 fn type_output() -> TypeInfo {
108 T::type_output()
109 }
110}
111
112impl<T: PyStubType> PyStubType for Arc<T> {
113 fn type_input() -> TypeInfo {
114 T::type_input()
115 }
116 fn type_output() -> TypeInfo {
117 T::type_output()
118 }
119}