2.2 KiB
permalink | title | published_date | layout | data | tags | |||
---|---|---|---|---|---|---|---|---|
/{{ year }}/{{ month }}/{{ day }}/a-rust-library-for-liblzf | A Rust library for LibLZF | 2014-10-15 11:10:00 +0200 | post.liquid |
|
|
In the last four months I did not produce much open-source code. I was busy writing my Bachelor Thesis. But I was active in the community, I attended several conferences, I read a lot of stuff and I wrote down a lot more things to do.
One of these things was "learn Rust", as you might figure (and writing more). Well, I submitted my thesis on Monday and went on to write some actual Rust code.
So I created lzf-rs, a wrapper around LibLZF, a super small and fast compression library, originally written by Marc Lehmann. LibLZF consists of just 4 files and is super-easy to compile as a small library. LibLZF itself is used by Redis for compressing strings in the RDB, that's how I got to know it. I still don't know how it works, but I want to learn it now (and maybe port the whole code to Rust instead of using a wrapper around the C library?)
I used Rust's Foreign Function Interface to write the wrapper. Accessing C code from within Rust is quite simple. The hard part is choosing the right data types and writing the API in a safe and usable way. I'm not 100% happy with the current API of lzf-rs, so expect it to change. I'm still in the process of figuring out what to expose and what not.
And now, enough of the talking. This is how to use the library:
use lzf;
fn main() {
let data = "foobar";
let compressed = lzf::compress(data.as_bytes()).unwrap();
let decompressed = lzf::decompress(compressed.as_slice(), data.len()).unwrap();
}
I'm happy to get some feedback. It's my first serious Rust code, so prone to bugs and maybe not to Rusty afterall. Ping me on Twitter, drop me an email or open an issue.