From 7ac74e115b5362289a3e868aa75ee255ae86f58d Mon Sep 17 00:00:00 2001 From: Anghelo Carvajal Date: Fri, 19 Jan 2024 00:26:44 -0300 Subject: [PATCH] Cow arguments (#13) * Change arguments to Cow to allow bytearray objects as arguments * Changelog --- CHANGELOG.md | 4 ++++ lib/src/mio0.rs | 15 +++++++++++---- lib/src/yay0.rs | 15 +++++++++++---- lib/src/yaz0.rs | 15 +++++++++++---- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a8effa..dfee400 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - A few code cleanups. +### Fixed + +- Functions not accepting `bytearray` objects. + ## [0.2.0] - 2023-12-28 ### Added diff --git a/lib/src/mio0.rs b/lib/src/mio0.rs index 0ab1076..b514fd9 100644 --- a/lib/src/mio0.rs +++ b/lib/src/mio0.rs @@ -287,14 +287,21 @@ pub(crate) mod python_bindings { use pyo3::prelude::*; use std::borrow::Cow; + /** + * We use a `Cow` instead of a plain &[u8] because the latter only allows Python's + * `bytes` objects, while `Cow`` allows for both `bytes` and `bytearray`. + * This is important because an argument typed as `bytes` allows to pass a + * `bytearray` object too. + */ + #[pyfunction] - pub(crate) fn decompress_mio0(bytes: &[u8]) -> Result, super::Crunch64Error> { - Ok(Cow::Owned(super::decompress(bytes)?.into())) + pub(crate) fn decompress_mio0(bytes: Cow<[u8]>) -> Result, super::Crunch64Error> { + Ok(Cow::Owned(super::decompress(&bytes)?.into())) } #[pyfunction] - pub(crate) fn compress_mio0(bytes: &[u8]) -> Result, super::Crunch64Error> { - Ok(Cow::Owned(super::compress(bytes)?.into())) + pub(crate) fn compress_mio0(bytes: Cow<[u8]>) -> Result, super::Crunch64Error> { + Ok(Cow::Owned(super::compress(&bytes)?.into())) } } diff --git a/lib/src/yay0.rs b/lib/src/yay0.rs index 359f7d6..901e1a2 100644 --- a/lib/src/yay0.rs +++ b/lib/src/yay0.rs @@ -300,14 +300,21 @@ pub(crate) mod python_bindings { use pyo3::prelude::*; use std::borrow::Cow; + /** + * We use a `Cow` instead of a plain &[u8] because the latter only allows Python's + * `bytes` objects, while `Cow`` allows for both `bytes` and `bytearray`. + * This is important because an argument typed as `bytes` allows to pass a + * `bytearray` object too. + */ + #[pyfunction] - pub(crate) fn decompress_yay0(bytes: &[u8]) -> Result, super::Crunch64Error> { - Ok(Cow::Owned(super::decompress(bytes)?.into())) + pub(crate) fn decompress_yay0(bytes: Cow<[u8]>) -> Result, super::Crunch64Error> { + Ok(Cow::Owned(super::decompress(&bytes)?.into())) } #[pyfunction] - pub(crate) fn compress_yay0(bytes: &[u8]) -> Result, super::Crunch64Error> { - Ok(Cow::Owned(super::compress(bytes)?.into())) + pub(crate) fn compress_yay0(bytes: Cow<[u8]>) -> Result, super::Crunch64Error> { + Ok(Cow::Owned(super::compress(&bytes)?.into())) } } diff --git a/lib/src/yaz0.rs b/lib/src/yaz0.rs index 6f0f284..b813641 100644 --- a/lib/src/yaz0.rs +++ b/lib/src/yaz0.rs @@ -290,14 +290,21 @@ pub(crate) mod python_bindings { use pyo3::prelude::*; use std::borrow::Cow; + /** + * We use a `Cow` instead of a plain &[u8] because the latter only allows Python's + * `bytes` objects, while `Cow`` allows for both `bytes` and `bytearray`. + * This is important because an argument typed as `bytes` allows to pass a + * `bytearray` object too. + */ + #[pyfunction] - pub(crate) fn decompress_yaz0(bytes: &[u8]) -> Result, super::Crunch64Error> { - Ok(Cow::Owned(super::decompress(bytes)?.into())) + pub(crate) fn decompress_yaz0(bytes: Cow<[u8]>) -> Result, super::Crunch64Error> { + Ok(Cow::Owned(super::decompress(&bytes)?.into())) } #[pyfunction] - pub(crate) fn compress_yaz0(bytes: &[u8]) -> Result, super::Crunch64Error> { - Ok(Cow::Owned(super::compress(bytes)?.into())) + pub(crate) fn compress_yaz0(bytes: Cow<[u8]>) -> Result, super::Crunch64Error> { + Ok(Cow::Owned(super::compress(&bytes)?.into())) } }