From d5e593097bed8a387d52e51de0d0b74a4fa7a246 Mon Sep 17 00:00:00 2001 From: zhangzhen Date: Fri, 12 May 2023 15:55:34 +0800 Subject: [PATCH] Issue #256: Stable sort for HashSet --- src/component/cyfs-base/src/codec/raw/raw_types.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/component/cyfs-base/src/codec/raw/raw_types.rs b/src/component/cyfs-base/src/codec/raw/raw_types.rs index f18bbeed7..9b028ad74 100644 --- a/src/component/cyfs-base/src/codec/raw/raw_types.rs +++ b/src/component/cyfs-base/src/codec/raw/raw_types.rs @@ -740,7 +740,7 @@ impl<'de, T: RawEncode + RawDecode<'de>> RawDecode<'de> for Vec { // HashSet -impl RawEncode for HashSet { +impl RawEncode for HashSet { fn raw_measure(&self, purpose: &Option) -> BuckyResult { let ulen = USize(self.len()); let mut bytes = ulen.raw_measure(purpose).unwrap(); @@ -757,7 +757,12 @@ impl RawEncode for HashSet { ) -> BuckyResult<&'a mut [u8]> { let ulen = USize(self.len()); let mut buf = ulen.raw_encode(buf, purpose)?; - for e in self { + + // stable sort + let mut values: Vec<&T> = self.iter().collect(); + values.sort(); + + for e in values { buf = e.raw_encode(buf, purpose)?; } Ok(buf)