byteorder_slice-3.0.0/.cargo_vcs_info.json0000644000000001360000000000100142120ustar { "git": { "sha1": "044018a45be8f549e2ecf222d3edf609c1319a22" }, "path_in_vcs": "" }byteorder_slice-3.0.0/.gitignore000064400000000000000000000000451046102023000147710ustar 00000000000000.idea/ target/ .vscode/ Cargo.lock byteorder_slice-3.0.0/Cargo.toml0000644000000015560000000000100122170ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "byteorder_slice" version = "3.0.0" authors = ["courvoif "] description = "Byteorder like crate for &[u8]" documentation = "https://docs.rs/byteorder_slice/" readme = "README.md" license = "MIT" repository = "https://github.com/courvoif/byteorder_slice" resolver = "2" [dependencies.byteorder] version = "1.4" [dev-dependencies.hex-literal] version = "0.3.4" byteorder_slice-3.0.0/Cargo.toml.orig000064400000000000000000000005501046102023000156710ustar 00000000000000[package] name = "byteorder_slice" version = "3.0.0" authors = ["courvoif "] edition = "2021" description = "Byteorder like crate for &[u8]" license = "MIT" documentation = "https://docs.rs/byteorder_slice/" repository = "https://github.com/courvoif/byteorder_slice" [dependencies] byteorder = "1.4" [dev-dependencies] hex-literal = "0.3.4" byteorder_slice-3.0.0/README.md000064400000000000000000000010521046102023000142570ustar 00000000000000# byteorder_slice Provides convenience methods for reading numbers and slices from a slice ## Usage Use ```byteorder_slice = "1.0.0"``` if you want reads to return an Option. Use ```byteorder_slice = "2.0.0"``` if you want reads to return a std::io::Result. ```rust use byteorder_slice::{BigEndian, LittleEndian, ReadSlice} let data = vec![0_u8; 100]; let src = &mut &data[..]; let a = src.read_u8().unwrap(); let b = src.read_u32::().unwrap(); let c = src.read_uint::(3).unwrap(); let d = src.read_slice(10).unwrap(); ``` byteorder_slice-3.0.0/rustfmt.toml000064400000000000000000000034161046102023000154070ustar 00000000000000array_width = 120 attr_fn_like_width = 120 binop_separator = "Front" blank_lines_lower_bound = 0 blank_lines_upper_bound = 2 brace_style = "SameLineWhere" chain_width = 100 color = "Always" combine_control_expr = false comment_width = 140 condense_wildcard_suffixes = true control_brace_style = "ClosingNextLine" empty_item_single_line = true enum_discrim_align_threshold = 0 fn_args_layout = "Tall" fn_call_width = 120 fn_single_line = false force_explicit_abi = true force_multiline_blocks = false format_code_in_doc_comments = true format_generated_files = false format_macro_matchers = true format_macro_bodies = true format_strings = true hard_tabs = false hex_literal_case = "Upper" imports_indent = "Block" imports_layout = "Mixed" indent_style = "Block" inline_attribute_width = 0 license_template_path = "" match_arm_blocks = true match_arm_leading_pipes = "Never" match_block_trailing_comma = true max_width = 140 merge_derives = true imports_granularity = "Module" newline_style = "Unix" normalize_comments = false normalize_doc_attributes = true # A tester overflow_delimited_expr = true remove_nested_parens = true reorder_impl_items = true reorder_imports = true group_imports = "StdExternalCrate" reorder_modules = true report_fixme = "Never" report_todo = "Never" skip_children = false single_line_if_else_max_width = 80 space_after_colon = true space_before_colon = false spaces_around_ranges = false struct_field_align_threshold = 0 struct_lit_single_line = true struct_lit_width = 80 struct_variant_width = 80 tab_spaces = 4 trailing_comma = "Vertical" trailing_semicolon = true type_punctuation_density = "Wide" unstable_features = true use_field_init_shorthand = true use_small_heuristics = "Off" use_try_shorthand = true version = "Two" where_single_line = false wrap_comments = true byteorder_slice-3.0.0/src/lib.rs000064400000000000000000000002531046102023000147050ustar 00000000000000#![allow(clippy::cast_lossless)] pub mod option; pub mod result; pub use byteorder; pub use byteorder::{BigEndian, ByteOrder, LittleEndian, NativeEndian, NetworkEndian}; byteorder_slice-3.0.0/src/option/mod.rs000064400000000000000000000000751046102023000162300ustar 00000000000000mod read_slice_impl; mod traits; pub use traits::ReadSlice; byteorder_slice-3.0.0/src/option/read_slice_impl.rs000064400000000000000000000124771046102023000205750ustar 00000000000000use std::ops::Not; use byteorder::ByteOrder; use super::traits::ReadSlice; impl<'a> ReadSlice<'a> for &'a [u8] { fn read_u8(&mut self) -> Option { if self.is_empty().not() { let b = self[0]; *self = &self[1..]; Some(b) } else { None } } fn read_u16(&mut self) -> Option { if self.len() >= 2 { let b = B::read_u16(self); *self = &self[2..]; Some(b) } else { None } } fn read_u32(&mut self) -> Option { if self.len() >= 4 { let b = B::read_u32(self); *self = &self[4..]; Some(b) } else { None } } fn read_u64(&mut self) -> Option { if self.len() >= 8 { let b = B::read_u64(self); *self = &self[8..]; Some(b) } else { None } } fn read_u128(&mut self) -> Option { if self.len() >= 16 { let b = B::read_u128(self); *self = &self[16..]; Some(b) } else { None } } fn read_uint32(&mut self, nb_bytes: usize) -> Option { if nb_bytes != 0 && nb_bytes <= 4 && self.len() >= nb_bytes { let b = B::read_uint(self, nb_bytes) as u32; *self = &self[nb_bytes..]; Some(b) } else { None } } fn read_uint64(&mut self, nb_bytes: usize) -> Option { if nb_bytes != 0 && nb_bytes <= 8 && self.len() >= nb_bytes { let b = B::read_uint(self, nb_bytes); *self = &self[nb_bytes..]; Some(b) } else { None } } fn read_uint128(&mut self, nb_bytes: usize) -> Option { if nb_bytes != 0 && nb_bytes <= 16 && self.len() >= nb_bytes { let b = B::read_uint128(self, nb_bytes); *self = &self[nb_bytes..]; Some(b) } else { None } } fn read_i8(&mut self) -> Option { if self.is_empty().not() { let b = self[0]; *self = &self[1..]; Some(b as i8) } else { None } } fn read_i16(&mut self) -> Option { if self.len() >= 2 { let b = B::read_i16(self); *self = &self[2..]; Some(b) } else { None } } fn read_i32(&mut self) -> Option { if self.len() >= 4 { let b = B::read_i32(self); *self = &self[4..]; Some(b) } else { None } } fn read_i64(&mut self) -> Option { if self.len() >= 8 { let b = B::read_i64(self); *self = &self[8..]; Some(b) } else { None } } fn read_i128(&mut self) -> Option { if self.len() >= 16 { let b = B::read_i128(self); *self = &self[16..]; Some(b) } else { None } } fn read_int32(&mut self, nb_bytes: usize) -> Option { if nb_bytes != 0 && nb_bytes <= 4 && self.len() >= nb_bytes { let b = B::read_int(self, nb_bytes) as i32; *self = &self[nb_bytes..]; Some(b) } else { None } } fn read_int64(&mut self, nb_bytes: usize) -> Option { if nb_bytes != 0 && nb_bytes <= 8 && self.len() >= nb_bytes { let b = B::read_int(self, nb_bytes); *self = &self[nb_bytes..]; Some(b) } else { None } } fn read_int128(&mut self, nb_bytes: usize) -> Option { if nb_bytes != 0 && nb_bytes <= 16 && self.len() >= nb_bytes { let b = B::read_int128(self, nb_bytes); *self = &self[nb_bytes..]; Some(b) } else { None } } fn read_f32(&mut self) -> Option { if self.len() >= 4 { let b = B::read_f32(self); *self = &self[4..]; Some(b) } else { None } } fn read_f64(&mut self) -> Option { if self.len() >= 8 { let b = B::read_f64(self); *self = &self[8..]; Some(b) } else { None } } fn read_slice(&mut self, nb_bytes: usize) -> Option<&'a [u8]> { if self.len() >= nb_bytes { let res = &self[..nb_bytes]; *self = &self[nb_bytes..]; Some(res) } else { None } } fn read_slice_to_end(&mut self) -> &'a [u8] { self.read_slice(self.len()).unwrap() } fn move_forward(&mut self, nb_bytes: usize) -> Option<()> { if self.len() >= nb_bytes { *self = &self[nb_bytes..]; Some(()) } else { None } } } byteorder_slice-3.0.0/src/option/traits.rs000064400000000000000000000044471046102023000167660ustar 00000000000000use byteorder::ByteOrder; /// Extends &[u8] with methods for reading numbers and slices pub trait ReadSlice<'a> { /// Try to read a u8 from self fn read_u8(&mut self) -> Option; /// Try to read a u16 from self fn read_u16(&mut self) -> Option; /// Try to read a u32 from self fn read_u32(&mut self) -> Option; /// Try to read a u64 from self fn read_u64(&mut self) -> Option; /// Try to read a u128 from self fn read_u128(&mut self) -> Option; /// Try to read an unsigned n-bytes integer from self fn read_uint32(&mut self, nb_bytes: usize) -> Option; /// Try to read an unsigned n-bytes integer from self fn read_uint64(&mut self, nb_bytes: usize) -> Option; /// Try to read an unsigned n-bytes integer from self fn read_uint128(&mut self, nb_bytes: usize) -> Option; /// Try to read a i8 from self fn read_i8(&mut self) -> Option; /// Try to read a i16 from self fn read_i16(&mut self) -> Option; /// Try to read a i32 from self fn read_i32(&mut self) -> Option; /// Try to read a i64 from self fn read_i64(&mut self) -> Option; /// Try to read a i128 from self fn read_i128(&mut self) -> Option; /// Try to read a signed n-bytes integer from self fn read_int32(&mut self, nb_bytes: usize) -> Option; /// Try to read a signed n-bytes integer from self fn read_int64(&mut self, nb_bytes: usize) -> Option; /// Try to read a signed n-bytes integer from self fn read_int128(&mut self, nb_bytes: usize) -> Option; /// Try to read a f32 from self fn read_f32(&mut self) -> Option; /// Try to read a f64 from self fn read_f64(&mut self) -> Option; /// Try to read a slice from self fn read_slice(&mut self, nb_bytes: usize) -> Option<&'a [u8]>; /// Read a slice from start to end of self fn read_slice_to_end(&mut self) -> &'a [u8]; /// Try to move forward in self #[must_use] fn move_forward(&mut self, nb_bytes: usize) -> Option<()>; } byteorder_slice-3.0.0/src/result/mod.rs000064400000000000000000000000751046102023000162360ustar 00000000000000mod read_slice_impl; mod traits; pub use traits::ReadSlice; byteorder_slice-3.0.0/src/result/read_slice_impl.rs000064400000000000000000000142241046102023000205730ustar 00000000000000use std::io::Result as IoResult; use std::ops::Not; use byteorder::ByteOrder; use super::traits::ReadSlice; impl<'a> ReadSlice<'a> for &'a [u8] { fn read_u8(&mut self) -> IoResult { if self.is_empty().not() { let b = self[0]; *self = &self[1..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_u16(&mut self) -> IoResult { if self.len() >= 2 { let b = B::read_u16(self); *self = &self[2..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_u32(&mut self) -> IoResult { if self.len() >= 4 { let b = B::read_u32(self); *self = &self[4..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_u64(&mut self) -> IoResult { if self.len() >= 8 { let b = B::read_u64(self); *self = &self[8..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_u128(&mut self) -> IoResult { if self.len() >= 16 { let b = B::read_u128(self); *self = &self[16..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_uint32(&mut self, nb_bytes: usize) -> IoResult { if nb_bytes != 0 && nb_bytes <= 4 && self.len() >= nb_bytes { let b = B::read_uint(self, nb_bytes) as u32; *self = &self[nb_bytes..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_uint64(&mut self, nb_bytes: usize) -> IoResult { if nb_bytes != 0 && nb_bytes <= 8 && self.len() >= nb_bytes { let b = B::read_uint(self, nb_bytes); *self = &self[nb_bytes..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_uint128(&mut self, nb_bytes: usize) -> IoResult { if nb_bytes != 0 && nb_bytes <= 16 && self.len() >= nb_bytes { let b = B::read_uint128(self, nb_bytes); *self = &self[nb_bytes..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_i8(&mut self) -> IoResult { if self.is_empty().not() { let b = self[0]; *self = &self[1..]; Ok(b as i8) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_i16(&mut self) -> IoResult { if self.len() >= 2 { let b = B::read_i16(self); *self = &self[2..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_i32(&mut self) -> IoResult { if self.len() >= 4 { let b = B::read_i32(self); *self = &self[4..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_i64(&mut self) -> IoResult { if self.len() >= 8 { let b = B::read_i64(self); *self = &self[8..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_i128(&mut self) -> IoResult { if self.len() >= 16 { let b = B::read_i128(self); *self = &self[16..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_int32(&mut self, nb_bytes: usize) -> IoResult { if nb_bytes != 0 && nb_bytes <= 4 && self.len() >= nb_bytes { let b = B::read_int(self, nb_bytes) as i32; *self = &self[nb_bytes..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_int64(&mut self, nb_bytes: usize) -> IoResult { if nb_bytes != 0 && nb_bytes <= 8 && self.len() >= nb_bytes { let b = B::read_int(self, nb_bytes); *self = &self[nb_bytes..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_int128(&mut self, nb_bytes: usize) -> IoResult { if nb_bytes != 0 && nb_bytes <= 16 && self.len() >= nb_bytes { let b = B::read_int128(self, nb_bytes); *self = &self[nb_bytes..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_f32(&mut self) -> IoResult { if self.len() >= 4 { let b = B::read_f32(self); *self = &self[4..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_f64(&mut self) -> IoResult { if self.len() >= 8 { let b = B::read_f64(self); *self = &self[8..]; Ok(b) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_slice(&mut self, nb_bytes: usize) -> IoResult<&'a [u8]> { if self.len() >= nb_bytes { let res = &self[..nb_bytes]; *self = &self[nb_bytes..]; Ok(res) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } fn read_slice_to_end(&mut self) -> &'a [u8] { self.read_slice(self.len()).unwrap() } fn move_forward(&mut self, nb_bytes: usize) -> IoResult<()> { if self.len() >= nb_bytes { *self = &self[nb_bytes..]; Ok(()) } else { Err(std::io::ErrorKind::UnexpectedEof.into()) } } } byteorder_slice-3.0.0/src/result/traits.rs000064400000000000000000000045411046102023000167670ustar 00000000000000use std::io::Result as IoResult; use byteorder::ByteOrder; /// Extends &[u8] with methods for reading numbers and slices pub trait ReadSlice<'a> { /// Try to read a u8 from self fn read_u8(&mut self) -> IoResult; /// Try to read a u16 from self fn read_u16(&mut self) -> IoResult; /// Try to read a u32 from self fn read_u32(&mut self) -> IoResult; /// Try to read a u64 from self fn read_u64(&mut self) -> IoResult; /// Try to read a u128 from self fn read_u128(&mut self) -> IoResult; /// Try to read an unsigned n-bytes integer from self fn read_uint32(&mut self, nb_bytes: usize) -> IoResult; /// Try to read an unsigned n-bytes integer from self fn read_uint64(&mut self, nb_bytes: usize) -> IoResult; /// Try to read an unsigned n-bytes integer from self fn read_uint128(&mut self, nb_bytes: usize) -> IoResult; /// Try to read a i8 from self fn read_i8(&mut self) -> IoResult; /// Try to read a i16 from self fn read_i16(&mut self) -> IoResult; /// Try to read a i32 from self fn read_i32(&mut self) -> IoResult; /// Try to read a i64 from self fn read_i64(&mut self) -> IoResult; /// Try to read a i128 from self fn read_i128(&mut self) -> IoResult; /// Try to read a signed n-bytes integer from self fn read_int32(&mut self, nb_bytes: usize) -> IoResult; /// Try to read a signed n-bytes integer from self fn read_int64(&mut self, nb_bytes: usize) -> IoResult; /// Try to read a signed n-bytes integer from self fn read_int128(&mut self, nb_bytes: usize) -> IoResult; /// Try to read a f32 from self fn read_f32(&mut self) -> IoResult; /// Try to read a f64 from self fn read_f64(&mut self) -> IoResult; /// Try to read a slice from self fn read_slice(&mut self, nb_bytes: usize) -> IoResult<&'a [u8]>; /// Read a slice from start to end of self fn read_slice_to_end(&mut self) -> &'a [u8]; /// Try to move forward in self fn move_forward(&mut self, nb_bytes: usize) -> IoResult<()>; } byteorder_slice-3.0.0/tests/option.rs000064400000000000000000000314571046102023000160340ustar 00000000000000use byteorder_slice::option::ReadSlice; use byteorder_slice::{BigEndian, LittleEndian}; use hex_literal::hex; #[test] fn u8() { let mut data = hex!("01 80").as_ref(); assert!(matches!(data.read_u8(), Some(1))); assert!(matches!(data.read_u8(), Some(128))); assert!(matches!(data.read_u8(), None)); } #[test] fn i8() { let mut data = hex!("01 80").as_ref(); assert!(matches!(data.read_i8(), Some(1))); assert!(matches!(data.read_i8(), Some(-128))); assert!(matches!(data.read_i8(), None)); } #[test] fn u16_be() { let mut data = hex!("0102 8000").as_ref(); assert!(matches!(data.read_u16::(), Some(258))); assert!(matches!(data.read_u16::(), Some(32768))); assert!(matches!(data.read_u16::(), None)); } #[test] fn u16_le() { let mut data = hex!("0201 0080").as_ref(); assert!(matches!(data.read_u16::(), Some(258))); assert!(matches!(data.read_u16::(), Some(32768))); assert!(matches!(data.read_u16::(), None)); } #[test] fn i16_be() { let mut data = hex!("0102 8000").as_ref(); assert!(matches!(data.read_i16::(), Some(258))); assert!(matches!(data.read_i16::(), Some(-32768))); assert!(matches!(data.read_i16::(), None)); } #[test] fn i16_le() { let mut data = hex!("0201 0080").as_ref(); assert!(matches!(data.read_i16::(), Some(258))); assert!(matches!(data.read_i16::(), Some(-32768))); assert!(matches!(data.read_i16::(), None)); } #[test] fn u32_be() { let mut data = hex!("01020304 80000000").as_ref(); assert!(matches!(data.read_u32::(), Some(16909060))); assert!(matches!(data.read_u32::(), Some(2147483648))); assert!(matches!(data.read_u32::(), None)); } #[test] fn u32_le() { let mut data = hex!("04030201 00000080").as_ref(); assert!(matches!(data.read_u32::(), Some(16909060))); assert!(matches!(data.read_u32::(), Some(2147483648))); assert!(matches!(data.read_u32::(), None)); } #[test] fn i32_be() { let mut data = hex!("01020304 80000000").as_ref(); assert!(matches!(data.read_i32::(), Some(16909060))); assert!(matches!(data.read_i32::(), Some(-2147483648))); assert!(matches!(data.read_i32::(), None)); } #[test] fn i32_le() { let mut data = hex!("04030201 00000080").as_ref(); assert!(matches!(data.read_i32::(), Some(16909060))); assert!(matches!(data.read_i32::(), Some(-2147483648))); assert!(matches!(data.read_i32::(), None)); } #[test] fn u64_be() { let mut data = hex!("0102030405060708 8000000000000000").as_ref(); assert!(matches!(data.read_u64::(), Some(72623859790382856))); assert!(matches!(data.read_u64::(), Some(9223372036854775808))); assert!(matches!(data.read_u64::(), None)); } #[test] fn u64_le() { let mut data = hex!("0807060504030201 0000000000000080").as_ref(); assert!(matches!(data.read_u64::(), Some(72623859790382856))); assert!(matches!(data.read_u64::(), Some(9223372036854775808))); assert!(matches!(data.read_u64::(), None)); } #[test] fn i64_be() { let mut data = hex!("0102030405060708 8000000000000000").as_ref(); assert!(matches!(data.read_i64::(), Some(72623859790382856))); assert!(matches!(data.read_i64::(), Some(-9223372036854775808))); assert!(matches!(data.read_i64::(), None)); } #[test] fn i64_le() { let mut data = hex!("0807060504030201 0000000000000080").as_ref(); assert!(matches!(data.read_i64::(), Some(72623859790382856))); assert!(matches!(data.read_i64::(), Some(-9223372036854775808))); assert!(matches!(data.read_i64::(), None)); } #[test] fn u128_be() { let mut data = hex!("0102030405060708090A0B0C0D0E0F10 80000000000000000000000000000000").as_ref(); assert!(matches!(data.read_u128::(), Some(1339673755198158349044581307228491536))); assert!(matches!(data.read_u128::(), Some(170141183460469231731687303715884105728))); assert!(matches!(data.read_u128::(), None)); } #[test] fn u128_le() { let mut data = hex!("0102030405060708090A0B0C0D0E0F10 80000000000000000000000000000000").as_ref(); assert!(matches!(data.read_u128::(), Some(1339673755198158349044581307228491536))); assert!(matches!(data.read_u128::(), Some(170141183460469231731687303715884105728))); assert!(matches!(data.read_u128::(), None)); } #[test] fn i128_be() { let mut data = hex!("0102030405060708090A0B0C0D0E0F10 80000000000000000000000000000000").as_ref(); assert!(matches!(data.read_i128::(), Some(1339673755198158349044581307228491536))); assert!(matches!(data.read_i128::(), Some(-170141183460469231731687303715884105728))); assert!(matches!(data.read_i128::(), None)); } #[test] fn i128_le() { let mut data = hex!("100F0E0D0C0B0A090807060504030201 00000000000000000000000000000080").as_ref(); assert!(matches!(data.read_i128::(), Some(1339673755198158349044581307228491536))); assert!(matches!(data.read_i128::(), Some(-170141183460469231731687303715884105728))); assert!(matches!(data.read_i128::(), None)); } #[test] fn uint_be() { let mut data = hex!("01 0102 010203 01020304 0102030405 010203040506 01020304050607 0102030405060708 010203040506070809 0102030405060708090A 0102030405060708090A0B 0102030405060708090A0B0C 0102030405060708090A0B0C0D 0102030405060708090A0B0C0D0E 0102030405060708090A0B0C0D0E0F 0102030405060708090A0B0C0D0E0F10 0102030405060708090A0B0C0D0E0F1011").as_ref(); assert!(matches!(data.read_uint128::(1), Some(1))); assert!(matches!(data.read_uint128::(2), Some(258))); assert!(matches!(data.read_uint128::(3), Some(66051))); assert!(matches!(data.read_uint128::(4), Some(16909060))); assert!(matches!(data.read_uint128::(5), Some(4328719365))); assert!(matches!(data.read_uint128::(6), Some(1108152157446))); assert!(matches!(data.read_uint128::(7), Some(283686952306183))); assert!(matches!(data.read_uint128::(8), Some(72623859790382856))); assert!(matches!(data.read_uint128::(9), Some(18591708106338011145))); assert!(matches!(data.read_uint128::(10), Some(4759477275222530853130))); assert!(matches!(data.read_uint128::(11), Some(1218426182456967898401291))); assert!(matches!(data.read_uint128::(12), Some(311917102708983781990730508))); assert!(matches!(data.read_uint128::(13), Some(79850778293499848189627010061))); assert!(matches!(data.read_uint128::(14), Some(20441799243135961136544514575630))); assert!(matches!(data.read_uint128::(15), Some(5233100606242806050955395731361295))); assert!(matches!(data.read_uint128::(16), Some(1339673755198158349044581307228491536))); } #[test] fn uint_le() { let mut data = hex!("01 0201 030201 04030201 0504030201 060504030201 07060504030201 0807060504030201 090807060504030201 0A090807060504030201 0B0A090807060504030201 0C0B0A090807060504030201 0D0C0B0A090807060504030201 0E0D0C0B0A090807060504030201 0F0E0D0C0B0A090807060504030201 100F0E0D0C0B0A090807060504030201 11100F0E0D0C0B0A090807060504030201").as_ref(); assert!(matches!(data.read_uint128::(1), Some(1))); assert!(matches!(data.read_uint128::(2), Some(258))); assert!(matches!(data.read_uint128::(3), Some(66051))); assert!(matches!(data.read_uint128::(4), Some(16909060))); assert!(matches!(data.read_uint128::(5), Some(4328719365))); assert!(matches!(data.read_uint128::(6), Some(1108152157446))); assert!(matches!(data.read_uint128::(7), Some(283686952306183))); assert!(matches!(data.read_uint128::(8), Some(72623859790382856))); assert!(matches!(data.read_uint128::(9), Some(18591708106338011145))); assert!(matches!(data.read_uint128::(10), Some(4759477275222530853130))); assert!(matches!(data.read_uint128::(11), Some(1218426182456967898401291))); assert!(matches!(data.read_uint128::(12), Some(311917102708983781990730508))); assert!(matches!(data.read_uint128::(13), Some(79850778293499848189627010061))); assert!(matches!(data.read_uint128::(14), Some(20441799243135961136544514575630))); assert!(matches!(data.read_uint128::(15), Some(5233100606242806050955395731361295))); assert!(matches!(data.read_uint128::(16), Some(1339673755198158349044581307228491536))); } #[test] fn int_be() { let mut data = hex!("81 8182 818283 81828384 8182838485 818283848586 81828384858687 8182838485868788 818283848586878889 8182838485868788898A 8182838485868788898A8B 8182838485868788898A8B8C 8182838485868788898A8B8C8D 8182838485868788898A8B8C8D8E 8182838485868788898A8B8C8D8E8F 8182838485868788898A8B8C8D8E8F18 8182838485868788898A8B8C8D8E8F1811").as_ref(); assert!(matches!(data.read_int128::(1), Some(-127))); assert!(matches!(data.read_int128::(2), Some(-32382))); assert!(matches!(data.read_int128::(3), Some(-8289661))); assert!(matches!(data.read_int128::(4), Some(-2122153084))); assert!(matches!(data.read_int128::(5), Some(-543271189371))); assert!(matches!(data.read_int128::(6), Some(-139077424478842))); assert!(matches!(data.read_int128::(7), Some(-35603820666583417))); assert!(matches!(data.read_int128::(8), Some(-9114578090645354616))); assert!(matches!(data.read_int128::(9), Some(-2333331991205210781559))); assert!(matches!(data.read_int128::(10), Some(-597332989748533960078966))); assert!(matches!(data.read_int128::(11), Some(-152917245375624693780215157))); assert!(matches!(data.read_int128::(12), Some(-39146814816159921607735080052))); assert!(matches!(data.read_int128::(13), Some(-10021584592936939931580180493171))); assert!(matches!(data.read_int128::(14), Some(-2565525655791856622484526206251634))); assert!(matches!(data.read_int128::(15), Some(-656774567882715295356038708800418161))); assert!(matches!(data.read_int128::(16), Some(-168134289377975115611145909452907049192))); assert!(matches!(hex!("FF7F").as_ref().read_int128::(2), Some(-129))); } #[test] fn int_le() { let mut data = hex!("81 8281 838281 84838281 8584838281 868584838281 87868584838281 8887868584838281 898887868584838281 8A898887868584838281 8B8A898887868584838281 8C8B8A898887868584838281 8D8C8B8A898887868584838281 8E8D8C8B8A898887868584838281 8F8E8D8C8B8A898887868584838281 188F8E8D8C8B8A898887868584838281 11188F8E8D8C8B8A898887868584838281").as_ref(); assert!(matches!(data.read_int128::(1), Some(-127))); assert!(matches!(data.read_int128::(2), Some(-32382))); assert!(matches!(data.read_int128::(3), Some(-8289661))); assert!(matches!(data.read_int128::(4), Some(-2122153084))); assert!(matches!(data.read_int128::(5), Some(-543271189371))); assert!(matches!(data.read_int128::(6), Some(-139077424478842))); assert!(matches!(data.read_int128::(7), Some(-35603820666583417))); assert!(matches!(data.read_int128::(8), Some(-9114578090645354616))); assert!(matches!(data.read_int128::(9), Some(-2333331991205210781559))); assert!(matches!(data.read_int128::(10), Some(-597332989748533960078966))); assert!(matches!(data.read_int128::(11), Some(-152917245375624693780215157))); assert!(matches!(data.read_int128::(12), Some(-39146814816159921607735080052))); assert!(matches!(data.read_int128::(13), Some(-10021584592936939931580180493171))); assert!(matches!(data.read_int128::(14), Some(-2565525655791856622484526206251634))); assert!(matches!(data.read_int128::(15), Some(-656774567882715295356038708800418161))); assert!(matches!(data.read_int128::(16), Some(-168134289377975115611145909452907049192))); } byteorder_slice-3.0.0/tests/result.rs000064400000000000000000000312111046102023000160260ustar 00000000000000use byteorder_slice::result::ReadSlice; use byteorder_slice::{BigEndian, LittleEndian}; use hex_literal::hex; #[test] fn u8() { let mut data = hex!("01 80").as_ref(); assert!(matches!(data.read_u8(), Ok(1))); assert!(matches!(data.read_u8(), Ok(128))); assert!(matches!(data.read_u8(), Err(_))); } #[test] fn i8() { let mut data = hex!("01 80").as_ref(); assert!(matches!(data.read_i8(), Ok(1))); assert!(matches!(data.read_i8(), Ok(-128))); assert!(matches!(data.read_i8(), Err(_))); } #[test] fn u16_be() { let mut data = hex!("0102 8000").as_ref(); assert!(matches!(data.read_u16::(), Ok(258))); assert!(matches!(data.read_u16::(), Ok(32768))); assert!(matches!(data.read_u16::(), Err(_))); } #[test] fn u16_le() { let mut data = hex!("0201 0080").as_ref(); assert!(matches!(data.read_u16::(), Ok(258))); assert!(matches!(data.read_u16::(), Ok(32768))); assert!(matches!(data.read_u16::(), Err(_))); } #[test] fn i16_be() { let mut data = hex!("0102 8000").as_ref(); assert!(matches!(data.read_i16::(), Ok(258))); assert!(matches!(data.read_i16::(), Ok(-32768))); assert!(matches!(data.read_i16::(), Err(_))); } #[test] fn i16_le() { let mut data = hex!("0201 0080").as_ref(); assert!(matches!(data.read_i16::(), Ok(258))); assert!(matches!(data.read_i16::(), Ok(-32768))); assert!(matches!(data.read_i16::(), Err(_))); } #[test] fn u32_be() { let mut data = hex!("01020304 80000000").as_ref(); assert!(matches!(data.read_u32::(), Ok(16909060))); assert!(matches!(data.read_u32::(), Ok(2147483648))); assert!(matches!(data.read_u32::(), Err(_))); } #[test] fn u32_le() { let mut data = hex!("04030201 00000080").as_ref(); assert!(matches!(data.read_u32::(), Ok(16909060))); assert!(matches!(data.read_u32::(), Ok(2147483648))); assert!(matches!(data.read_u32::(), Err(_))); } #[test] fn i32_be() { let mut data = hex!("01020304 80000000").as_ref(); assert!(matches!(data.read_i32::(), Ok(16909060))); assert!(matches!(data.read_i32::(), Ok(-2147483648))); assert!(matches!(data.read_i32::(), Err(_))); } #[test] fn i32_le() { let mut data = hex!("04030201 00000080").as_ref(); assert!(matches!(data.read_i32::(), Ok(16909060))); assert!(matches!(data.read_i32::(), Ok(-2147483648))); assert!(matches!(data.read_i32::(), Err(_))); } #[test] fn u64_be() { let mut data = hex!("0102030405060708 8000000000000000").as_ref(); assert!(matches!(data.read_u64::(), Ok(72623859790382856))); assert!(matches!(data.read_u64::(), Ok(9223372036854775808))); assert!(matches!(data.read_u64::(), Err(_))); } #[test] fn u64_le() { let mut data = hex!("0807060504030201 0000000000000080").as_ref(); assert!(matches!(data.read_u64::(), Ok(72623859790382856))); assert!(matches!(data.read_u64::(), Ok(9223372036854775808))); assert!(matches!(data.read_u64::(), Err(_))); } #[test] fn i64_be() { let mut data = hex!("0102030405060708 8000000000000000").as_ref(); assert!(matches!(data.read_i64::(), Ok(72623859790382856))); assert!(matches!(data.read_i64::(), Ok(-9223372036854775808))); assert!(matches!(data.read_i64::(), Err(_))); } #[test] fn i64_le() { let mut data = hex!("0807060504030201 0000000000000080").as_ref(); assert!(matches!(data.read_i64::(), Ok(72623859790382856))); assert!(matches!(data.read_i64::(), Ok(-9223372036854775808))); assert!(matches!(data.read_i64::(), Err(_))); } #[test] fn u128_be() { let mut data = hex!("0102030405060708090A0B0C0D0E0F10 80000000000000000000000000000000").as_ref(); assert!(matches!(data.read_u128::(), Ok(1339673755198158349044581307228491536))); assert!(matches!(data.read_u128::(), Ok(170141183460469231731687303715884105728))); assert!(matches!(data.read_u128::(), Err(_))); } #[test] fn u128_le() { let mut data = hex!("0102030405060708090A0B0C0D0E0F10 80000000000000000000000000000000").as_ref(); assert!(matches!(data.read_u128::(), Ok(1339673755198158349044581307228491536))); assert!(matches!(data.read_u128::(), Ok(170141183460469231731687303715884105728))); assert!(matches!(data.read_u128::(), Err(_))); } #[test] fn i128_be() { let mut data = hex!("0102030405060708090A0B0C0D0E0F10 80000000000000000000000000000000").as_ref(); assert!(matches!(data.read_i128::(), Ok(1339673755198158349044581307228491536))); assert!(matches!(data.read_i128::(), Ok(-170141183460469231731687303715884105728))); assert!(matches!(data.read_i128::(), Err(_))); } #[test] fn i128_le() { let mut data = hex!("100F0E0D0C0B0A090807060504030201 00000000000000000000000000000080").as_ref(); assert!(matches!(data.read_i128::(), Ok(1339673755198158349044581307228491536))); assert!(matches!(data.read_i128::(), Ok(-170141183460469231731687303715884105728))); assert!(matches!(data.read_i128::(), Err(_))); } #[test] fn uint_be() { let mut data = hex!("01 0102 010203 01020304 0102030405 010203040506 01020304050607 0102030405060708 010203040506070809 0102030405060708090A 0102030405060708090A0B 0102030405060708090A0B0C 0102030405060708090A0B0C0D 0102030405060708090A0B0C0D0E 0102030405060708090A0B0C0D0E0F 0102030405060708090A0B0C0D0E0F10 0102030405060708090A0B0C0D0E0F1011").as_ref(); assert!(matches!(data.read_uint128::(1), Ok(1))); assert!(matches!(data.read_uint128::(2), Ok(258))); assert!(matches!(data.read_uint128::(3), Ok(66051))); assert!(matches!(data.read_uint128::(4), Ok(16909060))); assert!(matches!(data.read_uint128::(5), Ok(4328719365))); assert!(matches!(data.read_uint128::(6), Ok(1108152157446))); assert!(matches!(data.read_uint128::(7), Ok(283686952306183))); assert!(matches!(data.read_uint128::(8), Ok(72623859790382856))); assert!(matches!(data.read_uint128::(9), Ok(18591708106338011145))); assert!(matches!(data.read_uint128::(10), Ok(4759477275222530853130))); assert!(matches!(data.read_uint128::(11), Ok(1218426182456967898401291))); assert!(matches!(data.read_uint128::(12), Ok(311917102708983781990730508))); assert!(matches!(data.read_uint128::(13), Ok(79850778293499848189627010061))); assert!(matches!(data.read_uint128::(14), Ok(20441799243135961136544514575630))); assert!(matches!(data.read_uint128::(15), Ok(5233100606242806050955395731361295))); assert!(matches!(data.read_uint128::(16), Ok(1339673755198158349044581307228491536))); } #[test] fn uint_le() { let mut data = hex!("01 0201 030201 04030201 0504030201 060504030201 07060504030201 0807060504030201 090807060504030201 0A090807060504030201 0B0A090807060504030201 0C0B0A090807060504030201 0D0C0B0A090807060504030201 0E0D0C0B0A090807060504030201 0F0E0D0C0B0A090807060504030201 100F0E0D0C0B0A090807060504030201 11100F0E0D0C0B0A090807060504030201").as_ref(); assert!(matches!(data.read_uint128::(1), Ok(1))); assert!(matches!(data.read_uint128::(2), Ok(258))); assert!(matches!(data.read_uint128::(3), Ok(66051))); assert!(matches!(data.read_uint128::(4), Ok(16909060))); assert!(matches!(data.read_uint128::(5), Ok(4328719365))); assert!(matches!(data.read_uint128::(6), Ok(1108152157446))); assert!(matches!(data.read_uint128::(7), Ok(283686952306183))); assert!(matches!(data.read_uint128::(8), Ok(72623859790382856))); assert!(matches!(data.read_uint128::(9), Ok(18591708106338011145))); assert!(matches!(data.read_uint128::(10), Ok(4759477275222530853130))); assert!(matches!(data.read_uint128::(11), Ok(1218426182456967898401291))); assert!(matches!(data.read_uint128::(12), Ok(311917102708983781990730508))); assert!(matches!(data.read_uint128::(13), Ok(79850778293499848189627010061))); assert!(matches!(data.read_uint128::(14), Ok(20441799243135961136544514575630))); assert!(matches!(data.read_uint128::(15), Ok(5233100606242806050955395731361295))); assert!(matches!(data.read_uint128::(16), Ok(1339673755198158349044581307228491536))); } #[test] fn int_be() { let mut data = hex!("81 8182 818283 81828384 8182838485 818283848586 81828384858687 8182838485868788 818283848586878889 8182838485868788898A 8182838485868788898A8B 8182838485868788898A8B8C 8182838485868788898A8B8C8D 8182838485868788898A8B8C8D8E 8182838485868788898A8B8C8D8E8F 8182838485868788898A8B8C8D8E8F18 8182838485868788898A8B8C8D8E8F1811").as_ref(); assert!(matches!(data.read_int128::(1), Ok(-127))); assert!(matches!(data.read_int128::(2), Ok(-32382))); assert!(matches!(data.read_int128::(3), Ok(-8289661))); assert!(matches!(data.read_int128::(4), Ok(-2122153084))); assert!(matches!(data.read_int128::(5), Ok(-543271189371))); assert!(matches!(data.read_int128::(6), Ok(-139077424478842))); assert!(matches!(data.read_int128::(7), Ok(-35603820666583417))); assert!(matches!(data.read_int128::(8), Ok(-9114578090645354616))); assert!(matches!(data.read_int128::(9), Ok(-2333331991205210781559))); assert!(matches!(data.read_int128::(10), Ok(-597332989748533960078966))); assert!(matches!(data.read_int128::(11), Ok(-152917245375624693780215157))); assert!(matches!(data.read_int128::(12), Ok(-39146814816159921607735080052))); assert!(matches!(data.read_int128::(13), Ok(-10021584592936939931580180493171))); assert!(matches!(data.read_int128::(14), Ok(-2565525655791856622484526206251634))); assert!(matches!(data.read_int128::(15), Ok(-656774567882715295356038708800418161))); assert!(matches!(data.read_int128::(16), Ok(-168134289377975115611145909452907049192))); assert!(matches!(hex!("FF7F").as_ref().read_int128::(2), Ok(-129))); } #[test] fn int_le() { let mut data = hex!("81 8281 838281 84838281 8584838281 868584838281 87868584838281 8887868584838281 898887868584838281 8A898887868584838281 8B8A898887868584838281 8C8B8A898887868584838281 8D8C8B8A898887868584838281 8E8D8C8B8A898887868584838281 8F8E8D8C8B8A898887868584838281 188F8E8D8C8B8A898887868584838281 11188F8E8D8C8B8A898887868584838281").as_ref(); assert!(matches!(data.read_int128::(1), Ok(-127))); assert!(matches!(data.read_int128::(2), Ok(-32382))); assert!(matches!(data.read_int128::(3), Ok(-8289661))); assert!(matches!(data.read_int128::(4), Ok(-2122153084))); assert!(matches!(data.read_int128::(5), Ok(-543271189371))); assert!(matches!(data.read_int128::(6), Ok(-139077424478842))); assert!(matches!(data.read_int128::(7), Ok(-35603820666583417))); assert!(matches!(data.read_int128::(8), Ok(-9114578090645354616))); assert!(matches!(data.read_int128::(9), Ok(-2333331991205210781559))); assert!(matches!(data.read_int128::(10), Ok(-597332989748533960078966))); assert!(matches!(data.read_int128::(11), Ok(-152917245375624693780215157))); assert!(matches!(data.read_int128::(12), Ok(-39146814816159921607735080052))); assert!(matches!(data.read_int128::(13), Ok(-10021584592936939931580180493171))); assert!(matches!(data.read_int128::(14), Ok(-2565525655791856622484526206251634))); assert!(matches!(data.read_int128::(15), Ok(-656774567882715295356038708800418161))); assert!(matches!(data.read_int128::(16), Ok(-168134289377975115611145909452907049192))); } byteorder_slice-3.0.0/tests/tests.rs000064400000000000000000000000721046102023000156530ustar 00000000000000#[rustfmt::skip] mod option; #[rustfmt::skip] mod result;