Skip to content

Commit

Permalink
Update lz4_flex crate to v0.11 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
nestor-sk authored Aug 8, 2024
1 parent 3723246 commit b6e5a4b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,23 @@ NIMBLELZ4_FORCE_BUILD=true mix deps.compile

You can compress and decompress data.

[Block format](https://github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md):

```elixir
iex> uncompressed = :crypto.strong_rand_bytes(10)
iex> compressed = NimbleLZ4.compress(uncompressed)
iex> {:ok, ^uncompressed} = NimbleLZ4.decompress(compressed, _uncompressed_size = 10)
true
```

[Frame format](https://github.com/lz4/lz4/blob/dev/doc/lz4_Frame_format.md) ([self-contained](https://android.googlesource.com/platform/external/lz4/+/HEAD/doc/lz4_Block_format.md#metadata)):

```elixir
iex> uncompressed = :crypto.strong_rand_bytes(10_000)
iex> compressed = NimbleLZ4.compress_frame(uncompressed)
iex> {:ok, ^uncompressed} = NimbleLZ4.decompress_frame(compressed)
true
```

[LZ4]: https://github.com/lz4/lz4
[RustlerPrecompiled]: https://github.com/philss/rustler_precompiled
4 changes: 2 additions & 2 deletions native/nimblelz4/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native/nimblelz4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ rustler = { version = "0.29", default-features = false, features = [
"derive",
"nif_version_2_15",
] }
lz4_flex = { version = "0.9.0" }
lz4_flex = { version = "0.11.0" }
4 changes: 2 additions & 2 deletions native/nimblelz4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustler::{Encoder, Env, Error, Term};
#[rustler::nif(schedule = "DirtyCpu")]
fn compress<'a>(env: Env<'a>, iolist_to_compress: Term<'a>) -> Result<Term<'a>, Error> {
let binary_to_compress: Binary = Binary::from_iolist(iolist_to_compress).unwrap();
let compressed_slice = lz4_flex::compress(binary_to_compress.as_slice());
let compressed_slice = lz4_flex::block::compress(binary_to_compress.as_slice());

let mut erl_bin: OwnedBinary = OwnedBinary::new(compressed_slice.len()).unwrap();

Expand Down Expand Up @@ -42,7 +42,7 @@ fn decompress<'a>(
binary_to_decompress: Binary,
uncompressed_size: usize,
) -> Result<Term<'a>, Error> {
match lz4_flex::decompress(binary_to_decompress.as_slice(), uncompressed_size) {
match lz4_flex::block::decompress(binary_to_decompress.as_slice(), uncompressed_size) {
Ok(decompressed_vec) => {
let mut erl_bin: OwnedBinary = OwnedBinary::new(decompressed_vec.len()).unwrap();
erl_bin
Expand Down
8 changes: 5 additions & 3 deletions test/nimble_lz4_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule NimbleLZ4Test do
end

test "with iodata" do
assert NimbleLZ4.compress_frame([]) == ""
assert NimbleLZ4.compress_frame([]) == "\x04\"M\x18`@\x82\0\0\0\0"

assert NimbleLZ4.compress_frame([?f, [[[?o]]], "o"]) ==
"\x04\"M\x18`@\x82\x03\0\0\x80foo\0\0\0\0"
Expand All @@ -67,8 +67,10 @@ defmodule NimbleLZ4Test do
end

test "with the wrong uncompressed size" do
assert {:error, message} = NimbleLZ4.decompress(NimbleLZ4.compress("foo"), 6)
assert message == "the expected decompressed size differs, actual 3, expected 6"
assert {:error, message} = NimbleLZ4.decompress(NimbleLZ4.compress("foo"), 2)

assert message ==
"provided output is too small for the decompressed data, actual 2, expected 3"
end
end

Expand Down

0 comments on commit b6e5a4b

Please sign in to comment.