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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Provides derive macros that implements conversion by built-in functions.
/// Derive macro generating an impl of [`__bool__`][py] method by [`Into<bool>`] trait.
///
/// # Expansion
///
/// This implements, for example:
///
/// ```
/// # use pyo3::prelude::*;
/// # #[pyclass]
/// # struct PyClass {}
/// # impl From<&PyClass> for bool {
/// # fn from(v: &PyClass) -> bool { true }
/// # }
/// #[pymethods]
/// impl PyClass {
/// fn __bool__(&self) -> bool {
/// Into::into(self)
/// }
/// }
/// ```
///
/// # Example
///
/// ```
/// use pyo3::{prelude::*, py_run};
///
/// use pyderive::PyNew;
/// use pyderive::convert::PyBool;
///
/// #[derive(PyNew, PyBool)]
/// #[pyclass]
/// struct PyClass {
/// field: i64
/// }
///
/// impl From<&PyClass> for bool {
/// fn from(value: &PyClass) -> Self {
/// value.field.is_positive()
/// }
/// }
///
/// let test = "
/// actual = bool(PyClass(1))
/// assert isinstance(actual, bool)
/// assert actual is True
/// ";
///
/// Python::attach(|py| {
/// let PyClass = py.get_type::<PyClass>();
/// py_run!(py, PyClass, test)
/// });
/// ```
///
/// [py]: https://docs.python.org/3/reference/datamodel.html#object.__bool__
pub use PyBool;
/// Derive macro generating an impl of [`__bytes__`][py] method by [`Into<Cow<[u8]>>`][core::convert::Into] trait.
///
/// # Expansion
///
/// This implements:
///
/// ```
/// # use std::borrow::Cow;
/// # use pyo3::prelude::*;
/// # #[pyclass]
/// # struct PyClass {}
/// # impl From<&PyClass> for Cow<'_, [u8]> {
/// # fn from(_value: &PyClass) -> Self {
/// # vec![].into()
/// # }
/// # }
/// #[pymethods]
/// impl PyClass {
/// fn __bytes__(&self) -> Cow<[u8]> {
/// Into::into(self)
/// }
/// }
/// ```
///
/// # Example
///
/// ```
/// use std::borrow::Cow;
///
/// use pyo3::{prelude::*, py_run};
///
/// use pyderive::PyNew;
/// use pyderive::convert::PyBytes;
///
/// #[derive(PyNew, PyBytes)]
/// #[pyclass]
/// struct PyClass {
/// a: u8,
/// b: u8,
/// c: u8
/// }
///
/// impl From<&PyClass> for Cow<'_, [u8]> {
/// fn from(value: &PyClass) -> Self {
/// vec![value.a, value.b, value.c].into()
/// }
/// }
///
/// let test = r#"
/// actual = bytes(PyClass(1, 2, 3))
/// assert isinstance(actual, bytes)
/// assert actual == b'\x01\x02\x03'
/// "#;
///
/// Python::attach(|py| {
/// let PyClass = py.get_type::<PyClass>();
/// py_run!(py, PyClass, test)
/// });
/// ```
///
/// [py]: https://docs.python.org/3/reference/datamodel.html#object.__bytes__
pub use PyBytes;
/// Derive macro generating an impl of [`__complex__`][py] method by [`Into<num_complex::Complex64>`] trait.
///
/// # Expansion
///
/// This implements, for example:
///
/// ```
/// # use pyo3::prelude::*;
/// # use num_complex;
/// # #[pyclass]
/// # struct PyClass {}
/// # impl From<&PyClass> for num_complex::Complex64 {
/// # fn from(v: &PyClass) -> num_complex::Complex64 { Self::new(0., 0.) }
/// # }
/// #[pymethods]
/// impl PyClass {
/// fn __complex__(&self) -> num_complex::Complex64 {
/// Into::into(self)
/// }
/// }
/// ```
///
///
/// # Example
///
/// ```
/// use pyo3::{prelude::*, py_run};
/// use num_complex::Complex64;
///
/// use pyderive::PyNew;
/// use pyderive::convert::PyComplex;
///
/// #[derive(PyNew, PyComplex)]
/// #[pyclass]
/// struct PyClass {
/// c: Complex64,
/// }
///
/// impl From<&PyClass> for Complex64 {
/// fn from(value: &PyClass) -> Complex64 {
/// value.c * 2.0
/// }
/// }
///
/// let test = "
/// actual = complex(PyClass(1.0 + 2.0j))
/// assert isinstance(actual, complex)
/// assert actual == 2.0 + 4.0j
/// ";
///
/// Python::attach(|py| {
/// let PyClass = py.get_type::<PyClass>();
/// py_run!(py, PyClass, test)
/// });
/// ```
///
/// [py]: https://docs.python.org/3/reference/datamodel.html#object.__complex__
pub use PyComplex;
/// Derive macro generating an impl of [`__float__`][py] method by [`Into<f64>`] trait.
///
/// # Expansion
///
/// This implements, for example:
///
/// ```
/// # use pyo3::prelude::*;
/// # #[pyclass]
/// # struct PyClass {}
/// # impl From<&PyClass> for f64 {
/// # fn from(v: &PyClass) -> f64 { 0.0 }
/// # }
/// #[pymethods]
/// impl PyClass {
/// fn __float__(&self) -> f64 {
/// Into::into(self)
/// }
/// }
/// ```
///
/// # Example
///
/// ```
/// use pyo3::{prelude::*, py_run};
///
/// use pyderive::PyNew;
/// use pyderive::convert::PyFloat;
///
/// #[derive(PyNew, PyFloat)]
/// #[pyclass]
/// struct PyClass {
/// field: i64
/// }
///
/// impl From<&PyClass> for f64 {
/// fn from(value: &PyClass) -> f64 {
/// (value.field * 2) as f64
/// }
/// }
///
/// let test = "
/// actual = float(PyClass(1))
/// assert isinstance(actual, float)
/// assert actual == 2.0
/// ";
///
/// Python::attach(|py| {
/// let PyClass = py.get_type::<PyClass>();
/// py_run!(py, PyClass, test)
/// });
/// ```
///
/// [py]: https://docs.python.org/3/reference/datamodel.html#object.__float__
pub use PyFloat;
/// Derive macro generating an impl of [`__index__`][py] method by [`Into<isize>`] trait.
///
/// # Expansion
///
/// This implements, for example:
///
/// ```
/// # use pyo3::prelude::*;
/// # #[pyclass]
/// # struct PyClass {}
/// # impl From<&PyClass> for isize {
/// # fn from(v: &PyClass) -> isize { 0 }
/// # }
/// #[pymethods]
/// impl PyClass {
/// fn __index__(&self) -> isize {
/// Into::into(self)
/// }
/// }
/// ```
///
/// # Example
///
/// ```
/// use pyo3::{prelude::*, py_run};
///
/// use pyderive::PyNew;
/// use pyderive::convert::PyIndex;
///
/// #[derive(PyNew, PyIndex)]
/// #[pyclass]
/// struct PyClass {
/// field: i64
/// }
///
/// impl From<&PyClass> for isize {
/// fn from(value: &PyClass) -> isize {
/// (value.field * 2) as isize
/// }
/// }
///
/// let test = "
/// actual = int(PyClass(1))
/// assert isinstance(actual, int)
/// assert actual == 2
/// ";
///
/// Python::attach(|py| {
/// let PyClass = py.get_type::<PyClass>();
/// py_run!(py, PyClass, test)
/// });
/// ```
///
/// [py]: https://docs.python.org/3/reference/datamodel.html#object.__index__
pub use PyIndex;
/// Derive macro generating an impl of [`__int__`][py] method by [`Into<i64>`] trait.
///
/// # Expansion
///
/// This implements, for example:
///
/// ```
/// # use pyo3::prelude::*;
/// # #[pyclass]
/// # struct PyClass {}
/// # impl From<&PyClass> for i64 {
/// # fn from(v: &PyClass) -> i64 { 0 }
/// # }
/// #[pymethods]
/// impl PyClass {
/// fn __int__(&self) -> i64 {
/// Into::into(self)
/// }
/// }
/// ```
///
///
/// # Example
///
/// ```
/// use pyo3::{prelude::*, py_run};
///
/// use pyderive::PyNew;
/// use pyderive::convert::PyInt;
///
/// #[derive(PyNew, PyInt)]
/// #[pyclass]
/// struct PyClass {
/// field: i64
/// }
///
/// impl From<&PyClass> for i64 {
/// fn from(value: &PyClass) -> i64 {
/// value.field * 2
/// }
/// }
///
/// let test = "
/// actual = int(PyClass(1))
/// assert isinstance(actual, int)
/// assert actual == 2
/// ";
///
/// Python::attach(|py| {
/// let PyClass = py.get_type::<PyClass>();
/// py_run!(py, PyClass, test)
/// });
/// ```
///
/// [py]: https://docs.python.org/3/reference/datamodel.html#object.__int__
pub use PyInt;