Crates.io | bswp |
lib.rs | bswp |
version | 1.0.0 |
source | src |
created_at | 2020-03-24 18:01:48.855045 |
updated_at | 2020-03-27 18:50:46.20589 |
description | byte swap lib |
homepage | |
repository | https://github.com/PicoJr/bswp |
max_upload_size | |
id | 222374 |
size | 27,616 |
Branch | Status |
---|---|
master |
Rust Byte Swap lib.
Swap bytes using patterns and masks.
bswp
requires Rust 1.40.0.
use bswp::pattern::{Pattern, Predicate, swap_iter};
let pattern = Pattern::new(0x42).with_mask(0xFF); // replace byte by 0x42
let predicate = Predicate::new().with_periodicity(2).with_offset(1); // replace odd bytes
let swaps = &[(pattern, predicate)];
let source: [u8; 4] = [0x41, 0x41, 0x41, 0x41];
let swapped = swap_iter(&source, swaps); // iterator on result
let swapped: Vec<u8> = swapped.collect();
assert_eq!(swapped, vec!(0x41, 0x42, 0x41, 0x42));
use std::io::Cursor;
use bswp::pattern::{Pattern, Predicate};
use bswp::io::swap_io;
// in memory reader (implements `Read`)
let mut reader: Cursor<Vec<u8>> = Cursor::new(vec![0x41, 0x42, 0x43, 0x44]);
// in memory writer (implements `Write`)
let mut writer: Cursor<Vec<u8>> = Cursor::new(Vec::new());
let swaps: &[(Pattern, Predicate)] = &[(Pattern::new(0x42).with_mask(0xFF), Predicate::new().with_periodicity(2).with_offset(0))];
let swap = swap_io(&mut reader, &mut writer, swaps);
assert!(swap.is_ok());
assert_eq!(swap.unwrap(), 4); // 4 bytes written
assert_eq!(writer.into_inner(), vec![0x42, 0x42, 0x42, 0x44])
Please see the CHANGELOG for a release history.