Skip to main content

pyenum/
lib.rs

1//! # pyenum
2//!
3//! Expose Rust `enum` types to Python as genuine `enum.Enum` subclasses via
4//! PyO3 — `#[derive(PyEnum)]`, functional-API class construction, a
5//! per-interpreter cache, and `IntoPyObject` / `FromPyObject` conversion
6//! plumbing generated by the derive.
7//!
8//! ## Scope
9//!
10//! * Unit-variant enums only; tuple / struct / generic / lifetime-parameterised
11//!   enums are rejected at compile time.
12//! * Five Python enum bases are selectable: `Enum` (default), `IntEnum`,
13//!   `StrEnum`, `Flag`, `IntFlag`.
14//! * Rust variant names are passed through unchanged (Python member name ==
15//!   Rust identifier).
16//! * The Python class is constructed exactly once per interpreter and cached
17//!   via `pyo3::sync::PyOnceLock`.
18//! * PyO3 0.28, CPython 3.11+.
19//! * Free-threaded (`--disable-gil`) Python, module reload, and sub-interpreter
20//!   finalisation are out of scope.
21
22#![deny(missing_debug_implementations)]
23#![cfg_attr(docsrs, feature(doc_cfg))]
24
25mod cache;
26mod construct;
27mod register;
28mod trait_def;
29
30pub use pyenum_derive::PyEnum;
31pub use register::{PyModuleExt, add_enum};
32pub use trait_def::{PyEnum as PyEnumTrait, PyEnumBase, PyEnumSpec, VariantLiteral};
33
34/// Implementation detail — referenced by the output of `#[derive(PyEnum)]`.
35///
36/// Consumers MUST NOT import anything from this module directly; its shape is
37/// allowed to change between patch releases without warning.
38#[doc(hidden)]
39pub mod __private {
40    pub use ::pyo3;
41    pub use ::pyo3::sync::PyOnceLock as OnceLock;
42
43    pub use crate::cache::get_or_build;
44    pub use crate::construct::build_py_enum;
45    pub use crate::trait_def::{PyEnum, PyEnumBase, PyEnumSpec, VariantLiteral};
46}