Summary
This is only about python objects like #[pymodule], #[pyclass], #[pyfunction], #[pymethod] and #[pyproperty].
Sometimes python names are not fit for rust naming convention.
- Though we can specify the name with
(name = "") attributes, we also need to decide how we give names for its rust side API.
- There are also python names conflict to rust keywords. They also need a special rule because we normally cannot name them same in Rust.
Detailed Explanation
List of unmatching names.
- Python type
int is type PyInt in Rust API.
- Python function
posix.WIFSIGNALED is posix::wifsignaled in Rust.
- Python type
_struct.error is PyTypeRef object pystruct::error_type in Rust.
- Python type
_weakref.ReferenceType is a function returning PyTypeRef _weakref::reference_type in Rust.
- Python variable
sys.api_version is const sys::API_VERSION in Rust.
- Python variable
dis.COMPILER_FLAG_NAMES is a function returning PyDictRef dis::compiler_flag_names in Rust.
We must handle types differently.
For example, we can expose &PyTypeRef for builtins::int() but PyInt through builtins::PyInt.
We can choose one of these kind of options for other names:
- Strictly follow Python names unless it is impossible.
- The names will not conflict.
- Some names will be
r# prefixed in Rust. They would need human-friendly aliases.
- Follow Python names as much as possible but with exceptions by its paired Rust type.
- For example,
_weakref.ReferenceType and int are both type in Python but PyTypeRef instance and PyInt struct for each in Rust. Similar for sys.api_version and dis.COMPILER_FLAG_NAMES
- Export a python-name aliased module separately. This can be generated by
derive macros.
- For this choice, we can optionally suggest more consistency.
- We can expose variable always through a function call. A function to return a const will be generated.
- We can attach emitted
vm parameter again to functions.
- Still types will be problem.
List of keyword conflict.
struct module.
- The exact python name is
_struct
mod operator.
- The exact python name is
__mod__
match function.
type type.
enum module.
List of confusing non-keyword names.
bool types
str types
Iterator for python and rust
Example of solutions
- Always keep python name using
r#. But allow alias.
- Add additional
_ suffix for conflicting type. match will be match_. But allow r# alias.
- Add
py prefix like current pybool, pytype, pystruct.
Drawbacks, Rationale, and Alternatives
Unresolved Questions
Everything. Any dicision will give concrete idea about public API design.
Summary
This is only about python objects like
#[pymodule],#[pyclass],#[pyfunction],#[pymethod]and#[pyproperty].Sometimes python names are not fit for rust naming convention.
(name = "")attributes, we also need to decide how we give names for its rust side API.Detailed Explanation
List of unmatching names.
intis typePyIntin Rust API.posix.WIFSIGNALEDisposix::wifsignaledin Rust._struct.erroris PyTypeRef objectpystruct::error_typein Rust._weakref.ReferenceTypeis a function returning PyTypeRef_weakref::reference_typein Rust.sys.api_versionis constsys::API_VERSIONin Rust.dis.COMPILER_FLAG_NAMESis a function returning PyDictRefdis::compiler_flag_namesin Rust.We must handle types differently.
For example, we can expose
&PyTypeRefforbuiltins::int()butPyIntthroughbuiltins::PyInt.We can choose one of these kind of options for other names:
r#prefixed in Rust. They would need human-friendly aliases._weakref.ReferenceTypeandintare both type in Python butPyTypeRefinstance andPyIntstruct for each in Rust. Similar for sys.api_version and dis.COMPILER_FLAG_NAMESderivemacros.vmparameter again to functions.List of keyword conflict.
structmodule._structmodoperator.__mod__matchfunction.typetype.enummodule.List of confusing non-keyword names.
booltypesstrtypesIteratorfor python and rustExample of solutions
r#. But allow alias._suffix for conflicting type.matchwill bematch_. But allowr#alias.pyprefix like currentpybool,pytype,pystruct.Drawbacks, Rationale, and Alternatives
Unresolved Questions
Everything. Any dicision will give concrete idea about public API design.