Skip to content

Commit

Permalink
Merge pull request #3675 from davidhewitt/dict2-try2
Browse files Browse the repository at this point in the history
implement `PyDictMethods`
  • Loading branch information
davidhewitt committed Dec 20, 2023
2 parents fd2fc98 + 15d309e commit 8bd2972
Show file tree
Hide file tree
Showing 5 changed files with 373 additions and 118 deletions.
8 changes: 8 additions & 0 deletions src/ffi_ptr_ext.rs
Expand Up @@ -35,6 +35,9 @@ pub(crate) trait FfiPtrExt: Sealed {

/// Same as `assume_borrowed_or_err`, but panics on NULL.
unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Py2Borrowed<'a, '_, PyAny>;

/// Same as `assume_borrowed_or_err`, but does not check for NULL.
unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Py2Borrowed<'a, '_, PyAny>;
}

impl FfiPtrExt for *mut ffi::PyObject {
Expand Down Expand Up @@ -68,4 +71,9 @@ impl FfiPtrExt for *mut ffi::PyObject {
unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Py2Borrowed<'a, '_, PyAny> {
Py2Borrowed::from_ptr(py, self)
}

#[inline]
unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Py2Borrowed<'a, '_, PyAny> {
Py2Borrowed::from_ptr_unchecked(py, self)
}
}
8 changes: 8 additions & 0 deletions src/instance.rs
Expand Up @@ -262,6 +262,14 @@ impl<'a, 'py> Py2Borrowed<'a, 'py, PyAny> {
py,
)
}

/// # Safety
/// This is similar to `std::slice::from_raw_parts`, the lifetime `'a` is completely defined by
/// the caller and it's the caller's responsibility to ensure that the reference this is
/// derived from is valid for the lifetime `'a`.
pub(crate) unsafe fn from_ptr_unchecked(py: Python<'py>, ptr: *mut ffi::PyObject) -> Self {
Self(NonNull::new_unchecked(ptr), PhantomData, py)
}
}

impl<'a, 'py, T> From<&'a Py2<'py, T>> for Py2Borrowed<'a, 'py, T> {
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Expand Up @@ -30,5 +30,6 @@ pub use crate::wrap_pyfunction;
// pub(crate) use crate::types::boolobject::PyBoolMethods;
// pub(crate) use crate::types::bytearray::PyByteArrayMethods;
// pub(crate) use crate::types::bytes::PyBytesMethods;
// pub(crate) use crate::types::dict::PyDictMethods;
// pub(crate) use crate::types::float::PyFloatMethods;
// pub(crate) use crate::types::sequence::PySequenceMethods;

0 comments on commit 8bd2972

Please sign in to comment.