Skip to content

Commit 442d13d

Browse files
committed
introduce Bound::unbind
1 parent e4fd557 commit 442d13d

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
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/types/set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl PySet {
9494

9595
/// Removes and returns an arbitrary element from the set.
9696
pub fn pop(&self) -> Option<PyObject> {
97-
self.as_borrowed().pop().map(Py::from)
97+
self.as_borrowed().pop().map(Bound::unbind)
9898
}
9999

100100
/// Returns an iterator of values in this set.

src/types/string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,13 @@ impl Py<PyString> {
435435

436436
impl IntoPy<Py<PyString>> for Bound<'_, PyString> {
437437
fn into_py(self, _py: Python<'_>) -> Py<PyString> {
438-
self.into()
438+
self.unbind()
439439
}
440440
}
441441

442442
impl IntoPy<Py<PyString>> for &Bound<'_, PyString> {
443443
fn into_py(self, _py: Python<'_>) -> Py<PyString> {
444-
self.clone().into()
444+
self.clone().unbind()
445445
}
446446
}
447447

0 commit comments

Comments
 (0)