| 1 |
use crate::{ |
| 2 |
config::Config, |
| 3 |
de::{read::Reader, BorrowDecode, BorrowDecoder, Decode, Decoder, DecoderImpl}, |
| 4 |
enc::{write::Writer, Encode, Encoder, EncoderImpl}, |
| 5 |
error::{DecodeError, EncodeError}, |
| 6 |
impl_borrow_decode, |
| 7 |
}; |
| 8 |
use core::time::Duration; |
| 9 |
use std::{ |
| 10 |
collections::{HashMap, HashSet}, |
| 11 |
ffi::{CStr, CString}, |
| 12 |
hash::Hash, |
| 13 |
io::Read, |
| 14 |
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}, |
| 15 |
path::{Path, PathBuf}, |
| 16 |
sync::{Mutex, RwLock}, |
| 17 |
time::SystemTime, |
| 18 |
}; |
| 19 |
|
| 20 |
/// Decode type `D` from the given reader with the given `Config`. The reader can be any type that implements `std::io::Read`, e.g. `std::fs::File`. |
| 21 |
/// |
| 22 |
/// See the [config] module for more information about config options. |
| 23 |
/// |
| 24 |
/// [config]: config/index.html |
| 25 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 26 |
pub fn decode_from_std_read<D: Decode<()>, C: Config, R: std::io::Read>( |
| 27 |
src: &mut R, |
| 28 |
config: C, |
| 29 |
) -> Result<D, DecodeError> { |
| 30 |
decode_from_std_read_with_context(src, config, ()) |
| 31 |
} |
| 32 |
|
| 33 |
/// Decode type `D` from the given reader with the given `Config` and `Context`. The reader can be any type that implements `std::io::Read`, e.g. `std::fs::File`. |
| 34 |
/// |
| 35 |
/// See the [config] module for more information about config options. |
| 36 |
/// |
| 37 |
/// [config]: config/index.html |
| 38 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 39 |
pub fn decode_from_std_read_with_context< |
| 40 |
Context, |
| 41 |
D: Decode<Context>, |
| 42 |
C: Config, |
| 43 |
R: std::io::Read, |
| 44 |
>( |
| 45 |
src: &mut R, |
| 46 |
config: C, |
| 47 |
context: Context, |
| 48 |
) -> Result<D, DecodeError> { |
| 49 |
let reader = IoReader::new(src); |
| 50 |
let mut decoder = DecoderImpl::<_, C, Context>::new(reader, config, context); |
| 51 |
D::decode(&mut decoder) |
| 52 |
} |
| 53 |
|
| 54 |
pub(crate) struct IoReader<R> { |
| 55 |
reader: R, |
| 56 |
} |
| 57 |
|
| 58 |
impl<R> IoReader<R> { |
| 59 |
pub const fn new(reader: R) -> Self { |
| 60 |
Self { reader } |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl<R> Reader for IoReader<R> |
| 65 |
where |
| 66 |
R: std::io::Read, |
| 67 |
{ |
| 68 |
#[inline(always)] |
| 69 |
fn read(&mut self, bytes: &mut [u8]) -> Result<(), DecodeError> { |
| 70 |
self.reader |
| 71 |
.read_exact(bytes) |
| 72 |
.map_err(|inner| DecodeError::Io { |
| 73 |
inner, |
| 74 |
additional: bytes.len(), |
| 75 |
}) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
impl<R> Reader for std::io::BufReader<R> |
| 80 |
where |
| 81 |
R: std::io::Read, |
| 82 |
{ |
| 83 |
fn read(&mut self, bytes: &mut [u8]) -> Result<(), DecodeError> { |
| 84 |
self.read_exact(bytes).map_err(|inner| DecodeError::Io { |
| 85 |
inner, |
| 86 |
additional: bytes.len(), |
| 87 |
}) |
| 88 |
} |
| 89 |
|
| 90 |
#[inline] |
| 91 |
fn peek_read(&mut self, n: usize) -> Option<&[u8]> { |
| 92 |
self.buffer().get(..n) |
| 93 |
} |
| 94 |
|
| 95 |
#[inline] |
| 96 |
fn consume(&mut self, n: usize) { |
| 97 |
<Self as std::io::BufRead>::consume(self, n); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/// Encode the given value into any type that implements `std::io::Write`, e.g. `std::fs::File`, with the given `Config`. |
| 102 |
/// See the [config] module for more information. |
| 103 |
/// Returns the amount of bytes written. |
| 104 |
/// |
| 105 |
/// [config]: config/index.html |
| 106 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 107 |
pub fn encode_into_std_write<E: Encode, C: Config, W: std::io::Write>( |
| 108 |
val: E, |
| 109 |
dst: &mut W, |
| 110 |
config: C, |
| 111 |
) -> Result<usize, EncodeError> { |
| 112 |
let writer = IoWriter::new(dst); |
| 113 |
let mut encoder = EncoderImpl::<_, C>::new(writer, config); |
| 114 |
val.encode(&mut encoder)?; |
| 115 |
Ok(encoder.into_writer().bytes_written()) |
| 116 |
} |
| 117 |
|
| 118 |
pub(crate) struct IoWriter<'a, W: std::io::Write> { |
| 119 |
writer: &'a mut W, |
| 120 |
bytes_written: usize, |
| 121 |
} |
| 122 |
|
| 123 |
impl<'a, W: std::io::Write> IoWriter<'a, W> { |
| 124 |
pub fn new(writer: &'a mut W) -> Self { |
| 125 |
Self { |
| 126 |
writer, |
| 127 |
bytes_written: 0, |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
pub const fn bytes_written(&self) -> usize { |
| 132 |
self.bytes_written |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
impl<W: std::io::Write> Writer for IoWriter<'_, W> { |
| 137 |
#[inline(always)] |
| 138 |
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> { |
| 139 |
self.writer |
| 140 |
.write_all(bytes) |
| 141 |
.map_err(|inner| EncodeError::Io { |
| 142 |
inner, |
| 143 |
index: self.bytes_written, |
| 144 |
})?; |
| 145 |
self.bytes_written += bytes.len(); |
| 146 |
Ok(()) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
impl Encode for &CStr { |
| 151 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 152 |
self.to_bytes().encode(encoder) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl Encode for CString { |
| 157 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 158 |
self.as_bytes().encode(encoder) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
impl<Context> Decode<Context> for CString { |
| 163 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 164 |
let vec = std::vec::Vec::decode(decoder)?; |
| 165 |
CString::new(vec).map_err(|inner| DecodeError::CStringNulError { |
| 166 |
position: inner.nul_position(), |
| 167 |
}) |
| 168 |
} |
| 169 |
} |
| 170 |
impl_borrow_decode!(CString); |
| 171 |
|
| 172 |
impl<T> Encode for Mutex<T> |
| 173 |
where |
| 174 |
T: Encode, |
| 175 |
{ |
| 176 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 177 |
let t = self.lock().map_err(|_| EncodeError::LockFailed { |
| 178 |
type_name: core::any::type_name::<Mutex<T>>(), |
| 179 |
})?; |
| 180 |
t.encode(encoder) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
impl<Context, T> Decode<Context> for Mutex<T> |
| 185 |
where |
| 186 |
T: Decode<Context>, |
| 187 |
{ |
| 188 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 189 |
let t = T::decode(decoder)?; |
| 190 |
Ok(Mutex::new(t)) |
| 191 |
} |
| 192 |
} |
| 193 |
impl<'de, T, Context> BorrowDecode<'de, Context> for Mutex<T> |
| 194 |
where |
| 195 |
T: BorrowDecode<'de, Context>, |
| 196 |
{ |
| 197 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 198 |
decoder: &mut D, |
| 199 |
) -> Result<Self, DecodeError> { |
| 200 |
let t = T::borrow_decode(decoder)?; |
| 201 |
Ok(Mutex::new(t)) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
impl<T> Encode for RwLock<T> |
| 206 |
where |
| 207 |
T: Encode, |
| 208 |
{ |
| 209 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 210 |
let t = self.read().map_err(|_| EncodeError::LockFailed { |
| 211 |
type_name: core::any::type_name::<RwLock<T>>(), |
| 212 |
})?; |
| 213 |
t.encode(encoder) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
impl<Context, T> Decode<Context> for RwLock<T> |
| 218 |
where |
| 219 |
T: Decode<Context>, |
| 220 |
{ |
| 221 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 222 |
let t = T::decode(decoder)?; |
| 223 |
Ok(RwLock::new(t)) |
| 224 |
} |
| 225 |
} |
| 226 |
impl<'de, T, Context> BorrowDecode<'de, Context> for RwLock<T> |
| 227 |
where |
| 228 |
T: BorrowDecode<'de, Context>, |
| 229 |
{ |
| 230 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 231 |
decoder: &mut D, |
| 232 |
) -> Result<Self, DecodeError> { |
| 233 |
let t = T::borrow_decode(decoder)?; |
| 234 |
Ok(RwLock::new(t)) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
impl Encode for SystemTime { |
| 239 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 240 |
let duration = self.duration_since(SystemTime::UNIX_EPOCH).map_err(|e| { |
| 241 |
EncodeError::InvalidSystemTime { |
| 242 |
inner: e, |
| 243 |
time: std::boxed::Box::new(*self), |
| 244 |
} |
| 245 |
})?; |
| 246 |
duration.encode(encoder) |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
impl<Context> Decode<Context> for SystemTime { |
| 251 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 252 |
let duration = Duration::decode(decoder)?; |
| 253 |
match SystemTime::UNIX_EPOCH.checked_add(duration) { |
| 254 |
Some(t) => Ok(t), |
| 255 |
None => Err(DecodeError::InvalidSystemTime { duration }), |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
impl_borrow_decode!(SystemTime); |
| 260 |
|
| 261 |
impl Encode for &'_ Path { |
| 262 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 263 |
match self.to_str() { |
| 264 |
Some(str) => str.encode(encoder), |
| 265 |
None => Err(EncodeError::InvalidPathCharacters), |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
impl<'de, Context> BorrowDecode<'de, Context> for &'de Path { |
| 271 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 272 |
decoder: &mut D, |
| 273 |
) -> Result<Self, DecodeError> { |
| 274 |
let str = <&'de str>::borrow_decode(decoder)?; |
| 275 |
Ok(Path::new(str)) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
impl Encode for PathBuf { |
| 280 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 281 |
self.as_path().encode(encoder) |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
impl<Context> Decode<Context> for PathBuf { |
| 286 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 287 |
let string = std::string::String::decode(decoder)?; |
| 288 |
Ok(string.into()) |
| 289 |
} |
| 290 |
} |
| 291 |
impl_borrow_decode!(PathBuf); |
| 292 |
|
| 293 |
impl Encode for IpAddr { |
| 294 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 295 |
match self { |
| 296 |
IpAddr::V4(v4) => { |
| 297 |
0u32.encode(encoder)?; |
| 298 |
v4.encode(encoder) |
| 299 |
} |
| 300 |
IpAddr::V6(v6) => { |
| 301 |
1u32.encode(encoder)?; |
| 302 |
v6.encode(encoder) |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
impl<Context> Decode<Context> for IpAddr { |
| 309 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 310 |
match u32::decode(decoder)? { |
| 311 |
0 => Ok(IpAddr::V4(Ipv4Addr::decode(decoder)?)), |
| 312 |
1 => Ok(IpAddr::V6(Ipv6Addr::decode(decoder)?)), |
| 313 |
found => Err(DecodeError::UnexpectedVariant { |
| 314 |
allowed: &crate::error::AllowedEnumVariants::Range { min: 0, max: 1 }, |
| 315 |
found, |
| 316 |
type_name: core::any::type_name::<IpAddr>(), |
| 317 |
}), |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
impl_borrow_decode!(IpAddr); |
| 322 |
|
| 323 |
impl Encode for Ipv4Addr { |
| 324 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 325 |
encoder.writer().write(&self.octets()) |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
impl<Context> Decode<Context> for Ipv4Addr { |
| 330 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 331 |
let mut buff = [0u8; 4]; |
| 332 |
decoder.reader().read(&mut buff)?; |
| 333 |
Ok(Self::from(buff)) |
| 334 |
} |
| 335 |
} |
| 336 |
impl_borrow_decode!(Ipv4Addr); |
| 337 |
|
| 338 |
impl Encode for Ipv6Addr { |
| 339 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 340 |
encoder.writer().write(&self.octets()) |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
impl<Context> Decode<Context> for Ipv6Addr { |
| 345 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 346 |
let mut buff = [0u8; 16]; |
| 347 |
decoder.reader().read(&mut buff)?; |
| 348 |
Ok(Self::from(buff)) |
| 349 |
} |
| 350 |
} |
| 351 |
impl_borrow_decode!(Ipv6Addr); |
| 352 |
|
| 353 |
impl Encode for SocketAddr { |
| 354 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 355 |
match self { |
| 356 |
SocketAddr::V4(v4) => { |
| 357 |
0u32.encode(encoder)?; |
| 358 |
v4.encode(encoder) |
| 359 |
} |
| 360 |
SocketAddr::V6(v6) => { |
| 361 |
1u32.encode(encoder)?; |
| 362 |
v6.encode(encoder) |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
impl<Context> Decode<Context> for SocketAddr { |
| 369 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 370 |
match u32::decode(decoder)? { |
| 371 |
0 => Ok(SocketAddr::V4(SocketAddrV4::decode(decoder)?)), |
| 372 |
1 => Ok(SocketAddr::V6(SocketAddrV6::decode(decoder)?)), |
| 373 |
found => Err(DecodeError::UnexpectedVariant { |
| 374 |
allowed: &crate::error::AllowedEnumVariants::Range { min: 0, max: 1 }, |
| 375 |
found, |
| 376 |
type_name: core::any::type_name::<SocketAddr>(), |
| 377 |
}), |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
impl_borrow_decode!(SocketAddr); |
| 382 |
|
| 383 |
impl Encode for SocketAddrV4 { |
| 384 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 385 |
self.ip().encode(encoder)?; |
| 386 |
self.port().encode(encoder) |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
impl<Context> Decode<Context> for SocketAddrV4 { |
| 391 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 392 |
let ip = Ipv4Addr::decode(decoder)?; |
| 393 |
let port = u16::decode(decoder)?; |
| 394 |
Ok(Self::new(ip, port)) |
| 395 |
} |
| 396 |
} |
| 397 |
impl_borrow_decode!(SocketAddrV4); |
| 398 |
|
| 399 |
impl Encode for SocketAddrV6 { |
| 400 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 401 |
self.ip().encode(encoder)?; |
| 402 |
self.port().encode(encoder) |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
impl<Context> Decode<Context> for SocketAddrV6 { |
| 407 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 408 |
let ip = Ipv6Addr::decode(decoder)?; |
| 409 |
let port = u16::decode(decoder)?; |
| 410 |
Ok(Self::new(ip, port, 0, 0)) |
| 411 |
} |
| 412 |
} |
| 413 |
impl_borrow_decode!(SocketAddrV6); |
| 414 |
|
| 415 |
impl std::error::Error for EncodeError { |
| 416 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 417 |
match self { |
| 418 |
Self::RefCellAlreadyBorrowed { inner, .. } => Some(inner), |
| 419 |
Self::Io { inner, .. } => Some(inner), |
| 420 |
Self::InvalidSystemTime { inner, .. } => Some(inner), |
| 421 |
_ => None, |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
impl std::error::Error for DecodeError { |
| 426 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 427 |
match self { |
| 428 |
Self::Utf8 { inner } => Some(inner), |
| 429 |
_ => None, |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
impl<K, V, S> Encode for HashMap<K, V, S> |
| 435 |
where |
| 436 |
K: Encode, |
| 437 |
V: Encode, |
| 438 |
{ |
| 439 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 440 |
crate::enc::encode_slice_len(encoder, self.len())?; |
| 441 |
for (k, v) in self.iter() { |
| 442 |
Encode::encode(k, encoder)?; |
| 443 |
Encode::encode(v, encoder)?; |
| 444 |
} |
| 445 |
Ok(()) |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
impl<Context, K, V, S> Decode<Context> for HashMap<K, V, S> |
| 450 |
where |
| 451 |
K: Decode<Context> + Eq + std::hash::Hash, |
| 452 |
V: Decode<Context>, |
| 453 |
S: std::hash::BuildHasher + Default, |
| 454 |
{ |
| 455 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 456 |
let len = crate::de::decode_slice_len(decoder)?; |
| 457 |
decoder.claim_container_read::<(K, V)>(len)?; |
| 458 |
|
| 459 |
let hash_builder: S = Default::default(); |
| 460 |
let mut map = HashMap::with_capacity_and_hasher(len, hash_builder); |
| 461 |
for _ in 0..len { |
| 462 |
// See the documentation on `unclaim_bytes_read` as to why we're doing this here |
| 463 |
decoder.unclaim_bytes_read(core::mem::size_of::<(K, V)>()); |
| 464 |
|
| 465 |
let k = K::decode(decoder)?; |
| 466 |
let v = V::decode(decoder)?; |
| 467 |
map.insert(k, v); |
| 468 |
} |
| 469 |
Ok(map) |
| 470 |
} |
| 471 |
} |
| 472 |
impl<'de, K, V, S, Context> BorrowDecode<'de, Context> for HashMap<K, V, S> |
| 473 |
where |
| 474 |
K: BorrowDecode<'de, Context> + Eq + std::hash::Hash, |
| 475 |
V: BorrowDecode<'de, Context>, |
| 476 |
S: std::hash::BuildHasher + Default, |
| 477 |
{ |
| 478 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 479 |
decoder: &mut D, |
| 480 |
) -> Result<Self, DecodeError> { |
| 481 |
let len = crate::de::decode_slice_len(decoder)?; |
| 482 |
decoder.claim_container_read::<(K, V)>(len)?; |
| 483 |
|
| 484 |
let hash_builder: S = Default::default(); |
| 485 |
let mut map = HashMap::with_capacity_and_hasher(len, hash_builder); |
| 486 |
for _ in 0..len { |
| 487 |
// See the documentation on `unclaim_bytes_read` as to why we're doing this here |
| 488 |
decoder.unclaim_bytes_read(core::mem::size_of::<(K, V)>()); |
| 489 |
|
| 490 |
let k = K::borrow_decode(decoder)?; |
| 491 |
let v = V::borrow_decode(decoder)?; |
| 492 |
map.insert(k, v); |
| 493 |
} |
| 494 |
Ok(map) |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
impl<Context, T, S> Decode<Context> for HashSet<T, S> |
| 499 |
where |
| 500 |
T: Decode<Context> + Eq + Hash, |
| 501 |
S: std::hash::BuildHasher + Default, |
| 502 |
{ |
| 503 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 504 |
let len = crate::de::decode_slice_len(decoder)?; |
| 505 |
decoder.claim_container_read::<T>(len)?; |
| 506 |
|
| 507 |
let hash_builder: S = Default::default(); |
| 508 |
let mut map: HashSet<T, S> = HashSet::with_capacity_and_hasher(len, hash_builder); |
| 509 |
for _ in 0..len { |
| 510 |
// See the documentation on `unclaim_bytes_read` as to why we're doing this here |
| 511 |
decoder.unclaim_bytes_read(core::mem::size_of::<T>()); |
| 512 |
|
| 513 |
let key = T::decode(decoder)?; |
| 514 |
map.insert(key); |
| 515 |
} |
| 516 |
Ok(map) |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
impl<'de, T, S, Context> BorrowDecode<'de, Context> for HashSet<T, S> |
| 521 |
where |
| 522 |
T: BorrowDecode<'de, Context> + Eq + Hash, |
| 523 |
S: std::hash::BuildHasher + Default, |
| 524 |
{ |
| 525 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 526 |
decoder: &mut D, |
| 527 |
) -> Result<Self, DecodeError> { |
| 528 |
let len = crate::de::decode_slice_len(decoder)?; |
| 529 |
decoder.claim_container_read::<T>(len)?; |
| 530 |
|
| 531 |
let mut map = HashSet::with_capacity_and_hasher(len, S::default()); |
| 532 |
for _ in 0..len { |
| 533 |
// See the documentation on `unclaim_bytes_read` as to why we're doing this here |
| 534 |
decoder.unclaim_bytes_read(core::mem::size_of::<T>()); |
| 535 |
|
| 536 |
let key = T::borrow_decode(decoder)?; |
| 537 |
map.insert(key); |
| 538 |
} |
| 539 |
Ok(map) |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
impl<T, S> Encode for HashSet<T, S> |
| 544 |
where |
| 545 |
T: Encode, |
| 546 |
{ |
| 547 |
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> { |
| 548 |
crate::enc::encode_slice_len(encoder, self.len())?; |
| 549 |
for item in self.iter() { |
| 550 |
item.encode(encoder)?; |
| 551 |
} |
| 552 |
Ok(()) |
| 553 |
} |
| 554 |
} |