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 12, 2023
1 parent 0ab7ca7 commit b5e461b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 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,13 +757,17 @@ 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)
}
}

impl<'de, T: Eq + Hash + RawEncode + RawDecode<'de>> RawDecode<'de> for HashSet<T> {
fn raw_decode(buf: &'de [u8]) -> BuckyResult<(Self, &'de [u8])> {
let (ulen, mut buf) = USize::raw_decode(buf)?;
Expand Down

0 comments on commit b5e461b

Please sign in to comment.