Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update README.md for newer APIs #74

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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