Reader Writer Builder
Introduction
This crate provides a convenient way to build std::io::Readers and std::io::Writers by chaining transformations. Since readers and writers are defined simultaneously through the same builder they can be used as inverses of each other.
Usage
Add this to your Cargo.toml:
[]
= "0.2.0"
Warning
This crate is provided "as is" without warranty of any kind. Furthermore, this crate is still in its infancy and lacks significant real world exposure. We try our best to ensure the quality of this crate, but we should warn you that it's more a proof of concept than something to be used in production at the moment.
Example
Let's say you have some application state you want to encrypt and store on disk. Once the application starts up you want to read that state back into memory. A good practice when encrypting is to compress the data beforehand so you may feel the desire to chain some readers and writers together.
use Compression;
use ;
use ;
The builder ensures the order of the readers will match the order of the writers so there's no opportunity for mistakes.
Writing something similar in the usual way is much more verbose and error prone.
use Result;
use ;
use ;
use ;
This second example doesn't support streaming, which is necessary in the case of large files. Notice how the save and load functionality is described in reverse order from each other and that configuration options like the file location and the encryption key need to be made available in multiple locations. This is needlessly challenging to maintain when compared to the former example.
Sources and Sinks
You may have noticed that the FileBuilder struct and the wincode function have a special role. They are examples of a source and a sink respectively. Sources are a typical starting point for chaining builders, since they can be constructed without an inner builder. Sinks are a typical ending point for chaining builders, since they can interface with other types than &[u8] which Read and Write are restricted to.
Features
To provide the functionality of many different readers and writers this crate has many optional dependencies which are enabled through a predefined set of features. The example above requires the wincode, chacha20 and flate2 features. Currently, the following features are available:
wincode: includes theserdeandwincodecrates and enables thewincodesink on theRwBuilderExttrait.rmp_serde: includes theserdeandrmp-serdecrates and enables thermp_serdeMessagePack sink on theRwBuilderExttrait.flate2: includes theflate2crate and enables thecrc,deflate,gzandzlibcompressions.zstd: includes thezstdcrate and enableszstdcompression.bzip2: includes thebzip2crate and enablesbzip2compression.lz4_flex: includes thelz4_flexcrate and enableslz4_flexcompression.chacha20: includes thecipherandchacha20crates and enableschacha20symmetric encryption.salsa20: includes thecipherandsalsa20crates and enablessalsa20symmetric encryption.aes_ctr: includes theaesandctrcrates and enablesaes128_ctrandaes256_ctrsymmetric encryption.digest: includes thedigestcrate and enables the generichashchecksumming combinator.sha2: includes thesha2crate and enablessha256andsha512hashing.crc32fast: includes thecrc32fastcrate and enablescrc32fastchecksumming.base64: includes thebase64crate and enables thebase64encoding/decoding modifier on theRwBuilderExttrait.serde_json: includes theserdeandserde_jsoncrates and enables theserde_jsonJSON sink on theRwBuilderExttrait.postcard: includes theserdeandpostcardcrates and enables thepostcardbinary sink on theRwBuilderExttrait.
Development Environment
Although not required, it is recommended to use the provided Nix flake to set up your development environment. This avoids polluting your host machine and ensures you are using the correct version of Rust and other dependencies.
To activate the environment:
Alternatively, you can build the project directly with:
Examples & Integration Tests
The tests/ directory contains highly-documented integration tests that demonstrate how to construct complex pipelines for real-world scenarios. We encourage developers to look at these tests to understand how to use this crate:
tests/secure_archival.rs: Demonstrates chainingFile->AES-256->Deflate->Postcardto securely archive sensitive Rust structures directly to disk.tests/json_streaming.rs: Demonstrates base64 encoding a JSON stream on the fly.tests/combinations.rs: Showcases complex combinations like roundtripping, dealing with sink limitations, and properCompression/Encryptionordering.
You can run these integration tests natively via cargo:
Benchmarks
Because rw-builder constructs a transparent proxy of std::io readers and writers, the overhead of the builder pattern is practically zero.
A criterion benchmark suite is included in benches/rw_benchmark.rs to measure the overhead of chaining transformations.
Hardware Details:
- CPU: AMD Ryzen 9 3900X 12-Core Processor
- RAM: 32 GB
- OS: Linux
Throughput (1MB Payload):
- Raw
Vec::write_all: ~22.5µs (43.2 GiB/s) VecBuilder: ~20.9µs (46.5 GiB/s) (Zero overhead)- Raw
DeflateEncoder: ~137.1µs (7.12 GiB/s) Deflatevia Builder: ~145.0µs (6.73 GiB/s) (< 5% overhead)
You can run the benchmarks yourself with:
Contributing
See CONTRIBUTING.md.
License
The rw-builder crate is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.
See LICENSE-APACHE.txt, LICENSE-MIT.txt, and COPYRIGHT.txt for details.