Annotate Rust blocks with "rust"

This commit is contained in:
mejrs 2022-01-29 01:03:57 +01:00
parent bb0ae4942f
commit 463543ad58
2 changed files with 14 additions and 14 deletions

View File

@ -53,31 +53,31 @@ After these steps you are ready to annotate your code!
The `#[cfg]` flags added by `pyo3-build-cfg` can be combined with all of Rust's logic in the `#[cfg]` attribute to create very precise conditional code generation. The following are some common patterns implemented using these flags:
```
```rust,ignore
#[cfg(Py_3_7)]
```
This `#[cfg]` marks code that will only be present on Python 3.7 and upwards. There are similar options `Py_3_8`, `Py_3_9`, `Py_3_10` and so on for each minor version.
```
```rust,ignore
#[cfg(not(Py_3_7))]
```
This `#[cfg]` marks code that will only be present on Python versions before (but not including) Python 3.7.
```
```rust,ignore
#[cfg(not(Py_LIMITED_API))]
```
This `#[cfg]` marks code that is only available when building for the unlimited Python API (i.e. PyO3's `abi3` feature is not enabled). This might be useful if you want to ship your extension module as an `abi3` wheel and also allow users to compile it from source to make use of optimizations only possible with the unlimited API.
```
```rust,ignore
#[cfg(any(Py_3_9, not(Py_LIMITED_API)))]
```
This `#[cfg]` marks code which is available when running Python 3.9 or newer, or when using the unlimited API with an older Python version. Patterns like this are commonly seen on Python APIs which were added to the limited Python API in a specific minor version.
```
```rust,ignore
#[cfg(PyPy)]
```

View File

@ -35,7 +35,7 @@ structs is not supported.
The derivation generates code that will attempt to access the attribute `my_string` on
the Python object, i.e. `obj.getattr("my_string")`, and call `extract()` on the attribute.
```
```rust
use pyo3::prelude::*;
#[derive(FromPyObject)]
@ -65,7 +65,7 @@ struct RustyStruct {
By setting the `#[pyo3(item)]` attribute on the field, PyO3 will attempt to extract the value by calling the `get_item` method on the Python object.
```
```rust
use pyo3::prelude::*;
@ -90,7 +90,7 @@ struct RustyStruct {
The argument passed to `getattr` and `get_item` can also be configured:
```
```rust
use pyo3::prelude::*;
#[derive(FromPyObject)]
@ -135,7 +135,7 @@ Tuple structs are also supported but do not allow customizing the extraction. Th
always assumed to be a Python tuple with the same length as the Rust type, the `n`th field
is extracted from the `n`th item in the Python tuple.
```
```rust
use pyo3::prelude::*;
#[derive(FromPyObject)]
@ -158,7 +158,7 @@ struct RustyTuple(String, String);
Tuple structs with a single field are treated as wrapper types which are described in the
following section. To override this behaviour and ensure that the input is in fact a tuple,
specify the struct as
```
```rust
use pyo3::prelude::*;
#[derive(FromPyObject)]
@ -184,7 +184,7 @@ in extracting directly from the input object, i.e. `obj.extract()`, rather than
an item or attribute. This behaviour is enabled per default for newtype structs and tuple-variants
with a single field.
```
```rust
use pyo3::prelude::*;
#[derive(FromPyObject)]
@ -223,7 +223,7 @@ i.e. a tuple variant assumes that the input is a Python tuple, and a struct vari
extracting fields as attributes but can be configured in the same manner. The `transparent`
attribute can be applied to single-field-variants.
```
```rust
use pyo3::prelude::*;
#[derive(FromPyObject)]
@ -370,7 +370,7 @@ tested variants is returned. The names reported in the error message can be cust
through the `#[pyo3(annotation = "name")]` attribute, e.g. to use conventional Python type
names:
```
```rust
use pyo3::prelude::*;
#[derive(FromPyObject)]
@ -454,7 +454,7 @@ All types in PyO3 implement this trait, as does a `#[pyclass]` which doesn't use
Occasionally you may choose to implement this for custom types which are mapped to Python types
_without_ having a unique python type.
```
```rust
use pyo3::prelude::*;
struct MyPyObjectWrapper(PyObject);