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
//! Register derived enums into a `#[pymodule]`.
//!
//! The only supported registration path is an explicit library-provided
//! helper — either the free [`add_enum`] function or the
//! [`PyModuleExt::add_enum`] extension method. No hidden global registry,
//! no replacement module macro.
use *;
use PyModule;
use cratePyEnum;
/// Register the Python class for `T` onto `module` under `T::SPEC.name`.
///
/// Triggers a one-time class construction on the first call per interpreter
/// per enum type (subsequent calls reuse the cached class). Prefer the
/// extension-method form [`PyModuleExt::add_enum`] inside `#[pymodule]`
/// bodies; this free function exists for call sites that want an explicit
/// type parameter.
///
/// If `module` already has an attribute with the same name, it is silently
/// replaced — consistent with `PyModule::add` and `m.add_class::<T>()?`.
/// Avoid registering two enums whose `#[pyenum(name = "...")]` resolves to
/// the same string within a single module.
///
/// ```rust,ignore
/// use pyenum::{PyEnum, add_enum};
/// use pyo3::prelude::*;
///
/// #[derive(Clone, Copy, PyEnum)]
/// pub enum Color { Red, Green, Blue }
///
/// #[pymodule]
/// fn demo(m: &Bound<'_, PyModule>) -> PyResult<()> {
/// add_enum::<Color>(m)
/// }
/// ```
/// Extension method for `Bound<'_, PyModule>` — the idiomatic way to
/// register a derived enum inside a `#[pymodule]` body.
///
/// ```rust,ignore
/// use pyenum::{PyEnum, PyModuleExt};
/// use pyo3::prelude::*;
///
/// #[derive(Clone, Copy, PyEnum)]
/// pub enum Color { Red, Green, Blue }
///
/// #[pymodule]
/// fn demo(m: &Bound<'_, PyModule>) -> PyResult<()> {
/// m.add_enum::<Color>()
/// }
/// ```