diff --git a/src/util/coding.rs b/src/util/coding.rs index 4fe6fd17528da771ba9346a9e4d8546ffc91d029..4359aa23222f9d82e4922e8b49a56b8e4096134f 100644 --- a/src/util/coding.rs +++ b/src/util/coding.rs @@ -33,17 +33,16 @@ pub fn varint_length(mut value: u64) -> usize { len } -/// 默认为大端bytes 小端bytes会转为大端bytes +/// 默认为小端bytes 大端bytes会转为小端bytes +#[cfg(target_endian = "little")] macro_rules! swap_bytes { - ($x: expr, noswap) => ($x); - ($x: expr, swap) => ($x.swap_bytes()); - ($x:expr)=>{ - if cfg!(target_endian = "big") { - swap_bytes!($x, noswap) - } else { - swap_bytes!($x, swap) - } - } + ($x:expr) => ($x) +} + +/// 默认为小端bytes 大端bytes会转为小端bytes +#[cfg(target_endian = "big")] +macro_rules! swap_bytes { + ($x:expr) => ($x.swap_bytes()) } /// 判断数据类型所需的字节数 @@ -1437,7 +1436,6 @@ fn test_read_buf() { let buf = unsafe { uncheck_read_buf(&Vector(&vec), 5, 4) }; println!("{:?}", buf); assert_eq!(&[1_u8, 2, 3, 4] as &[u8; 4], buf.deref()); - } #[test] @@ -1854,4 +1852,18 @@ fn test_type_capacity() { let type_capacity = type_capacity!(u64); println!("u64: {}", type_capacity); assert_eq!(8, type_capacity); +} + +#[test] +fn test_swap_bytes() { + let value = 0x04030201_u32; + let new_value = swap_bytes!(value); + println!("value: {:?}, new_value: {:?}", value, new_value); + assert_eq!(value, new_value); + // 小端存储bytes + let mut buf = [0x01, 0x02, 0x03, 0x04]; + let decode = unsafe { uncheck_decode_fixed32(&Buffer(&buf), 0) }; + // 小端存储的0x01,0x02,0x03,0x04解出来的数据要等于0x04030201_u32 + println!("value: {:?}, decode: {:?}", value, decode); + assert_eq!(value, decode); } \ No newline at end of file diff --git a/src/util/hash.rs b/src/util/hash.rs index 575ac3ca4e2ed3735524ac70e02000bee17d98c8..71a02198f601caf62c0584258738f9448cb5c6a4 100644 --- a/src/util/hash.rs +++ b/src/util/hash.rs @@ -144,7 +144,7 @@ impl Hash { // 每次按照四字节长度读取字节流中的数据 w,并使用普通的哈希函数计算哈希值。 let mut position: usize = 0; - while position + 4 <= limit { + while decoder.can_get() && position + 4 <= limit { //每次解码前4个字节,直到最后剩下小于4个字节 // rust的 &[u8] 是胖指针,带长度信息的,会做range check,所以是安全的。 // 虽然decode_fixed32 中也是解码4字节,但传入整个data在方法上不明确,因此传 [position..(position + 4)], 可以更加方便理解,对性能无影响