Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HashSet #516

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/roundtrip.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_main]
use libfuzzer_sys::fuzz_target;

use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::ffi::CString;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::num::{NonZeroI128, NonZeroI32, NonZeroU128, NonZeroU32};
Expand All @@ -14,6 +14,7 @@ use std::time::{Duration, SystemTime};
enum AllTypes {
BTreeMap(BTreeMap<u8, u8>),
HashMap(HashMap<u8, u8>),
HashSet(HashSet<u8>),
BTreeSet(BTreeSet<u8>),
VecDeque(VecDeque<AllTypes>),
Vec(Vec<AllTypes>),
Expand Down
36 changes: 35 additions & 1 deletion src/features/impl_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use crate::{
};
use core::time::Duration;
use std::{
collections::HashMap,
collections::{HashMap, HashSet},
ffi::{CStr, CString},
hash::Hash,
io::Read,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
path::{Path, PathBuf},
Expand Down Expand Up @@ -392,3 +393,36 @@ where
Ok(map)
}
}

impl<T> Decode for HashSet<T>
where
T: Decode + Eq + Hash,
{
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
let len = crate::de::decode_slice_len(decoder)?;
decoder.claim_container_read::<T>(len)?;

let mut map = HashSet::new();
for _ in 0..len {
// See the documentation on `unclaim_bytes_read` as to why we're doing this here
decoder.unclaim_bytes_read(core::mem::size_of::<T>());

let key = T::decode(decoder)?;
map.insert(key);
}
Ok(map)
}
}

impl<T> Encode for HashSet<T>
where
T: Encode,
{
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
crate::enc::encode_slice_len(encoder, self.len())?;
for item in self.iter() {
item.encode(encoder)?;
}
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//!
//! |Name |Default?|Supported types for Encode/Decode|Enabled methods |Other|
//! |------|--------|-----------------------------------------|-----------------------------------------------------------------|-----|
//! |std | Yes |`HashMap`|`decode_from_std_read` and `encode_into_std_write`|
//! |std | Yes |`HashMap` and `HashSet`|`decode_from_std_read` and `encode_into_std_write`|
//! |alloc | Yes |All common containers in alloc, like `Vec`, `String`, `Box`|`encode_to_vec`|
//! |atomic| Yes |All `Atomic*` integer types, e.g. `AtomicUsize`, and `AtomicBool`||
//! |derive| Yes |||Enables the `BorrowDecode`, `Decode` and `Encode` derive macros|
Expand Down
1 change: 1 addition & 0 deletions tests/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ fn test_container_limits() {
#[cfg(feature = "std")]
{
validate_fail::<std::collections::HashMap<i32, i32>>(slice);
validate_fail::<std::collections::HashSet<i32>>(slice);
}
}
}
5 changes: 5 additions & 0 deletions tests/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ fn test_std_commons() {
map.insert("you".to_owned(), "doing?".to_owned());
the_same(map);

let mut set = std::collections::HashSet::new();
set.insert("Hello".to_string());
set.insert("World".to_string());
the_same(set);

// Borrowed values
let config = bincode::config::standard();
let mut buffer = [0u8; 1024];
Expand Down