Add TryFrom impls for PyByteArray and PyMemoryView

This commit is contained in:
messense 2023-10-15 17:39:01 +08:00
parent 5b94241bd7
commit d4ed66fff0
No known key found for this signature in database
GPG key ID: BB41A8A2C716CCA9
2 changed files with 20 additions and 0 deletions

View file

@ -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<Self, Self::Error> {
PyByteArray::from(value)
}
}
#[cfg(test)]
mod tests {
use crate::exceptions;

View file

@ -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<Self, Self::Error> {
PyMemoryView::from(value)
}
}