Skip to content

Commit

Permalink
migrate PyDictMethods trait bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Aug 25, 2024
1 parent b2a2a1d commit 7ead7ff
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 56 deletions.
10 changes: 2 additions & 8 deletions src/conversions/hashbrown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
dict.set_item(k, v)?;
}
Ok(dict)
}
Expand All @@ -89,10 +86,7 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
dict.set_item(k, v)?;
}
Ok(dict)
}
Expand Down
12 changes: 3 additions & 9 deletions src/conversions/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
use crate::conversion::IntoPyObject;
use crate::types::*;
use crate::{Bound, BoundObject, FromPyObject, IntoPy, PyErr, PyObject, Python, ToPyObject};
use crate::{Bound, FromPyObject, IntoPy, PyErr, PyObject, Python, ToPyObject};
use std::{cmp, hash};

impl<K, V, H> ToPyObject for indexmap::IndexMap<K, V, H>
Expand Down Expand Up @@ -130,10 +130,7 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
dict.set_item(k, v)?;
}
Ok(dict)
}
Expand All @@ -152,10 +149,7 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
dict.set_item(k, v)?;
}
Ok(dict)
}
Expand Down
22 changes: 5 additions & 17 deletions src/conversions/std/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
conversion::IntoPyObject,
instance::Bound,
types::{any::PyAnyMethods, dict::PyDictMethods, IntoPyDict, PyDict},
BoundObject, FromPyObject, IntoPy, PyAny, PyErr, PyObject, Python, ToPyObject,
FromPyObject, IntoPy, PyAny, PyErr, PyObject, Python, ToPyObject,
};

impl<K, V, H> ToPyObject for collections::HashMap<K, V, H>
Expand Down Expand Up @@ -62,10 +62,7 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
dict.set_item(k, v)?;
}
Ok(dict)
}
Expand All @@ -84,10 +81,7 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
dict.set_item(k, v)?;
}
Ok(dict)
}
Expand Down Expand Up @@ -123,10 +117,7 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
dict.set_item(k, v)?;
}
Ok(dict)
}
Expand All @@ -144,10 +135,7 @@ where
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
dict.set_item(k, v)?;
}
Ok(dict)
}
Expand Down
70 changes: 48 additions & 22 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::instance::{Borrowed, Bound};
use crate::py_result_ext::PyResultExt;
use crate::types::any::PyAnyMethods;
use crate::types::{PyAny, PyList};
use crate::{ffi, Python, ToPyObject};
use crate::{ffi, BoundObject, Python, ToPyObject};

/// Represents a Python `dict`.
///
Expand Down Expand Up @@ -128,7 +128,7 @@ pub trait PyDictMethods<'py>: crate::sealed::Sealed {
/// This is equivalent to the Python expression `key in self`.
fn contains<K>(&self, key: K) -> PyResult<bool>
where
K: ToPyObject;
K: IntoPyObject<'py>;

/// Gets an item from the dictionary.
///
Expand All @@ -137,22 +137,22 @@ pub trait PyDictMethods<'py>: crate::sealed::Sealed {
/// To get a `KeyError` for non-existing keys, use `PyAny::get_item`.
fn get_item<K>(&self, key: K) -> PyResult<Option<Bound<'py, PyAny>>>
where
K: ToPyObject;
K: IntoPyObject<'py>;

/// Sets an item value.
///
/// This is equivalent to the Python statement `self[key] = value`.
fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()>
where
K: ToPyObject,
V: ToPyObject;
K: IntoPyObject<'py>,
V: IntoPyObject<'py>;

/// Deletes an item.
///
/// This is equivalent to the Python statement `del self[key]`.
fn del_item<K>(&self, key: K) -> PyResult<()>
where
K: ToPyObject;
K: IntoPyObject<'py>;

/// Returns a list of dict keys.
///
Expand Down Expand Up @@ -224,9 +224,9 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {

fn contains<K>(&self, key: K) -> PyResult<bool>
where
K: ToPyObject,
K: IntoPyObject<'py>,
{
fn inner(dict: &Bound<'_, PyDict>, key: Bound<'_, PyAny>) -> PyResult<bool> {
fn inner(dict: &Bound<'_, PyDict>, key: &Bound<'_, PyAny>) -> PyResult<bool> {
match unsafe { ffi::PyDict_Contains(dict.as_ptr(), key.as_ptr()) } {
1 => Ok(true),
0 => Ok(false),
Expand All @@ -235,16 +235,22 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
}

let py = self.py();
inner(self, key.to_object(py).into_bound(py))
inner(
self,
&key.into_pyobject(py)
.map_err(Into::into)?
.into_any()
.as_borrowed(),
)
}

fn get_item<K>(&self, key: K) -> PyResult<Option<Bound<'py, PyAny>>>
where
K: ToPyObject,
K: IntoPyObject<'py>,
{
fn inner<'py>(
dict: &Bound<'py, PyDict>,
key: Bound<'_, PyAny>,
key: &Bound<'_, PyAny>,
) -> PyResult<Option<Bound<'py, PyAny>>> {
let py = dict.py();
let mut result: *mut ffi::PyObject = std::ptr::null_mut();
Expand All @@ -258,18 +264,24 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
}

let py = self.py();
inner(self, key.to_object(py).into_bound(py))
inner(
self,
&key.into_pyobject(py)
.map_err(Into::into)?
.into_any()
.as_borrowed(),
)
}

fn set_item<K, V>(&self, key: K, value: V) -> PyResult<()>
where
K: ToPyObject,
V: ToPyObject,
K: IntoPyObject<'py>,
V: IntoPyObject<'py>,
{
fn inner(
dict: &Bound<'_, PyDict>,
key: Bound<'_, PyAny>,
value: Bound<'_, PyAny>,
key: &Bound<'_, PyAny>,
value: &Bound<'_, PyAny>,
) -> PyResult<()> {
err::error_on_minusone(dict.py(), unsafe {
ffi::PyDict_SetItem(dict.as_ptr(), key.as_ptr(), value.as_ptr())
Expand All @@ -279,23 +291,36 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> {
let py = self.py();
inner(
self,
key.to_object(py).into_bound(py),
value.to_object(py).into_bound(py),
&key.into_pyobject(py)
.map_err(Into::into)?
.into_any()
.as_borrowed(),
&value
.into_pyobject(py)
.map_err(Into::into)?
.into_any()
.as_borrowed(),
)
}

fn del_item<K>(&self, key: K) -> PyResult<()>
where
K: ToPyObject,
K: IntoPyObject<'py>,
{
fn inner(dict: &Bound<'_, PyDict>, key: Bound<'_, PyAny>) -> PyResult<()> {
fn inner(dict: &Bound<'_, PyDict>, key: &Bound<'_, PyAny>) -> PyResult<()> {
err::error_on_minusone(dict.py(), unsafe {
ffi::PyDict_DelItem(dict.as_ptr(), key.as_ptr())
})
}

let py = self.py();
inner(self, key.to_object(py).into_bound(py))
inner(
self,
&key.into_pyobject(py)
.map_err(Into::into)?
.into_any()
.as_borrowed(),
)
}

fn keys(&self) -> Bound<'py, PyList> {
Expand Down Expand Up @@ -527,6 +552,7 @@ mod borrowed_iter {
}
}

use crate::prelude::IntoPyObject;
pub(crate) use borrowed_iter::BorrowedDictIter;

/// Conversion trait that allows a sequence of tuples to be converted into `PyDict`
Expand All @@ -552,7 +578,7 @@ where
fn into_py_dict(self, py: Python<'_>) -> Bound<'_, PyDict> {
let dict = PyDict::new(py);
for item in self {
dict.set_item(item.key(), item.value())
dict.set_item(item.key().to_object(py), item.value().to_object(py))
.expect("Failed to set_item on dict");
}
dict
Expand Down

0 comments on commit 7ead7ff

Please sign in to comment.