Skip to content

Commit

Permalink
add Bound::as_unbound
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Mar 20, 2024
1 parent caf80ec commit cacdaea
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,13 @@ impl<'py, T> Bound<'py, T> {
unsafe { Py::from_non_null(non_null) }
}

/// Removes the connection for this `Bound<T>` from the GIL, allowing
/// it to cross thread boundaries, without transferring ownership.
#[inline]
pub fn as_unbound(&self) -> &Py<T> {
&self.1
}

/// Casts this `Bound<T>` as the corresponding "GIL Ref" type.
///
/// This is a helper to be used for migration from the deprecated "GIL Refs" API.
Expand Down Expand Up @@ -521,11 +528,11 @@ impl<'py, T> Borrowed<'_, 'py, T> {
/// # fn main() -> PyResult<()> {
/// Python::with_gil(|py| -> PyResult<()> {
/// let tuple = PyTuple::new_bound(py, [1, 2, 3]);
///
///
/// // borrows from `tuple`, so can only be
/// // used while `tuple` stays alive
/// let borrowed = tuple.get_borrowed_item(0)?;
///
///
/// // creates a new owned reference, which
/// // can be used indendently of `tuple`
/// let bound = borrowed.to_owned();
Expand Down Expand Up @@ -1958,10 +1965,12 @@ impl PyObject {
#[cfg(test)]
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
mod tests {
use std::intrinsics::assert_inhabited;

use super::{Bound, Py, PyObject};
use crate::types::any::PyAnyMethods;
use crate::types::PyCapsule;
use crate::types::{dict::IntoPyDict, PyDict, PyString};
use crate::types::{PyCapsule, PyStringMethods};
use crate::{ffi, Borrowed, PyAny, PyNativeType, PyResult, Python, ToPyObject};

#[test]
Expand Down Expand Up @@ -2161,6 +2170,20 @@ a = A()
});
}

#[test]
fn test_bound_py_conversions() {
Python::with_gil(|py| {
let obj: Bound<'_, PyString> = PyString::new_bound(py, "hello world");
let obj_unbound: &Py<PyString> = obj.as_unbound();
let obj_unbound_rebound: &Bound<'_, PyString> = obj_unbound.bind(py);

let obj_unbound: Py<PyString> = obj.unbind();
let obj_bound: Bound<'_, PyString> = obj_unbound.into_bound(py);

assert_eq!(obj.to_str().unwrap(), "hello world");
});
}

#[test]
fn bound_from_borrowed_ptr_constructors() {
// More detailed tests of the underlying semantics in pycell.rs
Expand Down

0 comments on commit cacdaea

Please sign in to comment.