From 2b386e506e5270fbd0614b707f49956932922d0e Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Sun, 11 Aug 2024 23:14:27 +0100 Subject: [PATCH] update `README.md` for newer APIs --- README.md | 30 +++++++++++------------------- src/lib.rs | 40 ++-------------------------------------- 2 files changed, 13 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 624e22d..441f29f 100644 --- a/README.md +++ b/README.md @@ -10,14 +10,7 @@ that which is produced directly by `pythonize`. This crate converts Rust types which implement the [Serde] serialization traits into Python objects using the [PyO3] library. -Pythonize has two public APIs: `pythonize` and `depythonize_bound`. - - -
- -⚠️ Warning: API update in progress 🛠️ - -PyO3 0.21 has introduced a significant new API, termed the "Bound" API after the new smart pointer `Bound`, and pythonize is doing the same. +Pythonize has two main public APIs: `pythonize` and `depythonize`.
@@ -28,8 +21,8 @@ PyO3 0.21 has introduced a significant new API, termed the "Bound" API after the ```rust use serde::{Serialize, Deserialize}; -use pyo3::Python; -use pythonize::{depythonize_bound, pythonize}; +use pyo3::prelude::*; +use pythonize::{depythonize, pythonize}; #[derive(Debug, Serialize, Deserialize, PartialEq)] struct Sample { @@ -37,21 +30,20 @@ struct Sample { bar: Option } -let gil = Python::acquire_gil(); -let py = gil.python(); - let sample = Sample { foo: "Foo".to_string(), bar: None }; -// Rust -> Python -let obj = pythonize(py, &sample).unwrap(); +Python::with_gil(|py| { + // Rust -> Python + let obj = pythonize(py, &sample).unwrap(); -assert_eq!("{'foo': 'Foo', 'bar': None}", &format!("{}", obj.as_ref(py).repr().unwrap())); + assert_eq!("{'foo': 'Foo', 'bar': None}", &format!("{}", obj.repr().unwrap())); -// Python -> Rust -let new_sample: Sample = depythonize_bound(obj.into_bound(py)).unwrap(); + // Python -> Rust + let new_sample: Sample = depythonize(&obj).unwrap(); -assert_eq!(new_sample, sample); + assert_eq!(new_sample, sample); +}) ``` diff --git a/src/lib.rs b/src/lib.rs index 4ce1751..186fdf6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,41 +1,5 @@ -//! This crate converts Rust types which implement the [Serde] serialization -//! traits into Python objects using the [PyO3] library. -//! -//! Pythonize has two public APIs: `pythonize` and `depythonize`. -//! -//! [Serde]: https://github.com/serde-rs/serde -//! [PyO3]: https://github.com/PyO3/pyo3 -//! -//! # Examples -//! ``` -//! use serde::{Serialize, Deserialize}; -//! use pyo3::{types::PyAnyMethods, Python}; -//! use pythonize::{depythonize, pythonize}; -//! -//! #[derive(Debug, Serialize, Deserialize, PartialEq)] -//! struct Sample { -//! foo: String, -//! bar: Option -//! } -//! -//! Python::with_gil(|py| { -//! let sample = Sample { -//! foo: "Foo".to_string(), -//! bar: None -//! }; -//! -//! // Rust -> Python -//! let obj = pythonize(py, &sample).unwrap(); -//! -//! assert_eq!("{'foo': 'Foo', 'bar': None}", &format!("{}", obj.repr().unwrap())); -//! -//! // Python -> Rust -//! let new_sample: Sample = depythonize(&obj).unwrap(); -//! -//! assert_eq!(new_sample, sample); -//! }); -//! -//! ``` +#![doc = include_str!("../README.md")] + mod de; mod error; mod ser;