Skip to content

Commit

Permalink
Issue buckyos#256: Stable sort for HashSet
Browse files Browse the repository at this point in the history
  • Loading branch information
streetycat committed May 8, 2023
1 parent 510347c commit 1dd34c7
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/component/cyfs-base/src/codec/raw/raw_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ impl<'de, T: RawEncode + RawDecode<'de>> RawDecode<'de> for Vec<T> {

// HashSet<T>

impl<T: RawEncode> RawEncode for HashSet<T> {
impl<T: RawEncode + std::cmp::Ord> RawEncode for HashSet<T> {
fn raw_measure(&self, purpose: &Option<RawEncodePurpose>) -> BuckyResult<usize> {
let ulen = USize(self.len());
let mut bytes = ulen.raw_measure(purpose).unwrap();
Expand All @@ -757,7 +757,12 @@ impl<T: RawEncode> RawEncode for HashSet<T> {
) -> 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)
Expand Down

0 comments on commit 1dd34c7

Please sign in to comment.