Skip to content

Commit

Permalink
introduce Bound::as_borrowed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 26, 2023
1 parent e9dd521 commit b64c3c6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
15 changes: 10 additions & 5 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ impl<'py, T> Bound<'py, T> {
self.into_non_null().as_ptr()
}

/// Casts this `Bound<T>` to a `Borrowed<T>` smart pointer.
pub fn as_borrowed<'a>(&'a self) -> Borrowed<'a, 'py, T> {
Borrowed(
unsafe { NonNull::new_unchecked(self.as_ptr()) },
PhantomData,
self.py(),
)
}

/// 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 @@ -300,11 +309,7 @@ impl<'a, 'py> Borrowed<'a, 'py, PyAny> {
impl<'a, 'py, T> From<&'a Bound<'py, T>> for Borrowed<'a, 'py, T> {
/// Create borrow on a Bound
fn from(instance: &'a Bound<'py, T>) -> Self {
Self(
unsafe { NonNull::new_unchecked(instance.as_ptr()) },
PhantomData,
instance.py(),
)
instance.as_borrowed()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/types/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,16 +400,16 @@ impl<'py> PyByteArrayMethods<'py> for Bound<'py, PyByteArray> {
}

fn data(&self) -> *mut u8 {
Borrowed::from(self).data()
self.as_borrowed().data()
}

unsafe fn as_bytes(&self) -> &[u8] {
Borrowed::from(self).as_bytes()
self.as_borrowed().as_bytes()
}

#[allow(clippy::mut_from_ref)]
unsafe fn as_bytes_mut(&self) -> &mut [u8] {
Borrowed::from(self).as_bytes_mut()
self.as_borrowed().as_bytes_mut()
}

fn to_vec(&self) -> Vec<u8> {
Expand Down
2 changes: 1 addition & 1 deletion src/types/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub trait PyBytesMethods<'py> {
impl<'py> PyBytesMethods<'py> for Bound<'py, PyBytes> {
#[inline]
fn as_bytes(&self) -> &[u8] {
Borrowed::from(self).as_bytes()
self.as_borrowed().as_bytes()
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,20 @@ pub trait PyStringMethods<'py> {
impl<'py> PyStringMethods<'py> for Bound<'py, PyString> {
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
fn to_str(&self) -> PyResult<&str> {
Borrowed::from(self).to_str()
self.as_borrowed().to_str()
}

fn to_cow(&self) -> PyResult<Cow<'_, str>> {
Borrowed::from(self).to_cow()
self.as_borrowed().to_cow()
}

fn to_string_lossy(&self) -> Cow<'_, str> {
Borrowed::from(self).to_string_lossy()
self.as_borrowed().to_string_lossy()
}

#[cfg(not(Py_LIMITED_API))]
unsafe fn data(&self) -> PyResult<PyStringData<'_>> {
Borrowed::from(self).data()
self.as_borrowed().data()
}
}

Expand Down

0 comments on commit b64c3c6

Please sign in to comment.