diff --git a/rust/src/events/internal_metadata.rs b/rust/src/events/internal_metadata.rs
index 53c7b1ba61..63774fbd54 100644
--- a/rust/src/events/internal_metadata.rs
+++ b/rust/src/events/internal_metadata.rs
@@ -38,9 +38,10 @@ use anyhow::Context;
use log::warn;
use pyo3::{
exceptions::PyAttributeError,
+ pybacked::PyBackedStr,
pyclass, pymethods,
- types::{PyDict, PyString},
- IntoPy, PyAny, PyObject, PyResult, Python,
+ types::{PyAnyMethods, PyDict, PyDictMethods, PyString},
+ Bound, IntoPy, PyAny, PyObject, PyResult, Python,
};
/// Definitions of the various fields of the internal metadata.
@@ -59,7 +60,7 @@ enum EventInternalMetadataData {
impl EventInternalMetadataData {
/// Convert the field to its name and python object.
- fn to_python_pair<'a>(&self, py: Python<'a>) -> (&'a PyString, PyObject) {
+ fn to_python_pair<'a>(&self, py: Python<'a>) -> (&'a Bound<'a, PyString>, PyObject) {
match self {
EventInternalMetadataData::OutOfBandMembership(o) => {
(pyo3::intern!(py, "out_of_band_membership"), o.into_py(py))
@@ -90,10 +91,13 @@ impl EventInternalMetadataData {
/// Converts from python key/values to the field.
///
/// Returns `None` if the key is a valid but unrecognized string.
- fn from_python_pair(key: &PyAny, value: &PyAny) -> PyResult<Option<Self>> {
- let key_str: &str = key.extract()?;
+ fn from_python_pair(
+ key: &Bound<'_, PyAny>,
+ value: &Bound<'_, PyAny>,
+ ) -> PyResult<Option<Self>> {
+ let key_str: PyBackedStr = key.extract()?;
- let e = match key_str {
+ let e = match &*key_str {
"out_of_band_membership" => EventInternalMetadataData::OutOfBandMembership(
value
.extract()
@@ -210,11 +214,11 @@ pub struct EventInternalMetadata {
#[pymethods]
impl EventInternalMetadata {
#[new]
- fn new(dict: &PyDict) -> PyResult<Self> {
+ fn new(dict: &Bound<'_, PyDict>) -> PyResult<Self> {
let mut data = Vec::with_capacity(dict.len());
for (key, value) in dict.iter() {
- match EventInternalMetadataData::from_python_pair(key, value) {
+ match EventInternalMetadataData::from_python_pair(&key, &value) {
Ok(Some(entry)) => data.push(entry),
Ok(None) => {}
Err(err) => {
@@ -240,7 +244,7 @@ impl EventInternalMetadata {
///
/// Note that `outlier` and `stream_ordering` are stored in separate columns so are not returned here.
fn get_dict(&self, py: Python<'_>) -> PyResult<PyObject> {
- let dict = PyDict::new(py);
+ let dict = PyDict::new_bound(py);
for entry in &self.data {
let (key, value) = entry.to_python_pair(py);
diff --git a/rust/src/events/mod.rs b/rust/src/events/mod.rs
index ee857b3d72..a4ade1a178 100644
--- a/rust/src/events/mod.rs
+++ b/rust/src/events/mod.rs
@@ -20,20 +20,23 @@
//! Classes for representing Events.
-use pyo3::{types::PyModule, PyResult, Python};
+use pyo3::{
+ types::{PyAnyMethods, PyModule, PyModuleMethods},
+ Bound, PyResult, Python,
+};
mod internal_metadata;
/// Called when registering modules with python.
-pub fn register_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
- let child_module = PyModule::new(py, "events")?;
+pub fn register_module(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
+ let child_module = PyModule::new_bound(py, "events")?;
child_module.add_class::<internal_metadata::EventInternalMetadata>()?;
- m.add_submodule(child_module)?;
+ m.add_submodule(&child_module)?;
// We need to manually add the module to sys.modules to make `from
// synapse.synapse_rust import events` work.
- py.import("sys")?
+ py.import_bound("sys")?
.getattr("modules")?
.set_item("synapse.synapse_rust.events", child_module)?;
|