Skip to content

Commit 0344921

Browse files
authored
Merge pull request #3694 from davidhewitt/set-frozenset
implement `PySetMethods` and `PyFrozenSetMethods`
2 parents c44d2f5 + 442d13d commit 0344921

File tree

8 files changed

+388
-140
lines changed

8 files changed

+388
-140
lines changed

src/instance.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ impl<'py, T> Bound<'py, T> {
206206
)
207207
}
208208

209+
/// Removes the connection for this `Bound<T>` from the GIL, allowing
210+
/// it to cross thread boundaries.
211+
pub fn unbind(self) -> Py<T> {
212+
// Safety: the type T is known to be correct and the ownership of the
213+
// pointer is transferred to the new Py<T> instance.
214+
unsafe { Py::from_non_null(self.into_non_null()) }
215+
}
216+
209217
/// Casts this `Bound<T>` as the corresponding "GIL Ref" type.
210218
///
211219
/// This is a helper to be used for migration from the deprecated "GIL Refs" API.
@@ -973,7 +981,7 @@ impl<T> Py<T> {
973981
where
974982
N: IntoPy<Py<PyString>>,
975983
{
976-
self.bind(py).as_any().getattr(attr_name).map(Into::into)
984+
self.bind(py).as_any().getattr(attr_name).map(Bound::unbind)
977985
}
978986

979987
/// Sets an attribute value.
@@ -1017,21 +1025,21 @@ impl<T> Py<T> {
10171025
args: impl IntoPy<Py<PyTuple>>,
10181026
kwargs: Option<&PyDict>,
10191027
) -> PyResult<PyObject> {
1020-
self.bind(py).as_any().call(args, kwargs).map(Into::into)
1028+
self.bind(py).as_any().call(args, kwargs).map(Bound::unbind)
10211029
}
10221030

10231031
/// Calls the object with only positional arguments.
10241032
///
10251033
/// This is equivalent to the Python expression `self(*args)`.
10261034
pub fn call1(&self, py: Python<'_>, args: impl IntoPy<Py<PyTuple>>) -> PyResult<PyObject> {
1027-
self.bind(py).as_any().call1(args).map(Into::into)
1035+
self.bind(py).as_any().call1(args).map(Bound::unbind)
10281036
}
10291037

10301038
/// Calls the object without arguments.
10311039
///
10321040
/// This is equivalent to the Python expression `self()`.
10331041
pub fn call0(&self, py: Python<'_>) -> PyResult<PyObject> {
1034-
self.bind(py).as_any().call0().map(Into::into)
1042+
self.bind(py).as_any().call0().map(Bound::unbind)
10351043
}
10361044

10371045
/// Calls a method on the object.
@@ -1054,7 +1062,7 @@ impl<T> Py<T> {
10541062
self.bind(py)
10551063
.as_any()
10561064
.call_method(name, args, kwargs)
1057-
.map(Into::into)
1065+
.map(Bound::unbind)
10581066
}
10591067

10601068
/// Calls a method on the object with only positional arguments.
@@ -1071,7 +1079,7 @@ impl<T> Py<T> {
10711079
self.bind(py)
10721080
.as_any()
10731081
.call_method1(name, args)
1074-
.map(Into::into)
1082+
.map(Bound::unbind)
10751083
}
10761084

10771085
/// Calls a method on the object with no arguments.
@@ -1084,7 +1092,7 @@ impl<T> Py<T> {
10841092
where
10851093
N: IntoPy<Py<PyString>>,
10861094
{
1087-
self.bind(py).as_any().call_method0(name).map(Into::into)
1095+
self.bind(py).as_any().call_method0(name).map(Bound::unbind)
10881096
}
10891097

10901098
/// Create a `Py<T>` instance by taking ownership of the given FFI pointer.
@@ -1292,7 +1300,7 @@ where
12921300
impl<T> std::convert::From<Bound<'_, T>> for Py<T> {
12931301
#[inline]
12941302
fn from(other: Bound<'_, T>) -> Self {
1295-
unsafe { Self::from_non_null(other.into_non_null()) }
1303+
other.unbind()
12961304
}
12971305
}
12981306

@@ -1618,7 +1626,7 @@ a = A()
16181626
.as_borrowed()
16191627
.to_owned();
16201628
let ptr = instance.as_ptr();
1621-
let instance: PyObject = instance.clone().into();
1629+
let instance: PyObject = instance.clone().unbind();
16221630
assert_eq!(instance.as_ptr(), ptr);
16231631
})
16241632
}

src/prelude.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ pub use crate::types::bytearray::PyByteArrayMethods;
3131
pub use crate::types::bytes::PyBytesMethods;
3232
pub use crate::types::dict::PyDictMethods;
3333
pub use crate::types::float::PyFloatMethods;
34+
pub use crate::types::frozenset::PyFrozenSetMethods;
3435
pub use crate::types::list::PyListMethods;
3536
pub use crate::types::mapping::PyMappingMethods;
3637
pub use crate::types::sequence::PySequenceMethods;
38+
pub use crate::types::set::PySetMethods;
3739
pub use crate::types::string::PyStringMethods;

src/types/dict.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -626,15 +626,6 @@ impl<'py> BoundDictIterator<'py> {
626626
}
627627
}
628628

629-
impl<'py> IntoIterator for &'_ Bound<'py, PyDict> {
630-
type Item = (Bound<'py, PyAny>, Bound<'py, PyAny>);
631-
type IntoIter = BoundDictIterator<'py>;
632-
633-
fn into_iter(self) -> Self::IntoIter {
634-
self.iter()
635-
}
636-
}
637-
638629
impl<'py> IntoIterator for Bound<'py, PyDict> {
639630
type Item = (Bound<'py, PyAny>, Bound<'py, PyAny>);
640631
type IntoIter = BoundDictIterator<'py>;

0 commit comments

Comments
 (0)