Skip to content

Commit

Permalink
Cow arguments (#13)
Browse files Browse the repository at this point in the history
* Change arguments to Cow to allow bytearray objects as arguments

* Changelog
  • Loading branch information
AngheloAlf committed Jan 19, 2024
1 parent a4996ff commit 7ac74e1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions lib/src/mio0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::decompress(bytes)?.into()))
pub(crate) fn decompress_mio0(bytes: Cow<[u8]>) -> Result<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::decompress(&bytes)?.into()))
}

#[pyfunction]
pub(crate) fn compress_mio0(bytes: &[u8]) -> Result<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::compress(bytes)?.into()))
pub(crate) fn compress_mio0(bytes: Cow<[u8]>) -> Result<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::compress(&bytes)?.into()))
}
}

Expand Down
15 changes: 11 additions & 4 deletions lib/src/yay0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::decompress(bytes)?.into()))
pub(crate) fn decompress_yay0(bytes: Cow<[u8]>) -> Result<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::decompress(&bytes)?.into()))
}

#[pyfunction]
pub(crate) fn compress_yay0(bytes: &[u8]) -> Result<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::compress(bytes)?.into()))
pub(crate) fn compress_yay0(bytes: Cow<[u8]>) -> Result<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::compress(&bytes)?.into()))
}
}

Expand Down
15 changes: 11 additions & 4 deletions lib/src/yaz0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::decompress(bytes)?.into()))
pub(crate) fn decompress_yaz0(bytes: Cow<[u8]>) -> Result<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::decompress(&bytes)?.into()))
}

#[pyfunction]
pub(crate) fn compress_yaz0(bytes: &[u8]) -> Result<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::compress(bytes)?.into()))
pub(crate) fn compress_yaz0(bytes: Cow<[u8]>) -> Result<Cow<[u8]>, super::Crunch64Error> {
Ok(Cow::Owned(super::compress(&bytes)?.into()))
}
}

Expand Down

0 comments on commit 7ac74e1

Please sign in to comment.