Python is a reference-counted language and those references can be held for an arbitrarily long time which is untraceable by the Rust compiler. They are a compile-time only concept there is no way to access Rust lifetimes at runtime from a dynamic language like Python.Īs soon as Rust data is exposed to Python, there is no guarantee which the Rust compiler can make on how long the data will live. Rust lifetimes are used by the Rust compiler to reason about a program's memory safety. The reason for each of these is explained below. In particular, they must have no lifetime parameters, no generic parameters, and must implement Send. To integrate Rust types with Python, PyO3 needs to place some restrictions on the types which can be annotated with #. To see these generated implementations, refer to the implementation details at the end of this chapter. The above example generates implementations for PyTypeInfo and P圜lass for M圜lass and MyEnum. OtherVariant = 30, // PyO3 supports custom discriminants. PyO3 supports custom discriminants in enums To define a custom Python class, add the # attribute to a Rust struct or a fieldless enum. Below is a list of links to the relevant section of this chapter for each: This chapter will discuss the functionality and configuration these attributes offer. (If the multiple-pymethods feature is enabled, each # is allowed to have multiple # blocks.) # may also have implementations for Python magic methods such as _str_. They will usually also have one #-annotated impl block for the struct, which is used to define Python methods and constants for the generated Python type. C-like enum) to generate a Python type for it. The main attribute is #, which is placed upon a Rust struct or a fieldless enum (a.k.a. PyO3 exposes a group of attributes powered by Rust's proc macro system for defining Python classes as Rust structs.
0 Comments
Leave a Reply. |