Skip to content

Commit

Permalink
Add TryFrom impls for PyByteArray and PyMemoryView
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Oct 15, 2023
1 parent 5b94241 commit d4ed66f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/types/bytearray.rs
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/types/memoryview.rs
Expand Up @@ -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)
}
}

0 comments on commit d4ed66f

Please sign in to comment.