From 320b9362d1d2edc3b83e88582fb2646e7f362349 Mon Sep 17 00:00:00 2001 From: Zane Hannan Date: Fri, 13 Aug 2021 19:25:14 +1000 Subject: [PATCH 1/3] run cargo fmt --- benches/bench.rs | 102 +++++++-- benches/clone.rs | 96 ++++++-- benches/recycler.rs | 91 ++++++-- benches/serde.rs | 540 ++++++++++++++++++++++---------------------- src/abomonated.rs | 19 +- src/lib.rs | 222 ++++++++++-------- tests/tests.rs | 216 +++++++++++++----- 7 files changed, 798 insertions(+), 488 deletions(-) diff --git a/benches/bench.rs b/benches/bench.rs index 9ce8a3c..fc3e019 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -6,49 +6,107 @@ extern crate test; use abomonation::*; use test::Bencher; -#[bench] fn empty_enc(bencher: &mut Bencher) { _bench_enc(bencher, vec![(); 1024]); } -#[bench] fn empty_dec(bencher: &mut Bencher) { _bench_dec(bencher, vec![(); 1024]); } +#[bench] +fn empty_enc(bencher: &mut Bencher) { + _bench_enc(bencher, vec![(); 1024]); +} +#[bench] +fn empty_dec(bencher: &mut Bencher) { + _bench_dec(bencher, vec![(); 1024]); +} -#[bench] fn u64_enc(bencher: &mut Bencher) { _bench_enc(bencher, vec![0u64; 1024]); } -#[bench] fn u64_dec(bencher: &mut Bencher) { _bench_dec(bencher, vec![0u64; 1024]); } +#[bench] +fn u64_enc(bencher: &mut Bencher) { + _bench_enc(bencher, vec![0u64; 1024]); +} +#[bench] +fn u64_dec(bencher: &mut Bencher) { + _bench_dec(bencher, vec![0u64; 1024]); +} -#[bench] fn u32x2_enc(bencher: &mut Bencher) { _bench_enc(bencher, vec![(0u32,0u32); 1024]); } -#[bench] fn u32x2_dec(bencher: &mut Bencher) { _bench_dec(bencher, vec![(0u32,0u32); 1024]); } +#[bench] +fn u32x2_enc(bencher: &mut Bencher) { + _bench_enc(bencher, vec![(0u32, 0u32); 1024]); +} +#[bench] +fn u32x2_dec(bencher: &mut Bencher) { + _bench_dec(bencher, vec![(0u32, 0u32); 1024]); +} -#[bench] fn u8_u64_enc(bencher: &mut Bencher) { _bench_enc(bencher, vec![(0u8, 0u64); 512]); } -#[bench] fn u8_u64_dec(bencher: &mut Bencher) { _bench_dec(bencher, vec![(0u8, 0u64); 512]); } +#[bench] +fn u8_u64_enc(bencher: &mut Bencher) { + _bench_enc(bencher, vec![(0u8, 0u64); 512]); +} +#[bench] +fn u8_u64_dec(bencher: &mut Bencher) { + _bench_dec(bencher, vec![(0u8, 0u64); 512]); +} -#[bench] fn string10_enc(bencher: &mut Bencher) { _bench_enc(bencher, vec![format!("grawwwwrr!"); 1024]); } -#[bench] fn string10_dec(bencher: &mut Bencher) { _bench_dec(bencher, vec![format!("grawwwwrr!"); 1024]); } +#[bench] +fn string10_enc(bencher: &mut Bencher) { + _bench_enc(bencher, vec![format!("grawwwwrr!"); 1024]); +} +#[bench] +fn string10_dec(bencher: &mut Bencher) { + _bench_dec(bencher, vec![format!("grawwwwrr!"); 1024]); +} -#[bench] fn string20_enc(bencher: &mut Bencher) { _bench_enc(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); } -#[bench] fn string20_dec(bencher: &mut Bencher) { _bench_dec(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); } +#[bench] +fn string20_enc(bencher: &mut Bencher) { + _bench_enc(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); +} +#[bench] +fn string20_dec(bencher: &mut Bencher) { + _bench_dec(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); +} -#[bench] fn vec_u_s_enc(bencher: &mut Bencher) { _bench_enc(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); } -#[bench] fn vec_u_s_dec(bencher: &mut Bencher) { _bench_dec(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); } +#[bench] +fn vec_u_s_enc(bencher: &mut Bencher) { + _bench_enc(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); +} +#[bench] +fn vec_u_s_dec(bencher: &mut Bencher) { + _bench_dec(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); +} -#[bench] fn vec_u_vn_s_enc(bencher: &mut Bencher) { _bench_enc(bencher, vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32]); } -#[bench] fn vec_u_vn_s_dec(bencher: &mut Bencher) { _bench_dec(bencher, vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32]); } +#[bench] +fn vec_u_vn_s_enc(bencher: &mut Bencher) { + _bench_enc( + bencher, + vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32], + ); +} +#[bench] +fn vec_u_vn_s_dec(bencher: &mut Bencher) { + _bench_dec( + bencher, + vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32], + ); +} fn _bench_enc(bencher: &mut Bencher, record: T) { - // prepare encoded data for bencher.bytes let mut bytes = Vec::new(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } // repeatedly encode this many bytes bencher.bytes = bytes.len() as u64; bencher.iter(|| { bytes.clear(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } }); } -fn _bench_dec(bencher: &mut Bencher, record: T) { - +fn _bench_dec(bencher: &mut Bencher, record: T) { // prepare encoded data let mut bytes = Vec::new(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } // repeatedly decode (and validate) bencher.bytes = bytes.len() as u64; diff --git a/benches/clone.rs b/benches/clone.rs index a040895..e1f82ba 100644 --- a/benches/clone.rs +++ b/benches/clone.rs @@ -6,51 +6,101 @@ extern crate test; use abomonation::*; use test::Bencher; -#[bench] fn empty_e_d(bencher: &mut Bencher) { _bench_e_d(bencher, vec![(); 1024]); } -#[bench] fn empty_cln(bencher: &mut Bencher) { _bench_cln(bencher, vec![(); 1024]); } +#[bench] +fn empty_e_d(bencher: &mut Bencher) { + _bench_e_d(bencher, vec![(); 1024]); +} +#[bench] +fn empty_cln(bencher: &mut Bencher) { + _bench_cln(bencher, vec![(); 1024]); +} -#[bench] fn u64_e_d(bencher: &mut Bencher) { _bench_e_d(bencher, vec![0u64; 1024]); } -#[bench] fn u64_cln(bencher: &mut Bencher) { _bench_cln(bencher, vec![0u64; 1024]); } +#[bench] +fn u64_e_d(bencher: &mut Bencher) { + _bench_e_d(bencher, vec![0u64; 1024]); +} +#[bench] +fn u64_cln(bencher: &mut Bencher) { + _bench_cln(bencher, vec![0u64; 1024]); +} -#[bench] fn u8_u64_e_d(bencher: &mut Bencher) { _bench_e_d(bencher, vec![(0u8, 0u64); 512]); } -#[bench] fn u8_u64_cln(bencher: &mut Bencher) { _bench_cln(bencher, vec![(0u8, 0u64); 512]); } +#[bench] +fn u8_u64_e_d(bencher: &mut Bencher) { + _bench_e_d(bencher, vec![(0u8, 0u64); 512]); +} +#[bench] +fn u8_u64_cln(bencher: &mut Bencher) { + _bench_cln(bencher, vec![(0u8, 0u64); 512]); +} -#[bench] fn string10_e_d(bencher: &mut Bencher) { _bench_e_d(bencher, vec![format!("grawwwwrr!"); 1024]); } -#[bench] fn string10_cln(bencher: &mut Bencher) { _bench_cln(bencher, vec![format!("grawwwwrr!"); 1024]); } +#[bench] +fn string10_e_d(bencher: &mut Bencher) { + _bench_e_d(bencher, vec![format!("grawwwwrr!"); 1024]); +} +#[bench] +fn string10_cln(bencher: &mut Bencher) { + _bench_cln(bencher, vec![format!("grawwwwrr!"); 1024]); +} -#[bench] fn string20_e_d(bencher: &mut Bencher) { _bench_e_d(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); } -#[bench] fn string20_cln(bencher: &mut Bencher) { _bench_cln(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); } +#[bench] +fn string20_e_d(bencher: &mut Bencher) { + _bench_e_d(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); +} +#[bench] +fn string20_cln(bencher: &mut Bencher) { + _bench_cln(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); +} -#[bench] fn vec_u_s_e_d(bencher: &mut Bencher) { _bench_e_d(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); } -#[bench] fn vec_u_s_cln(bencher: &mut Bencher) { _bench_cln(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); } +#[bench] +fn vec_u_s_e_d(bencher: &mut Bencher) { + _bench_e_d(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); +} +#[bench] +fn vec_u_s_cln(bencher: &mut Bencher) { + _bench_cln(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); +} -#[bench] fn vec_u_vn_s_e_d(bencher: &mut Bencher) { _bench_e_d(bencher, vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32]); } -#[bench] fn vec_u_vn_s_cln(bencher: &mut Bencher) { _bench_cln(bencher, vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32]); } +#[bench] +fn vec_u_vn_s_e_d(bencher: &mut Bencher) { + _bench_e_d( + bencher, + vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32], + ); +} +#[bench] +fn vec_u_vn_s_cln(bencher: &mut Bencher) { + _bench_cln( + bencher, + vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32], + ); +} fn _bench_e_d(bencher: &mut Bencher, record: T) { - // prepare encoded data for bencher.bytes let mut bytes = Vec::new(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } // repeatedly encode this many bytes bencher.bytes = bytes.len() as u64; bencher.iter(|| { bytes = vec![]; - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } unsafe { decode::(&mut bytes) }.is_some() }); } -fn _bench_cln(bencher: &mut Bencher, record: T) { - +fn _bench_cln(bencher: &mut Bencher, record: T) { // prepare encoded data let mut bytes = Vec::new(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } // repeatedly decode (and validate) bencher.bytes = bytes.len() as u64; - bencher.iter(|| { - record.clone() - }); + bencher.iter(|| record.clone()); } diff --git a/benches/recycler.rs b/benches/recycler.rs index cc9ae94..aaf2e46 100644 --- a/benches/recycler.rs +++ b/benches/recycler.rs @@ -1,52 +1,95 @@ #![feature(test)] -extern crate recycler; extern crate abomonation; +extern crate recycler; extern crate test; -use recycler::{Recyclable, Recycler, make_recycler}; use abomonation::*; +use recycler::{make_recycler, Recyclable, Recycler}; use test::Bencher; // use std::io::Read; -#[bench] fn empty_own(bencher: &mut Bencher) { _bench_own(bencher, vec![(); 1024]); } -#[bench] fn u64_own(bencher: &mut Bencher) { _bench_own(bencher, vec![0u64; 1024]); } -#[bench] fn u8_u64_own(bencher: &mut Bencher) { _bench_own(bencher, vec![(0u8, 0u64); 512]); } -#[bench] fn string10_own(bencher: &mut Bencher) { _bench_own(bencher, vec![format!("grawwwwrr!"); 1024]); } -#[bench] fn string20_own(bencher: &mut Bencher) { _bench_own(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); } -#[bench] fn vec_u_s_own(bencher: &mut Bencher) { _bench_own(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); } -#[bench] fn vec_u_vn_s_own(bencher: &mut Bencher) { _bench_own(bencher, vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32]); } - -#[bench] fn empty_rec(bencher: &mut Bencher) { _bench_rec(bencher, vec![(); 1024]); } -#[bench] fn u64_rec(bencher: &mut Bencher) { _bench_rec(bencher, vec![0u64; 1024]); } -#[bench] fn u8_u64_rec(bencher: &mut Bencher) { _bench_rec(bencher, vec![(0u8, 0u64); 512]); } -#[bench] fn string10_rec(bencher: &mut Bencher) { _bench_rec(bencher, vec![format!("grawwwwrr!"); 1024]); } -#[bench] fn string20_rec(bencher: &mut Bencher) { _bench_rec(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); } -#[bench] fn vec_u_s_rec(bencher: &mut Bencher) { _bench_rec(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); } +#[bench] +fn empty_own(bencher: &mut Bencher) { + _bench_own(bencher, vec![(); 1024]); +} +#[bench] +fn u64_own(bencher: &mut Bencher) { + _bench_own(bencher, vec![0u64; 1024]); +} +#[bench] +fn u8_u64_own(bencher: &mut Bencher) { + _bench_own(bencher, vec![(0u8, 0u64); 512]); +} +#[bench] +fn string10_own(bencher: &mut Bencher) { + _bench_own(bencher, vec![format!("grawwwwrr!"); 1024]); +} +#[bench] +fn string20_own(bencher: &mut Bencher) { + _bench_own(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); +} +#[bench] +fn vec_u_s_own(bencher: &mut Bencher) { + _bench_own(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); +} +#[bench] +fn vec_u_vn_s_own(bencher: &mut Bencher) { + _bench_own( + bencher, + vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32], + ); +} + +#[bench] +fn empty_rec(bencher: &mut Bencher) { + _bench_rec(bencher, vec![(); 1024]); +} +#[bench] +fn u64_rec(bencher: &mut Bencher) { + _bench_rec(bencher, vec![0u64; 1024]); +} +#[bench] +fn u8_u64_rec(bencher: &mut Bencher) { + _bench_rec(bencher, vec![(0u8, 0u64); 512]); +} +#[bench] +fn string10_rec(bencher: &mut Bencher) { + _bench_rec(bencher, vec![format!("grawwwwrr!"); 1024]); +} +#[bench] +fn string20_rec(bencher: &mut Bencher) { + _bench_rec(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); +} +#[bench] +fn vec_u_s_rec(bencher: &mut Bencher) { + _bench_rec(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); +} // TODO : this reveals that working with a `vec![(); 1 << 40]` does not get optimized away. // #[bench] fn vec_u_vn_s_rec(bencher: &mut Bencher) { _bench_rec(bencher, vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32]); } -fn _bench_own(bencher: &mut Bencher, record: T) { - +fn _bench_own(bencher: &mut Bencher, record: T) { // prepare encoded data let mut bytes = Vec::new(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } // repeatedly decode (and validate) bencher.bytes = bytes.len() as u64; bencher.iter(|| { - (*unsafe {decode::(&mut bytes[..]) }.unwrap().0).clone() + (*unsafe { decode::(&mut bytes[..]) }.unwrap().0).clone() // assert!(record == result); }); } - -fn _bench_rec(bencher: &mut Bencher, record: T) { - +fn _bench_rec(bencher: &mut Bencher, record: T) { // prepare encoded data let mut bytes = Vec::new(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } let mut recycler = make_recycler::(); recycler.recycle(record); diff --git a/benches/serde.rs b/benches/serde.rs index 5965794..2c14fb5 100644 --- a/benches/serde.rs +++ b/benches/serde.rs @@ -3,25 +3,27 @@ extern crate abomonation; extern crate test; +use abomonation::{decode, encode, Abomonation}; use test::Bencher; -use abomonation::{Abomonation, encode, decode}; #[bench] fn bench_populate(b: &mut Bencher) { - b.iter(|| { - Log::new() - }); + b.iter(|| Log::new()); } #[bench] fn bench_serialize(b: &mut Bencher) { let log = Log::new(); let mut bytes = vec![]; - unsafe { encode(&log, &mut bytes).unwrap(); } + unsafe { + encode(&log, &mut bytes).unwrap(); + } b.bytes = bytes.len() as u64; b.iter(|| { bytes.clear(); - unsafe { encode(&log, &mut bytes).unwrap(); } + unsafe { + encode(&log, &mut bytes).unwrap(); + } test::black_box(&bytes); }); } @@ -30,7 +32,9 @@ fn bench_serialize(b: &mut Bencher) { fn bench_deserialize(b: &mut Bencher) { let log = Log::new(); let mut bytes = vec![]; - unsafe { encode(&log, &mut bytes).unwrap(); } + unsafe { + encode(&log, &mut bytes).unwrap(); + } b.bytes = bytes.len() as u64; b.iter(|| { test::black_box(unsafe { decode::(&mut bytes) }); @@ -41,16 +45,18 @@ fn bench_deserialize(b: &mut Bencher) { fn bench_deserialize_assert(b: &mut Bencher) { let log = Log::new(); let mut bytes = vec![]; - unsafe { encode(&log, &mut bytes).unwrap(); } + unsafe { + encode(&log, &mut bytes).unwrap(); + } b.bytes = bytes.len() as u64; b.iter(|| { assert!(unsafe { decode::(&mut bytes) }.unwrap().0 == &log); }); } -unsafe_abomonate!(Http : content_type, user_agent, referer, request_uri); -unsafe_abomonate!(Origin : ip, hostname); -unsafe_abomonate!(Log : http, origin, server_ip, server_name, remote_ip, ray_id); +unsafe_abomonate!(Http: content_type, user_agent, referer, request_uri); +unsafe_abomonate!(Origin: ip, hostname); +unsafe_abomonate!(Log: http, origin, server_ip, server_name, remote_ip, ray_id); #[derive(Eq, PartialEq)] pub struct Http { @@ -126,262 +132,262 @@ pub enum ZonePlan { #[derive(Eq, PartialEq)] pub enum Country { - UNKNOWN, - A1, - A2, - O1, - AD, - AE, - AF, - AG, - AI, - AL, - AM, - AO, - AP, - AQ, - AR, - AS, - AT, - AU, - AW, - AX, - AZ, - BA, - BB, - BD, - BE, - BF, - BG, - BH, - BI, - BJ, - BL, - BM, - BN, - BO, - BQ, - BR, - BS, - BT, - BV, - BW, - BY, - BZ, - CA, - CC, - CD, - CF, - CG, - CH, - CI, - CK, - CL, - CM, - CN, - CO, - CR, - CU, - CV, - CW, - CX, - CY, - CZ, - DE, - DJ, - DK, - DM, - DO, - DZ, - EC, - EE, - EG, - EH, - ER, - ES, - ET, - EU, - FI, - FJ, - FK, - FM, - FO, - FR, - GA, - GB, - GD, - GE, - GF, - GG, - GH, - GI, - GL, - GM, - GN, - GP, - GQ, - GR, - GS, - GT, - GU, - GW, - GY, - HK, - HM, - HN, - HR, - HT, - HU, - ID, - IE, - IL, - IM, - IN, - IO, - IQ, - IR, - IS, - IT, - JE, - JM, - JO, - JP, - KE, - KG, - KH, - KI, - KM, - KN, - KP, - KR, - KW, - KY, - KZ, - LA, - LB, - LC, - LI, - LK, - LR, - LS, - LT, - LU, - LV, - LY, - MA, - MC, - MD, - ME, - MF, - MG, - MH, - MK, - ML, - MM, - MN, - MO, - MP, - MQ, - MR, - MS, - MT, - MU, - MV, - MW, - MX, - MY, - MZ, - NA, - NC, - NE, - NF, - NG, - NI, - NL, - NO, - NP, - NR, - NU, - NZ, - OM, - PA, - PE, - PF, - PG, - PH, - PK, - PL, - PM, - PN, - PR, - PS, - PT, - PW, - PY, - QA, - RE, - RO, - RS, - RU, - RW, - SA, - SB, - SC, - SD, - SE, - SG, - SH, - SI, - SJ, - SK, - SL, - SM, - SN, - SO, - SR, - SS, - ST, - SV, - SX, - SY, - SZ, - TC, - TD, - TF, - TG, - TH, - TJ, - TK, - TL, - TM, - TN, - TO, - TR, - TT, - TV, - TW, - TZ, - UA, - UG, - UM, - US, - UY, - UZ, - VA, - VC, - VE, - VG, - VI, - VN, - VU, - WF, - WS, - XX, - YE, - YT, - ZA, - ZM, - ZW, + UNKNOWN, + A1, + A2, + O1, + AD, + AE, + AF, + AG, + AI, + AL, + AM, + AO, + AP, + AQ, + AR, + AS, + AT, + AU, + AW, + AX, + AZ, + BA, + BB, + BD, + BE, + BF, + BG, + BH, + BI, + BJ, + BL, + BM, + BN, + BO, + BQ, + BR, + BS, + BT, + BV, + BW, + BY, + BZ, + CA, + CC, + CD, + CF, + CG, + CH, + CI, + CK, + CL, + CM, + CN, + CO, + CR, + CU, + CV, + CW, + CX, + CY, + CZ, + DE, + DJ, + DK, + DM, + DO, + DZ, + EC, + EE, + EG, + EH, + ER, + ES, + ET, + EU, + FI, + FJ, + FK, + FM, + FO, + FR, + GA, + GB, + GD, + GE, + GF, + GG, + GH, + GI, + GL, + GM, + GN, + GP, + GQ, + GR, + GS, + GT, + GU, + GW, + GY, + HK, + HM, + HN, + HR, + HT, + HU, + ID, + IE, + IL, + IM, + IN, + IO, + IQ, + IR, + IS, + IT, + JE, + JM, + JO, + JP, + KE, + KG, + KH, + KI, + KM, + KN, + KP, + KR, + KW, + KY, + KZ, + LA, + LB, + LC, + LI, + LK, + LR, + LS, + LT, + LU, + LV, + LY, + MA, + MC, + MD, + ME, + MF, + MG, + MH, + MK, + ML, + MM, + MN, + MO, + MP, + MQ, + MR, + MS, + MT, + MU, + MV, + MW, + MX, + MY, + MZ, + NA, + NC, + NE, + NF, + NG, + NI, + NL, + NO, + NP, + NR, + NU, + NZ, + OM, + PA, + PE, + PF, + PG, + PH, + PK, + PL, + PM, + PN, + PR, + PS, + PT, + PW, + PY, + QA, + RE, + RO, + RS, + RU, + RW, + SA, + SB, + SC, + SD, + SE, + SG, + SH, + SI, + SJ, + SK, + SL, + SM, + SN, + SO, + SR, + SS, + ST, + SV, + SX, + SY, + SZ, + TC, + TD, + TF, + TG, + TH, + TJ, + TK, + TL, + TM, + TN, + TO, + TR, + TT, + TV, + TW, + TZ, + UA, + UG, + UM, + US, + UY, + UZ, + VA, + VC, + VE, + VG, + VI, + VN, + VU, + WF, + WS, + XX, + YE, + YT, + ZA, + ZM, + ZW, } #[derive(Eq, PartialEq)] diff --git a/src/abomonated.rs b/src/abomonated.rs index 328b195..4515e8c 100644 --- a/src/abomonated.rs +++ b/src/abomonated.rs @@ -1,9 +1,8 @@ - -use std::mem::transmute; use std::marker::PhantomData; +use std::mem::transmute; use std::ops::{Deref, DerefMut}; -use super::{Abomonation, decode}; +use super::{decode, Abomonation}; /// A type wrapping owned decoded abomonated data. /// @@ -49,13 +48,12 @@ use super::{Abomonation, decode}; /// panic!("failed to decode"); /// } /// ``` -pub struct Abomonated> { +pub struct Abomonated> { phantom: PhantomData, decoded: S, } -impl> Abomonated { - +impl> Abomonated { /// Attempts to create decoded data from owned mutable bytes. /// /// This method will return `None` if it is unable to decode the data with @@ -94,7 +92,6 @@ impl> Abomonated { /// not change if the `bytes: S` instance is moved. Good examples are /// `Vec` whereas bad examples are `[u8; 16]`. pub unsafe fn new(mut bytes: S) -> Option { - // performs the underlying pointer correction, indicates success. let decoded = decode::(bytes.deref_mut()).is_some(); @@ -103,21 +100,19 @@ impl> Abomonated { phantom: PhantomData, decoded: bytes, }) - } - else { + } else { None } } } -impl> Abomonated { +impl> Abomonated { pub fn as_bytes(&self) -> &[u8] { &self.decoded } } - -impl> Deref for Abomonated { +impl> Deref for Abomonated { type Target = T; #[inline] fn deref(&self) -> &T { diff --git a/src/lib.rs b/src/lib.rs index 016bcc8..5feef07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,10 +35,10 @@ //! } //! ``` -use std::mem; // yup, used pretty much everywhere. -use std::io::Write; // for bytes.write_all; push_all is unstable and extend is slow. use std::io::Result as IOResult; +use std::io::Write; // for bytes.write_all; push_all is unstable and extend is slow. use std::marker::PhantomData; +use std::mem; // yup, used pretty much everywhere. use std::num::*; pub mod abomonated; @@ -121,14 +121,14 @@ pub unsafe fn encode(typed: &T, write: &mut W) -> IORe /// ``` #[inline] pub unsafe fn decode(bytes: &mut [u8]) -> Option<(&T, &mut [u8])> { - if bytes.len() < mem::size_of::() { None } - else { + if bytes.len() < mem::size_of::() { + None + } else { let (split1, split2) = bytes.split_at_mut(mem::size_of::()); let result: &mut T = mem::transmute(split1.get_unchecked_mut(0)); if let Some(remaining) = result.exhume(split2) { Some((result, remaining)) - } - else { + } else { None } } @@ -158,20 +158,28 @@ pub fn measure(typed: &T) -> usize { /// If you are concerned about safety, it may be best to avoid Abomonation all together. It does /// several things that may be undefined behavior, depending on how undefined behavior is defined. pub trait Abomonation { - /// Write any additional information about `&self` beyond its binary representation. /// /// Most commonly this is owned data on the other end of pointers in `&self`. The return value /// reports any failures in writing to `write`. - #[inline(always)] unsafe fn entomb(&self, _write: &mut W) -> IOResult<()> { Ok(()) } + #[inline(always)] + unsafe fn entomb(&self, _write: &mut W) -> IOResult<()> { + Ok(()) + } /// Recover any information for `&mut self` not evident from its binary representation. /// /// Most commonly this populates pointers with valid references into `bytes`. - #[inline(always)] unsafe fn exhume<'a,'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { Some(bytes) } + #[inline(always)] + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + Some(bytes) + } /// Reports the number of further bytes required to entomb `self`. - #[inline(always)] fn extent(&self) -> usize { 0 } + #[inline(always)] + fn extent(&self) -> usize { + 0 + } } /// The `unsafe_abomonate!` macro takes a type name with an optional list of fields, and implements @@ -216,7 +224,7 @@ pub trait Abomonation { /// } /// ``` #[macro_export] -#[deprecated(since="0.5", note="please use the abomonation_derive crate")] +#[deprecated(since = "0.5", note = "please use the abomonation_derive crate")] macro_rules! unsafe_abomonate { ($t:ty) => { impl Abomonation for $t { } @@ -267,95 +275,107 @@ macro_rules! tuple_abomonate { ); } -impl Abomonation for u8 { } -impl Abomonation for u16 { } -impl Abomonation for u32 { } -impl Abomonation for u64 { } -impl Abomonation for u128 { } -impl Abomonation for usize { } +impl Abomonation for u8 {} +impl Abomonation for u16 {} +impl Abomonation for u32 {} +impl Abomonation for u64 {} +impl Abomonation for u128 {} +impl Abomonation for usize {} -impl Abomonation for i8 { } -impl Abomonation for i16 { } -impl Abomonation for i32 { } -impl Abomonation for i64 { } -impl Abomonation for i128 { } -impl Abomonation for isize { } +impl Abomonation for i8 {} +impl Abomonation for i16 {} +impl Abomonation for i32 {} +impl Abomonation for i64 {} +impl Abomonation for i128 {} +impl Abomonation for isize {} -impl Abomonation for NonZeroU8 { } -impl Abomonation for NonZeroU16 { } -impl Abomonation for NonZeroU32 { } -impl Abomonation for NonZeroU64 { } -impl Abomonation for NonZeroU128 { } -impl Abomonation for NonZeroUsize { } +impl Abomonation for NonZeroU8 {} +impl Abomonation for NonZeroU16 {} +impl Abomonation for NonZeroU32 {} +impl Abomonation for NonZeroU64 {} +impl Abomonation for NonZeroU128 {} +impl Abomonation for NonZeroUsize {} -impl Abomonation for NonZeroI8 { } -impl Abomonation for NonZeroI16 { } -impl Abomonation for NonZeroI32 { } -impl Abomonation for NonZeroI64 { } -impl Abomonation for NonZeroI128 { } -impl Abomonation for NonZeroIsize { } +impl Abomonation for NonZeroI8 {} +impl Abomonation for NonZeroI16 {} +impl Abomonation for NonZeroI32 {} +impl Abomonation for NonZeroI64 {} +impl Abomonation for NonZeroI128 {} +impl Abomonation for NonZeroIsize {} -impl Abomonation for f32 { } -impl Abomonation for f64 { } +impl Abomonation for f32 {} +impl Abomonation for f64 {} -impl Abomonation for bool { } -impl Abomonation for () { } +impl Abomonation for bool {} +impl Abomonation for () {} -impl Abomonation for char { } +impl Abomonation for char {} -impl Abomonation for ::std::time::Duration { } +impl Abomonation for ::std::time::Duration {} impl Abomonation for PhantomData {} impl Abomonation for std::ops::Range { - #[inline(always)] unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { + #[inline(always)] + unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { self.start.entomb(write)?; self.end.entomb(write)?; Ok(()) } - #[inline(always)] unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut[u8]) -> Option<&'b mut [u8]> { - let tmp = bytes; bytes = self.start.exhume(tmp)?; - let tmp = bytes; bytes = self.end.exhume(tmp)?; + #[inline(always)] + unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + let tmp = bytes; + bytes = self.start.exhume(tmp)?; + let tmp = bytes; + bytes = self.end.exhume(tmp)?; Some(bytes) } - #[inline] fn extent(&self) -> usize { + #[inline] + fn extent(&self) -> usize { self.start.extent() << 1 } } impl Abomonation for Option { - #[inline(always)] unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { + #[inline(always)] + unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { if let &Some(ref inner) = self { inner.entomb(write)?; } Ok(()) } - #[inline(always)] unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut[u8]) -> Option<&'b mut [u8]> { + #[inline(always)] + unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { if let &mut Some(ref mut inner) = self { - let tmp = bytes; bytes = inner.exhume(tmp)?; + let tmp = bytes; + bytes = inner.exhume(tmp)?; } Some(bytes) } - #[inline] fn extent(&self) -> usize { + #[inline] + fn extent(&self) -> usize { self.as_ref().map(|inner| inner.extent()).unwrap_or(0) } } impl Abomonation for Result { - #[inline(always)] unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { + #[inline(always)] + unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { match self { &Ok(ref inner) => inner.entomb(write)?, &Err(ref inner) => inner.entomb(write)?, }; Ok(()) } - #[inline(always)] unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut[u8]) -> Option<&'b mut [u8]> { + #[inline(always)] + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { match self { &mut Ok(ref mut inner) => inner.exhume(bytes), &mut Err(ref mut inner) => inner.exhume(bytes), } } - #[inline] fn extent(&self) -> usize { + #[inline] + fn extent(&self) -> usize { match self { &Ok(ref inner) => inner.extent(), &Err(ref inner) => inner.extent(), @@ -396,23 +416,29 @@ tuple_abomonate!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD tuple_abomonate!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE); tuple_abomonate!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF); - macro_rules! array_abomonate { - ($size:expr) => ( + ($size:expr) => { impl Abomonation for [T; $size] { #[inline(always)] - unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { - for element in self { element.entomb(write)?; } + unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { + for element in self { + element.entomb(write)?; + } Ok(()) } #[inline(always)] - unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut[u8]) -> Option<&'b mut [u8]> { + unsafe fn exhume<'a, 'b>( + &'a mut self, + mut bytes: &'b mut [u8], + ) -> Option<&'b mut [u8]> { for element in self { - let tmp = bytes; bytes = element.exhume(tmp)?; + let tmp = bytes; + bytes = element.exhume(tmp)?; } Some(bytes) } - #[inline(always)] fn extent(&self) -> usize { + #[inline(always)] + fn extent(&self) -> usize { let mut size = 0; for element in self { size += element.extent(); @@ -420,7 +446,7 @@ macro_rules! array_abomonate { size } } - ) + }; } array_abomonate!(0); @@ -464,15 +490,20 @@ impl Abomonation for String { Ok(()) } #[inline] - unsafe fn exhume<'a,'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - if self.len() > bytes.len() { None } - else { + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + if self.len() > bytes.len() { + None + } else { let (mine, rest) = bytes.split_at_mut(self.len()); - std::ptr::write(self, String::from_raw_parts(mem::transmute(mine.as_ptr()), self.len(), self.len())); + std::ptr::write( + self, + String::from_raw_parts(mem::transmute(mine.as_ptr()), self.len(), self.len()), + ); Some(rest) } } - #[inline] fn extent(&self) -> usize { + #[inline] + fn extent(&self) -> usize { self.len() } } @@ -481,21 +512,26 @@ impl Abomonation for Vec { #[inline] unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { write.write_all(typed_to_bytes(&self[..]))?; - for element in self.iter() { element.entomb(write)?; } + for element in self.iter() { + element.entomb(write)?; + } Ok(()) } #[inline] - unsafe fn exhume<'a,'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { - + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { // extract memory from bytes to back our vector let binary_len = self.len() * mem::size_of::(); - if binary_len > bytes.len() { None } - else { + if binary_len > bytes.len() { + None + } else { let (mine, mut rest) = bytes.split_at_mut(binary_len); let slice = std::slice::from_raw_parts_mut(mine.as_mut_ptr() as *mut T, self.len()); - std::ptr::write(self, Vec::from_raw_parts(slice.as_mut_ptr(), self.len(), self.len())); + std::ptr::write( + self, + Vec::from_raw_parts(slice.as_mut_ptr(), self.len(), self.len()), + ); for element in self.iter_mut() { - let temp = rest; // temp variable explains lifetimes (mysterious!) + let temp = rest; // temp variable explains lifetimes (mysterious!) rest = element.exhume(temp)?; } Some(rest) @@ -514,40 +550,50 @@ impl Abomonation for Vec { impl Abomonation for Box { #[inline] unsafe fn entomb(&self, bytes: &mut W) -> IOResult<()> { - bytes.write_all(std::slice::from_raw_parts(mem::transmute(&**self), mem::size_of::()))?; + bytes.write_all(std::slice::from_raw_parts( + mem::transmute(&**self), + mem::size_of::(), + ))?; (**self).entomb(bytes)?; Ok(()) } #[inline] - unsafe fn exhume<'a,'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + unsafe fn exhume<'a, 'b>(&'a mut self, bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { let binary_len = mem::size_of::(); - if binary_len > bytes.len() { None } - else { + if binary_len > bytes.len() { + None + } else { let (mine, mut rest) = bytes.split_at_mut(binary_len); std::ptr::write(self, mem::transmute(mine.as_mut_ptr() as *mut T)); - let temp = rest; rest = (**self).exhume(temp)?; + let temp = rest; + rest = (**self).exhume(temp)?; Some(rest) } } - #[inline] fn extent(&self) -> usize { + #[inline] + fn extent(&self) -> usize { mem::size_of::() + (&**self).extent() } } // This method currently enables undefined behavior, by exposing padding bytes. -#[inline] unsafe fn typed_to_bytes(slice: &[T]) -> &[u8] { - std::slice::from_raw_parts(slice.as_ptr() as *const u8, slice.len() * mem::size_of::()) +#[inline] +unsafe fn typed_to_bytes(slice: &[T]) -> &[u8] { + std::slice::from_raw_parts( + slice.as_ptr() as *const u8, + slice.len() * mem::size_of::(), + ) } mod network { + use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; use Abomonation; - use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6, IpAddr, Ipv4Addr, Ipv6Addr}; - impl Abomonation for IpAddr { } - impl Abomonation for Ipv4Addr { } - impl Abomonation for Ipv6Addr { } + impl Abomonation for IpAddr {} + impl Abomonation for Ipv4Addr {} + impl Abomonation for Ipv6Addr {} - impl Abomonation for SocketAddr { } - impl Abomonation for SocketAddrV4 { } - impl Abomonation for SocketAddrV6 { } + impl Abomonation for SocketAddr {} + impl Abomonation for SocketAddrV4 {} + impl Abomonation for SocketAddrV6 {} } diff --git a/tests/tests.rs b/tests/tests.rs index 340e776..031e040 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -2,34 +2,112 @@ extern crate abomonation; use abomonation::*; -#[test] fn test_array() { _test_pass(vec![[0, 1, 2]; 1024]); } -#[test] fn test_nonzero() { _test_pass(vec![[std::num::NonZeroI32::new(1)]; 1024]); } -#[test] fn test_opt_vec() { _test_pass(vec![Some(vec![0,1,2]), None]); } -#[test] fn test_alignment() { _test_pass(vec![(format!("x"), vec![1,2,3]); 1024]); } -#[test] fn test_alignment_128() { _test_pass(vec![(format!("x"), vec![1u128,2,3]); 1024]); } -#[test] fn test_option_box_u64() { _test_pass(vec![Some(Box::new(0u64))]); } -#[test] fn test_option_vec() { _test_pass(vec![Some(vec![0, 1, 2])]); } -#[test] fn test_u32x4_pass() { _test_pass(vec![((1,2,3),vec![(0u32, 0u32, 0u32, 0u32); 1024])]); } -#[test] fn test_u64_pass() { _test_pass(vec![0u64; 1024]); } -#[test] fn test_u128_pass() { _test_pass(vec![0u128; 1024]); } -#[test] fn test_string_pass() { _test_pass(vec![format!("grawwwwrr!"); 1024]); } -#[test] fn test_vec_u_s_pass() { _test_pass(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); } - -#[test] fn test_u64_fail() { _test_fail(vec![0u64; 1024]); } -#[test] fn test_u128_fail() { _test_fail(vec![0u128; 1024]); } -#[test] fn test_string_fail() { _test_fail(vec![format!("grawwwwrr!"); 1024]); } -#[test] fn test_vec_u_s_fail() { _test_fail(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); } - -#[test] fn test_array_size() { _test_size(vec![[0, 1, 2]; 1024]); } -#[test] fn test_opt_vec_size() { _test_size(vec![Some(vec![0,1,2]), None]); } -#[test] fn test_alignment_size() { _test_size(vec![(format!("x"), vec![1,2,3]); 1024]); } -#[test] fn test_option_box_u64_size() { _test_size(vec![Some(Box::new(0u64))]); } -#[test] fn test_option_vec_size() { _test_size(vec![Some(vec![0, 1, 2])]); } -#[test] fn test_u32x4_size() { _test_size(vec![((1,2,3),vec![(0u32, 0u32, 0u32, 0u32); 1024])]); } -#[test] fn test_u64_size() { _test_size(vec![0u64; 1024]); } -#[test] fn test_u128_size() { _test_size(vec![0u128; 1024]); } -#[test] fn test_string_size() { _test_size(vec![format!("grawwwwrr!"); 1024]); } -#[test] fn test_vec_u_s_size() { _test_size(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); } +#[test] +fn test_array() { + _test_pass(vec![[0, 1, 2]; 1024]); +} +#[test] +fn test_nonzero() { + _test_pass(vec![[std::num::NonZeroI32::new(1)]; 1024]); +} +#[test] +fn test_opt_vec() { + _test_pass(vec![Some(vec![0, 1, 2]), None]); +} +#[test] +fn test_alignment() { + _test_pass(vec![(format!("x"), vec![1, 2, 3]); 1024]); +} +#[test] +fn test_alignment_128() { + _test_pass(vec![(format!("x"), vec![1u128, 2, 3]); 1024]); +} +#[test] +fn test_option_box_u64() { + _test_pass(vec![Some(Box::new(0u64))]); +} +#[test] +fn test_option_vec() { + _test_pass(vec![Some(vec![0, 1, 2])]); +} +#[test] +fn test_u32x4_pass() { + _test_pass(vec![((1, 2, 3), vec![(0u32, 0u32, 0u32, 0u32); 1024])]); +} +#[test] +fn test_u64_pass() { + _test_pass(vec![0u64; 1024]); +} +#[test] +fn test_u128_pass() { + _test_pass(vec![0u128; 1024]); +} +#[test] +fn test_string_pass() { + _test_pass(vec![format!("grawwwwrr!"); 1024]); +} +#[test] +fn test_vec_u_s_pass() { + _test_pass(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); +} + +#[test] +fn test_u64_fail() { + _test_fail(vec![0u64; 1024]); +} +#[test] +fn test_u128_fail() { + _test_fail(vec![0u128; 1024]); +} +#[test] +fn test_string_fail() { + _test_fail(vec![format!("grawwwwrr!"); 1024]); +} +#[test] +fn test_vec_u_s_fail() { + _test_fail(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); +} + +#[test] +fn test_array_size() { + _test_size(vec![[0, 1, 2]; 1024]); +} +#[test] +fn test_opt_vec_size() { + _test_size(vec![Some(vec![0, 1, 2]), None]); +} +#[test] +fn test_alignment_size() { + _test_size(vec![(format!("x"), vec![1, 2, 3]); 1024]); +} +#[test] +fn test_option_box_u64_size() { + _test_size(vec![Some(Box::new(0u64))]); +} +#[test] +fn test_option_vec_size() { + _test_size(vec![Some(vec![0, 1, 2])]); +} +#[test] +fn test_u32x4_size() { + _test_size(vec![((1, 2, 3), vec![(0u32, 0u32, 0u32, 0u32); 1024])]); +} +#[test] +fn test_u64_size() { + _test_size(vec![0u64; 1024]); +} +#[test] +fn test_u128_size() { + _test_size(vec![0u128; 1024]); +} +#[test] +fn test_string_size() { + _test_size(vec![format!("grawwwwrr!"); 1024]); +} +#[test] +fn test_vec_u_s_size() { + _test_size(vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); +} #[test] fn test_phantom_data_for_non_abomonatable_type() { @@ -38,9 +116,11 @@ fn test_phantom_data_for_non_abomonatable_type() { _test_pass(PhantomData::::default()); } -fn _test_pass(record: T) { +fn _test_pass(record: T) { let mut bytes = Vec::new(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } { let (result, rest) = unsafe { decode::(&mut bytes[..]) }.unwrap(); assert!(&record == result); @@ -50,18 +130,21 @@ fn _test_pass(record: T) { fn _test_fail(record: T) { let mut bytes = Vec::new(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } bytes.pop(); assert!(unsafe { decode::(&mut bytes[..]) }.is_none()); } fn _test_size(record: T) { let mut bytes = Vec::new(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } assert_eq!(bytes.len(), measure(&record)); } - #[derive(Eq, PartialEq)] struct MyStruct { a: String, @@ -69,16 +152,22 @@ struct MyStruct { c: Vec, } -unsafe_abomonate!(MyStruct : a, b, c); +unsafe_abomonate!(MyStruct: a, b, c); #[test] fn test_macro() { // create some test data out of abomonation-approved types - let record = MyStruct{ a: "test".to_owned(), b: 0, c: vec![0, 1, 2] }; + let record = MyStruct { + a: "test".to_owned(), + b: 0, + c: vec![0, 1, 2], + }; // encode vector into a Vec let mut bytes = Vec::new(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } // decode a &Vec<(u64, String)> from binary data if let Some((result, rest)) = unsafe { decode::(&mut bytes) } { @@ -90,41 +179,64 @@ fn test_macro() { #[test] fn test_macro_size() { // create some test data out of abomonation-approved types - let record = MyStruct{ a: "test".to_owned(), b: 0, c: vec![0, 1, 2] }; + let record = MyStruct { + a: "test".to_owned(), + b: 0, + c: vec![0, 1, 2], + }; // encode vector into a Vec let mut bytes = Vec::new(); - unsafe { encode(&record, &mut bytes).unwrap(); } + unsafe { + encode(&record, &mut bytes).unwrap(); + } assert_eq!(bytes.len(), measure(&record)); } #[test] fn test_multiple_encode_decode() { let mut bytes = Vec::new(); - unsafe { encode(&0u32, &mut bytes).unwrap(); } - unsafe { encode(&7u64, &mut bytes).unwrap(); } - unsafe { encode(&vec![1,2,3], &mut bytes).unwrap(); } - unsafe { encode(&"grawwwwrr".to_owned(), &mut bytes).unwrap(); } + unsafe { + encode(&0u32, &mut bytes).unwrap(); + } + unsafe { + encode(&7u64, &mut bytes).unwrap(); + } + unsafe { + encode(&vec![1, 2, 3], &mut bytes).unwrap(); + } + unsafe { + encode(&"grawwwwrr".to_owned(), &mut bytes).unwrap(); + } - let (t, r) = unsafe { decode::(&mut bytes) }.unwrap(); assert!(*t == 0); - let (t, r) = unsafe { decode::(r) }.unwrap(); assert!(*t == 7); - let (t, r) = unsafe { decode::>(r) }.unwrap(); assert!(*t == vec![1,2,3]); - let (t, _r) = unsafe { decode::(r) }.unwrap(); assert!(*t == "grawwwwrr".to_owned()); + let (t, r) = unsafe { decode::(&mut bytes) }.unwrap(); + assert!(*t == 0); + let (t, r) = unsafe { decode::(r) }.unwrap(); + assert!(*t == 7); + let (t, r) = unsafe { decode::>(r) }.unwrap(); + assert!(*t == vec![1, 2, 3]); + let (t, _r) = unsafe { decode::(r) }.unwrap(); + assert!(*t == "grawwwwrr".to_owned()); } #[test] fn test_net_types() { - - use std::net::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr}; + use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; let socket_addr4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(128, 0, 0, 1)), 1234); let socket_addr6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 1234); let mut bytes = Vec::new(); - unsafe { encode(&socket_addr4, &mut bytes).unwrap(); } - unsafe { encode(&socket_addr6, &mut bytes).unwrap(); } + unsafe { + encode(&socket_addr4, &mut bytes).unwrap(); + } + unsafe { + encode(&socket_addr6, &mut bytes).unwrap(); + } - let (t, r) = unsafe { decode::(&mut bytes) }.unwrap(); assert!(*t == socket_addr4); - let (t, _r) = unsafe { decode::(r) }.unwrap(); assert!(*t == socket_addr6); + let (t, r) = unsafe { decode::(&mut bytes) }.unwrap(); + assert!(*t == socket_addr4); + let (t, _r) = unsafe { decode::(r) }.unwrap(); + assert!(*t == socket_addr6); } From 575041fc752510e6b00a95b9e36fc037fac383f5 Mon Sep 17 00:00:00 2001 From: Zane Hannan Date: Fri, 13 Aug 2021 19:31:58 +1000 Subject: [PATCH 2/3] add array const --- src/lib.rs | 171 +++++++++++++++++++++-------------------------------- 1 file changed, 68 insertions(+), 103 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5feef07..903c903 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![cfg_attr(feature = "specialisation", feature(specialization))] //! Abomonation (spelling intentional) is a fast serialization / deserialization crate. //! //! Abomonation takes typed elements and simply writes their contents as binary. @@ -181,6 +182,8 @@ pub trait Abomonation { 0 } } +#[cfg(feature = "specialisation")] +default impl Abomonation for T {} /// The `unsafe_abomonate!` macro takes a type name with an optional list of fields, and implements /// `Abomonation` for the type, following the pattern of the tuple implementations: each method @@ -275,45 +278,48 @@ macro_rules! tuple_abomonate { ); } -impl Abomonation for u8 {} -impl Abomonation for u16 {} -impl Abomonation for u32 {} -impl Abomonation for u64 {} -impl Abomonation for u128 {} -impl Abomonation for usize {} - -impl Abomonation for i8 {} -impl Abomonation for i16 {} -impl Abomonation for i32 {} -impl Abomonation for i64 {} -impl Abomonation for i128 {} -impl Abomonation for isize {} - -impl Abomonation for NonZeroU8 {} -impl Abomonation for NonZeroU16 {} -impl Abomonation for NonZeroU32 {} -impl Abomonation for NonZeroU64 {} -impl Abomonation for NonZeroU128 {} -impl Abomonation for NonZeroUsize {} - -impl Abomonation for NonZeroI8 {} -impl Abomonation for NonZeroI16 {} -impl Abomonation for NonZeroI32 {} -impl Abomonation for NonZeroI64 {} -impl Abomonation for NonZeroI128 {} -impl Abomonation for NonZeroIsize {} - -impl Abomonation for f32 {} -impl Abomonation for f64 {} - -impl Abomonation for bool {} -impl Abomonation for () {} - -impl Abomonation for char {} - -impl Abomonation for ::std::time::Duration {} - -impl Abomonation for PhantomData {} +#[cfg(not(feature = "specialisation"))] +mod copy { + impl Abomonation for u8 {} + impl Abomonation for u16 {} + impl Abomonation for u32 {} + impl Abomonation for u64 {} + impl Abomonation for u128 {} + impl Abomonation for usize {} + + impl Abomonation for i8 {} + impl Abomonation for i16 {} + impl Abomonation for i32 {} + impl Abomonation for i64 {} + impl Abomonation for i128 {} + impl Abomonation for isize {} + + impl Abomonation for NonZeroU8 {} + impl Abomonation for NonZeroU16 {} + impl Abomonation for NonZeroU32 {} + impl Abomonation for NonZeroU64 {} + impl Abomonation for NonZeroU128 {} + impl Abomonation for NonZeroUsize {} + + impl Abomonation for NonZeroI8 {} + impl Abomonation for NonZeroI16 {} + impl Abomonation for NonZeroI32 {} + impl Abomonation for NonZeroI64 {} + impl Abomonation for NonZeroI128 {} + impl Abomonation for NonZeroIsize {} + + impl Abomonation for f32 {} + impl Abomonation for f64 {} + + impl Abomonation for bool {} + impl Abomonation for () {} + + impl Abomonation for char {} + + impl Abomonation for ::std::time::Duration {} + + impl Abomonation for PhantomData {} +} impl Abomonation for std::ops::Range { #[inline(always)] @@ -416,73 +422,32 @@ tuple_abomonate!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD tuple_abomonate!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE); tuple_abomonate!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF); -macro_rules! array_abomonate { - ($size:expr) => { - impl Abomonation for [T; $size] { - #[inline(always)] - unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { - for element in self { - element.entomb(write)?; - } - Ok(()) - } - #[inline(always)] - unsafe fn exhume<'a, 'b>( - &'a mut self, - mut bytes: &'b mut [u8], - ) -> Option<&'b mut [u8]> { - for element in self { - let tmp = bytes; - bytes = element.exhume(tmp)?; - } - Some(bytes) - } - #[inline(always)] - fn extent(&self) -> usize { - let mut size = 0; - for element in self { - size += element.extent(); - } - size - } +impl Abomonation for [T; N] { + #[inline(always)] + unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { + for element in self { + element.entomb(write)?; } - }; + Ok(()) + } + #[inline(always)] + unsafe fn exhume<'a, 'b>(&'a mut self, mut bytes: &'b mut [u8]) -> Option<&'b mut [u8]> { + for element in self { + let tmp = bytes; + bytes = element.exhume(tmp)?; + } + Some(bytes) + } + #[inline(always)] + fn extent(&self) -> usize { + let mut size = 0; + for element in self { + size += element.extent(); + } + size + } } -array_abomonate!(0); -array_abomonate!(1); -array_abomonate!(2); -array_abomonate!(3); -array_abomonate!(4); -array_abomonate!(5); -array_abomonate!(6); -array_abomonate!(7); -array_abomonate!(8); -array_abomonate!(9); -array_abomonate!(10); -array_abomonate!(11); -array_abomonate!(12); -array_abomonate!(13); -array_abomonate!(14); -array_abomonate!(15); -array_abomonate!(16); -array_abomonate!(17); -array_abomonate!(18); -array_abomonate!(19); -array_abomonate!(20); -array_abomonate!(21); -array_abomonate!(22); -array_abomonate!(23); -array_abomonate!(24); -array_abomonate!(25); -array_abomonate!(26); -array_abomonate!(27); -array_abomonate!(28); -array_abomonate!(29); -array_abomonate!(30); -array_abomonate!(31); -array_abomonate!(32); - impl Abomonation for String { #[inline] unsafe fn entomb(&self, write: &mut W) -> IOResult<()> { From b9325155d35164d12f4cada9d7d73183747940d4 Mon Sep 17 00:00:00 2001 From: Zane Hannan Date: Fri, 13 Aug 2021 19:32:39 +1000 Subject: [PATCH 3/3] add array const --- src/lib.rs | 66 +++++++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 903c903..1b86e8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -#![cfg_attr(feature = "specialisation", feature(specialization))] //! Abomonation (spelling intentional) is a fast serialization / deserialization crate. //! //! Abomonation takes typed elements and simply writes their contents as binary. @@ -278,48 +277,45 @@ macro_rules! tuple_abomonate { ); } -#[cfg(not(feature = "specialisation"))] -mod copy { - impl Abomonation for u8 {} - impl Abomonation for u16 {} - impl Abomonation for u32 {} - impl Abomonation for u64 {} - impl Abomonation for u128 {} - impl Abomonation for usize {} +impl Abomonation for u8 {} +impl Abomonation for u16 {} +impl Abomonation for u32 {} +impl Abomonation for u64 {} +impl Abomonation for u128 {} +impl Abomonation for usize {} - impl Abomonation for i8 {} - impl Abomonation for i16 {} - impl Abomonation for i32 {} - impl Abomonation for i64 {} - impl Abomonation for i128 {} - impl Abomonation for isize {} +impl Abomonation for i8 {} +impl Abomonation for i16 {} +impl Abomonation for i32 {} +impl Abomonation for i64 {} +impl Abomonation for i128 {} +impl Abomonation for isize {} - impl Abomonation for NonZeroU8 {} - impl Abomonation for NonZeroU16 {} - impl Abomonation for NonZeroU32 {} - impl Abomonation for NonZeroU64 {} - impl Abomonation for NonZeroU128 {} - impl Abomonation for NonZeroUsize {} +impl Abomonation for NonZeroU8 {} +impl Abomonation for NonZeroU16 {} +impl Abomonation for NonZeroU32 {} +impl Abomonation for NonZeroU64 {} +impl Abomonation for NonZeroU128 {} +impl Abomonation for NonZeroUsize {} - impl Abomonation for NonZeroI8 {} - impl Abomonation for NonZeroI16 {} - impl Abomonation for NonZeroI32 {} - impl Abomonation for NonZeroI64 {} - impl Abomonation for NonZeroI128 {} - impl Abomonation for NonZeroIsize {} +impl Abomonation for NonZeroI8 {} +impl Abomonation for NonZeroI16 {} +impl Abomonation for NonZeroI32 {} +impl Abomonation for NonZeroI64 {} +impl Abomonation for NonZeroI128 {} +impl Abomonation for NonZeroIsize {} - impl Abomonation for f32 {} - impl Abomonation for f64 {} +impl Abomonation for f32 {} +impl Abomonation for f64 {} - impl Abomonation for bool {} - impl Abomonation for () {} +impl Abomonation for bool {} +impl Abomonation for () {} - impl Abomonation for char {} +impl Abomonation for char {} - impl Abomonation for ::std::time::Duration {} +impl Abomonation for ::std::time::Duration {} - impl Abomonation for PhantomData {} -} +impl Abomonation for PhantomData {} impl Abomonation for std::ops::Range { #[inline(always)]