diff --git a/src/util/comparator_test.rs b/src/util/comparator_test.rs index e1ad1b62b0e57ea8971d2b56564c216ccaf9e208..d6d311b8822e7fd7b289a6cabf13e5c2a2e2358e 100644 --- a/src/util/comparator_test.rs +++ b/src/util/comparator_test.rs @@ -20,12 +20,9 @@ mod test { let option_val = comp.compare(&Slice::from("a"), &Slice::from("ab")); assert_eq!(option_val.unwrap(), Ordering::Less); - // // todo Slice 存在 bug 未修复 - // let comp = BytewiseComparatorImpl::default(); - // let option_val = comp.compare(&Slice::from("b"), &Slice::from("abcd")); - // assert_eq!(option_val.unwrap(), Ordering::Greater); + let option_val = comp.compare(&Slice::from("b"), &Slice::from("abcd")); + assert_eq!(option_val.unwrap(), Ordering::Greater); - let comp = BytewiseComparatorImpl::default(); let option_val = comp.compare(&Slice::from("abcd"), &Slice::from("abcd")); assert_eq!(option_val.unwrap(), Ordering::Equal); } diff --git a/src/util/slice.rs b/src/util/slice.rs index 26ea8b17359ad73bd5484abfe492ba504dc74da5..6416db3ce863c7d2713b34f86cbaf72a3604b0d4 100644 --- a/src/util/slice.rs +++ b/src/util/slice.rs @@ -152,24 +152,20 @@ impl PartialEq for Slice { impl PartialOrd for Slice { /// 判断两个 slice 的大小关系 fn partial_cmp(&self, other: &Self) -> Option { - match self.size().partial_cmp(&other.size()) { - Some(Ordering::Equal) => { - let cmp = unsafe { - memcmp( - self.data.as_ptr() as *const i8, - other.data.as_ptr() as *const i8, - self.size(), - ) - }; - if cmp == 0 { - Some(Ordering::Equal) - } else if cmp > 0 { - Some(Ordering::Greater) - } else { - Some(Ordering::Less) - } - } - op => op + let min = self.size().min(other.size()); + let cmp = unsafe { + memcmp( + self.data.as_ptr() as *const i8, + other.data.as_ptr() as *const i8, + min, + ) + }; + if cmp == 0 { + self.size().partial_cmp(&other.size()) + } else if cmp > 0 { + Some(Ordering::Greater) + } else { + Some(Ordering::Less) } } } diff --git a/src/util/slice_test.rs b/src/util/slice_test.rs index a2e0ec5780b3916957f2eeb2538eeb116ff81c7f..9baf1d17fbf6e1fd0adf9c14d35919821e45acaa 100644 --- a/src/util/slice_test.rs +++ b/src/util/slice_test.rs @@ -88,7 +88,7 @@ mod test { #[test] fn test_merge2() { let mut a0 = Slice::from("123"); - let mut a2 = Slice::from("456"); + let a2 = Slice::from("456"); a0.merge(a2, None); assert_eq!(String::from("123456"), String::from(a0)); }