mirror of https://git.sr.ht/~stygianentity/bincode
Added HashSet (#516)
* Added HashSet * Added hashset to the same tests that hashmap has
This commit is contained in:
parent
03450ac49f
commit
caa71b5d9f
|
|
@ -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};
|
||||
|
|
@ -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>),
|
||||
|
|
|
|||
|
|
@ -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},
|
||||
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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|
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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];
|
||||
|
|
|
|||
Loading…
Reference in New Issue