Skip to content

Commit

Permalink
update README.md for newer APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Aug 11, 2024
1 parent 33451dc commit 2b386e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 57 deletions.
30 changes: 11 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.


<div class="warning">

⚠️ Warning: API update in progress 🛠️

PyO3 0.21 has introduced a significant new API, termed the "Bound" API after the new smart pointer `Bound<T>`, and pythonize is doing the same.
Pythonize has two main public APIs: `pythonize` and `depythonize`.

</div>

Expand All @@ -28,30 +21,29 @@ 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 {
foo: String,
bar: Option<usize>
}

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);
})
```
40 changes: 2 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<usize>
//! }
//!
//! 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;
Expand Down

0 comments on commit 2b386e5

Please sign in to comment.