1 个不稳定版本
0.1.0 | 2019年4月29日 |
---|
#14 in #bit-field
47KB
1K SLoC
bin_codec.rs
Rust 的二进制编解码宏
欢迎任何建议和拉取请求
目前仅支持结构体编解码!
并且不稳定或不安全使用!
字段属性
- bits:位大小定义
- is_some:用于
Option
字段 - count:用于
Vec
字段 - has_next:用于
Vec
字段
#[bin(bits({num}))]
- 在编码模式和解码模式中的效果
use bin_codec::*;
use bin_codec_derive::{BinEncodeBe, BinDecodeBe};
#[test]
fn test_has_bit_field() {
#[derive(BinEncodeBe)]
struct Struct<T> where T: Encode {
#[bin(bits(24))]
a_field: T,
#[bin(bits(16))]
b_field: T,
}
let s = Struct::<_> {
a_field: 0x345678_i32,
b_field: 0x3344,
};
let mut target = [0u8; 5];
s.encode(&mut target, 0, &mut ()).unwrap();
assert_eq!(&[0x34, 0x56, 0x78, 0x33, 0x44], &target[..]);
let mut target = [0u8; 5];
s.encode(&mut target, 0, &mut()).unwrap();
println!("{:#02X?}", target);
assert_eq!(&[0x78, 0x56, 0x34, 0x44, 0x33], &target[..]);
}
#[bin(is_some({code})]
- 在解码模式中的效果
- 与 Option 字段的效果
Option 字段示例
use bin_codec::*;
use bin_codec_derive::{BinEncodeBe, BinDecodeBe};
#[test]
fn test_option() {
#[derive(BinEncodeBe, BinDecodeBe)]
struct Struct {
a_field: i32,
#[bin(is_some("a_field == 0"))]
b_field: Option<i32>,
}
let target = [0x12,0x34,0x56,0x78];
let (s, size) = Struct::decode(&target, 0, &mut Context::default()).unwrap();
assert_eq!(size, 32);
assert_eq!(s.a_field, 0x12345678);
assert_eq!(None, s.b_field);
//
let target = [0,0,0,0,0x12,0x34,0x56,0x78];
let (s, size) = Struct::decode(&target, 0, &mut Context::default()).unwrap();
assert_eq!(size, 64);
assert_eq!(s.a_field, 0);
assert_eq!(Some(0x12345678), s.b_field);
}
Vec 字段示例
use bin_codec::*;
use bin_codec_derive::{BinEncodeBe, BinDecodeBe};
#[test]
fn test_vec() {
#[derive(BinDecodeBe, Debug)]
#[bin(has_next(value != 0))]
struct Item {
value: u8,
}
#[derive(BinDecodeBe, Debug)]
struct Struct {
a_field: u8,
#[bin(is_some(a_field == 0))]
b_field: Vec<Item>,
}
let (s, size) = Struct::decode(&[0, 1, 2, 0, 1], 0, &mut Context::default()).unwrap();
assert_eq!(0, s.a_field);
assert_eq!(3, s.b_field.len());
}
#[bin(count({code}))]
- 在解码模式中的效果
- 与 Vec 字段的效果
use bin_codec::*;
use bin_codec_derive::{BinEncodeBe, BinDecodeBe};
#[test]
fn test_bits_on_vec() {
#[derive(BinDecodeBe, BinEncodeBe)]
struct S {
count: u8,
#[bin(bits(16))]
#[bin(count("count as usize"))]
values: Vec<u32>,
}
let (s, size) = S::decode(&[3,1,2,3,4,5,6], 0, &mut Context::default()).unwrap();
assert_eq!(size, 56);
assert_eq!(s.count, 3);
assert_eq!(&[0x0102, 0x0304, 0x0506], s.values.as_slice());
//
let mut target = [0u8; 7];
let size = s.encode(&mut target, 0, &mut Context::default()).unwrap();
assert_eq!(size, 56);
assert_eq!(&[3,1,2,3,4,5,6], &target);
//
let (s, size) = S::decode(&[3,1,2,3,4,5,6], 0, &mut Context::default()).unwrap();
assert_eq!(size, 56);
assert_eq!(s.count, 3);
assert_eq!(&[0x201, 0x403, 0x605], s.values.as_slice());
//
let mut target = [0u8; 7];
let size = s.encode(&mut target, 0, &mut Context::default()).unwrap();
assert_eq!(size, 56);
assert_eq!(&[3,1,2,3,4,5,6], &target);
}
许可证
本项目许可在以下任一许可下:
- Apache 许可证 2.0 版,(LICENSE-APACHE 或 http://www.apache.org/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
任由您选择。
依赖
~2MB
~46K SLoC