Function to_pyobject

Source
pub fn to_pyobject<'py, T>(
    py: Python<'py>,
    value: &T,
) -> Result<Bound<'py, PyAny>, Error>
where T: Serialize + ?Sized,
Expand description

Serialize T: Serialize into a pyo3::PyAny value.

§Examples

§string

use pyo3::{Python, types::{PyString, PyAnyMethods}};
use serde_pyobject::to_pyobject;

Python::with_gil(|py| {
    // char
    let obj = to_pyobject(py, &'a').unwrap();
    assert!(obj.is_exact_instance_of::<PyString>());
    // &str
    let obj = to_pyobject(py, "test").unwrap();
    assert!(obj.is_exact_instance_of::<PyString>());
});

§integer

use pyo3::{Python, types::{PyLong, PyAnyMethods}};
use serde_pyobject::to_pyobject;

Python::with_gil(|py| {
    let obj = to_pyobject(py, &1_u16).unwrap();
    assert!(obj.is_exact_instance_of::<PyLong>());

    let obj = to_pyobject(py, &1_i64).unwrap();
    assert!(obj.is_exact_instance_of::<PyLong>());

    let obj = to_pyobject(py, &1_i64).unwrap();
    assert!(obj.is_exact_instance_of::<PyLong>());
});

§float

use pyo3::{Python, types::{PyFloat, PyAnyMethods}};
use serde_pyobject::to_pyobject;

Python::with_gil(|py| {
    let obj = to_pyobject(py, &3.1_f32).unwrap();
    assert!(obj.is_exact_instance_of::<PyFloat>());

    let obj = to_pyobject(py, &-3.1_f64).unwrap();
    assert!(obj.is_exact_instance_of::<PyFloat>());
});

§option

Rust None is serialized to Python None, and Some(value) is serialized as value is serialized

use pyo3::{Python, types::{PyLong, PyAnyMethods}};
use serde_pyobject::to_pyobject;

Python::with_gil(|py| {
    let obj = to_pyobject(py, &Option::<i32>::None).unwrap();
    assert!(obj.is(&py.None()));

    let obj = to_pyobject(py, &Some(1_i64)).unwrap();
    assert!(obj.is_exact_instance_of::<PyLong>());
});

§unit

Rust’s () is serialized to Python’s ()

use pyo3::{Python, types::{PyTuple, PyAnyMethods}};
use serde_pyobject::to_pyobject;

Python::with_gil(|py| {
    let obj = to_pyobject(py, &()).unwrap();
    assert!(obj.is(&PyTuple::empty_bound(py)));
});

§unit_struct

Unit is serialized as an empty tuple ()

use serde::Serialize;
use pyo3::{Python, types::{PyTuple, PyAnyMethods}};
use serde_pyobject::{to_pyobject, pydict};

#[derive(Serialize)]
struct UnitStruct;

Python::with_gil(|py| {
    let obj = to_pyobject(py, &UnitStruct {}).unwrap();
    assert!(obj.eq(PyTuple::empty_bound(py)).unwrap());
});

§unit_variant

use serde::Serialize;
use pyo3::{Python, types::{PyTuple, PyAnyMethods}};
use serde_pyobject::{to_pyobject, pydict};

#[derive(Serialize)]
enum UnitVariant {
    A,
    B,
}

Python::with_gil(|py| {
    let obj = to_pyobject(py, &UnitVariant::A).unwrap();
    assert!(obj.eq("A").unwrap());
    let obj = to_pyobject(py, &UnitVariant::B).unwrap();
    assert!(obj.eq("B").unwrap());
});

§newtype_struct

use serde::Serialize;
use pyo3::{Python, types::{PyLong, PyAnyMethods}};
use serde_pyobject::to_pyobject;

#[derive(Serialize)]
struct NewtypeStruct(u8);

Python::with_gil(|py| {
    let obj = to_pyobject(py, &NewtypeStruct(10)).unwrap();
    assert!(obj.is_exact_instance_of::<PyLong>());
    assert!(obj.eq(10).unwrap());
});

§newtype_variant

use serde::Serialize;
use pyo3::{Python, types::PyAnyMethods};
use serde_pyobject::{to_pyobject, pydict};

#[derive(Serialize)]
enum NewtypeVariant {
    N(u8),
}

Python::with_gil(|py| {
    let obj = to_pyobject(py, &NewtypeVariant::N(3)).unwrap();
    assert!(obj.eq(pydict! { "N" => 3 }.unwrap()).unwrap());
});

§seq

use pyo3::{Python, types::PyAnyMethods};
use serde_pyobject::{to_pyobject, pylist};

Python::with_gil(|py| {
    let obj = to_pyobject(py, &vec![1, 2, 3]).unwrap();
    assert!(obj.eq(pylist![py; 1, 2, 3].unwrap()).unwrap());
});

§tuple

use pyo3::{IntoPy, Python, types::{PyTuple, PyAnyMethods}};
use serde_pyobject::to_pyobject;

Python::with_gil(|py| {
    let obj = to_pyobject(py, &(3, "test")).unwrap();
    assert!(obj.eq(PyTuple::new_bound(py, [3.into_py(py), "test".into_py(py)])).unwrap());
});

§tuple struct

use pyo3::{Python, types::{PyTuple, PyAnyMethods}};
use serde::Serialize;
use serde_pyobject::to_pyobject;

#[derive(Serialize)]
struct TupleStruct(u8, u8, u8);

Python::with_gil(|py| {
    let obj = to_pyobject(py, &TupleStruct(1, 2, 3)).unwrap();
    assert!(obj.eq(PyTuple::new_bound(py, [1, 2, 3])).unwrap());
});

§tuple variant

use pyo3::{Python, types::PyAnyMethods};
use serde::Serialize;
use serde_pyobject::{to_pyobject, pydict};

#[derive(Serialize)]
enum TupleVariant {
    T(u8, u8),
}

Python::with_gil(|py| {
    let obj = to_pyobject(py, &TupleVariant::T(1, 2)).unwrap();
    assert!(obj.eq(pydict!{ "T" => (1, 2) }.unwrap()).unwrap());
});

§map

use pyo3::{Python, types::PyAnyMethods};
use serde_pyobject::{to_pyobject, pydict};
use maplit::hashmap;

Python::with_gil(|py| {
    let obj = to_pyobject(py, &hashmap! {
        "a".to_owned() => 1_u8,
        "b".to_owned() => 2,
        "c".to_owned() => 3
    }).unwrap();
    assert!(obj.eq(pydict! {
        "a" => 1,
        "b" => 2,
        "c" => 3
    }.unwrap()).unwrap());
});

§struct

use serde::Serialize;
use pyo3::{IntoPy, Python, types::{PyTuple, PyAnyMethods}};
use serde_pyobject::{to_pyobject, pydict};

#[derive(Serialize)]
struct Struct {
    a: i32,
    b: String,
}

Python::with_gil(|py| {
    let obj = to_pyobject(py, &Struct { a: 32, b: "test".to_string() }).unwrap();
    assert!(obj.eq(pydict!{ "a" => 32, "b" => "test" }.unwrap()).unwrap());
});

§struct variant

use serde::Serialize;
use pyo3::{Python, types::PyAnyMethods};
use serde_pyobject::{to_pyobject, pydict};

#[derive(Serialize)]
enum StructVariant {
    S { r: u8, g: u8, b: u8 },
}

Python::with_gil(|py| {
    let obj = to_pyobject(py, &StructVariant::S { r: 1, g: 2, b: 3 }).unwrap();
    assert!(
        obj.eq(
            pydict! {
                "S" => pydict! {
                    "r" => 1,
                    "g" => 2,
                    "b" => 3
                }.unwrap()
            }.unwrap()
        ).unwrap()
    );
});