From d4ed66fff0acbcf115a6511b6258cf1c6b1fc706 Mon Sep 17 00:00:00 2001 From: messense Date: Sun, 15 Oct 2023 17:39:01 +0800 Subject: [PATCH] Add `TryFrom` impls for `PyByteArray` and `PyMemoryView` --- src/types/bytearray.rs | 10 ++++++++++ src/types/memoryview.rs | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/types/bytearray.rs b/src/types/bytearray.rs index 46c31ba4..d68a7e73 100644 --- a/src/types/bytearray.rs +++ b/src/types/bytearray.rs @@ -241,6 +241,16 @@ impl PyByteArray { } } +impl<'py> TryFrom<&'py PyAny> for &'py PyByteArray { + type Error = crate::PyErr; + + /// Creates a new Python `bytearray` object from another Python object that + /// implements the buffer protocol. + fn try_from(value: &'py PyAny) -> Result { + PyByteArray::from(value) + } +} + #[cfg(test)] mod tests { use crate::exceptions; diff --git a/src/types/memoryview.rs b/src/types/memoryview.rs index 09ed9639..0d115540 100644 --- a/src/types/memoryview.rs +++ b/src/types/memoryview.rs @@ -17,3 +17,13 @@ impl PyMemoryView { } } } + +impl<'py> TryFrom<&'py PyAny> for &'py PyMemoryView { + type Error = crate::PyErr; + + /// Creates a new Python `memoryview` object from another Python object that + /// implements the buffer protocol. + fn try_from(value: &'py PyAny) -> Result { + PyMemoryView::from(value) + } +}