| 1 |
use super::{ |
| 2 |
read::{BorrowReader, Reader}, |
| 3 |
BorrowDecode, BorrowDecoder, Decode, Decoder, |
| 4 |
}; |
| 5 |
use crate::{ |
| 6 |
config::{Endianness, IntEncoding, InternalEndianConfig, InternalIntEncodingConfig}, |
| 7 |
error::{DecodeError, IntegerType}, |
| 8 |
impl_borrow_decode, |
| 9 |
}; |
| 10 |
use core::{ |
| 11 |
cell::{Cell, RefCell}, |
| 12 |
cmp::Reverse, |
| 13 |
num::{ |
| 14 |
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128, |
| 15 |
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, Wrapping, |
| 16 |
}, |
| 17 |
ops::{Bound, Range, RangeInclusive}, |
| 18 |
time::Duration, |
| 19 |
}; |
| 20 |
|
| 21 |
impl<Context> Decode<Context> for bool { |
| 22 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 23 |
match u8::decode(decoder)? { |
| 24 |
0 => Ok(false), |
| 25 |
1 => Ok(true), |
| 26 |
x => Err(DecodeError::InvalidBooleanValue(x)), |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
impl_borrow_decode!(bool); |
| 31 |
|
| 32 |
impl<Context> Decode<Context> for u8 { |
| 33 |
#[inline] |
| 34 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 35 |
decoder.claim_bytes_read(1)?; |
| 36 |
if let Some(buf) = decoder.reader().peek_read(1) { |
| 37 |
let byte = buf[0]; |
| 38 |
decoder.reader().consume(1); |
| 39 |
Ok(byte) |
| 40 |
} else { |
| 41 |
let mut bytes = [0u8; 1]; |
| 42 |
decoder.reader().read(&mut bytes)?; |
| 43 |
Ok(bytes[0]) |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
impl_borrow_decode!(u8); |
| 48 |
|
| 49 |
impl<Context> Decode<Context> for NonZeroU8 { |
| 50 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 51 |
NonZeroU8::new(u8::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 52 |
non_zero_type: IntegerType::U8, |
| 53 |
}) |
| 54 |
} |
| 55 |
} |
| 56 |
impl_borrow_decode!(NonZeroU8); |
| 57 |
|
| 58 |
impl<Context> Decode<Context> for u16 { |
| 59 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 60 |
decoder.claim_bytes_read(2)?; |
| 61 |
match D::C::INT_ENCODING { |
| 62 |
IntEncoding::Variable => { |
| 63 |
crate::varint::varint_decode_u16(decoder.reader(), D::C::ENDIAN) |
| 64 |
} |
| 65 |
IntEncoding::Fixed => { |
| 66 |
let mut bytes = [0u8; 2]; |
| 67 |
decoder.reader().read(&mut bytes)?; |
| 68 |
Ok(match D::C::ENDIAN { |
| 69 |
Endianness::Little => u16::from_le_bytes(bytes), |
| 70 |
Endianness::Big => u16::from_be_bytes(bytes), |
| 71 |
}) |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
impl_borrow_decode!(u16); |
| 77 |
|
| 78 |
impl<Context> Decode<Context> for NonZeroU16 { |
| 79 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 80 |
NonZeroU16::new(u16::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 81 |
non_zero_type: IntegerType::U16, |
| 82 |
}) |
| 83 |
} |
| 84 |
} |
| 85 |
impl_borrow_decode!(NonZeroU16); |
| 86 |
|
| 87 |
impl<Context> Decode<Context> for u32 { |
| 88 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 89 |
decoder.claim_bytes_read(4)?; |
| 90 |
match D::C::INT_ENCODING { |
| 91 |
IntEncoding::Variable => { |
| 92 |
crate::varint::varint_decode_u32(decoder.reader(), D::C::ENDIAN) |
| 93 |
} |
| 94 |
IntEncoding::Fixed => { |
| 95 |
let mut bytes = [0u8; 4]; |
| 96 |
decoder.reader().read(&mut bytes)?; |
| 97 |
Ok(match D::C::ENDIAN { |
| 98 |
Endianness::Little => u32::from_le_bytes(bytes), |
| 99 |
Endianness::Big => u32::from_be_bytes(bytes), |
| 100 |
}) |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
impl_borrow_decode!(u32); |
| 106 |
|
| 107 |
impl<Context> Decode<Context> for NonZeroU32 { |
| 108 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 109 |
NonZeroU32::new(u32::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 110 |
non_zero_type: IntegerType::U32, |
| 111 |
}) |
| 112 |
} |
| 113 |
} |
| 114 |
impl_borrow_decode!(NonZeroU32); |
| 115 |
|
| 116 |
impl<Context> Decode<Context> for u64 { |
| 117 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 118 |
decoder.claim_bytes_read(8)?; |
| 119 |
match D::C::INT_ENCODING { |
| 120 |
IntEncoding::Variable => { |
| 121 |
crate::varint::varint_decode_u64(decoder.reader(), D::C::ENDIAN) |
| 122 |
} |
| 123 |
IntEncoding::Fixed => { |
| 124 |
let mut bytes = [0u8; 8]; |
| 125 |
decoder.reader().read(&mut bytes)?; |
| 126 |
Ok(match D::C::ENDIAN { |
| 127 |
Endianness::Little => u64::from_le_bytes(bytes), |
| 128 |
Endianness::Big => u64::from_be_bytes(bytes), |
| 129 |
}) |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
impl_borrow_decode!(u64); |
| 135 |
|
| 136 |
impl<Context> Decode<Context> for NonZeroU64 { |
| 137 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 138 |
NonZeroU64::new(u64::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 139 |
non_zero_type: IntegerType::U64, |
| 140 |
}) |
| 141 |
} |
| 142 |
} |
| 143 |
impl_borrow_decode!(NonZeroU64); |
| 144 |
|
| 145 |
impl<Context> Decode<Context> for u128 { |
| 146 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 147 |
decoder.claim_bytes_read(16)?; |
| 148 |
match D::C::INT_ENCODING { |
| 149 |
IntEncoding::Variable => { |
| 150 |
crate::varint::varint_decode_u128(decoder.reader(), D::C::ENDIAN) |
| 151 |
} |
| 152 |
IntEncoding::Fixed => { |
| 153 |
let mut bytes = [0u8; 16]; |
| 154 |
decoder.reader().read(&mut bytes)?; |
| 155 |
Ok(match D::C::ENDIAN { |
| 156 |
Endianness::Little => u128::from_le_bytes(bytes), |
| 157 |
Endianness::Big => u128::from_be_bytes(bytes), |
| 158 |
}) |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
impl_borrow_decode!(u128); |
| 164 |
|
| 165 |
impl<Context> Decode<Context> for NonZeroU128 { |
| 166 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 167 |
NonZeroU128::new(u128::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 168 |
non_zero_type: IntegerType::U128, |
| 169 |
}) |
| 170 |
} |
| 171 |
} |
| 172 |
impl_borrow_decode!(NonZeroU128); |
| 173 |
|
| 174 |
impl<Context> Decode<Context> for usize { |
| 175 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 176 |
decoder.claim_bytes_read(8)?; |
| 177 |
match D::C::INT_ENCODING { |
| 178 |
IntEncoding::Variable => { |
| 179 |
crate::varint::varint_decode_usize(decoder.reader(), D::C::ENDIAN) |
| 180 |
} |
| 181 |
IntEncoding::Fixed => { |
| 182 |
let mut bytes = [0u8; 8]; |
| 183 |
decoder.reader().read(&mut bytes)?; |
| 184 |
|
| 185 |
let value = match D::C::ENDIAN { |
| 186 |
Endianness::Little => u64::from_le_bytes(bytes), |
| 187 |
Endianness::Big => u64::from_be_bytes(bytes), |
| 188 |
}; |
| 189 |
|
| 190 |
value |
| 191 |
.try_into() |
| 192 |
.map_err(|_| DecodeError::OutsideUsizeRange(value)) |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
impl_borrow_decode!(usize); |
| 198 |
|
| 199 |
impl<Context> Decode<Context> for NonZeroUsize { |
| 200 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 201 |
NonZeroUsize::new(usize::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 202 |
non_zero_type: IntegerType::Usize, |
| 203 |
}) |
| 204 |
} |
| 205 |
} |
| 206 |
impl_borrow_decode!(NonZeroUsize); |
| 207 |
|
| 208 |
impl<Context> Decode<Context> for i8 { |
| 209 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 210 |
decoder.claim_bytes_read(1)?; |
| 211 |
let mut bytes = [0u8; 1]; |
| 212 |
decoder.reader().read(&mut bytes)?; |
| 213 |
Ok(bytes[0] as i8) |
| 214 |
} |
| 215 |
} |
| 216 |
impl_borrow_decode!(i8); |
| 217 |
|
| 218 |
impl<Context> Decode<Context> for NonZeroI8 { |
| 219 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 220 |
NonZeroI8::new(i8::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 221 |
non_zero_type: IntegerType::I8, |
| 222 |
}) |
| 223 |
} |
| 224 |
} |
| 225 |
impl_borrow_decode!(NonZeroI8); |
| 226 |
|
| 227 |
impl<Context> Decode<Context> for i16 { |
| 228 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 229 |
decoder.claim_bytes_read(2)?; |
| 230 |
match D::C::INT_ENCODING { |
| 231 |
IntEncoding::Variable => { |
| 232 |
crate::varint::varint_decode_i16(decoder.reader(), D::C::ENDIAN) |
| 233 |
} |
| 234 |
IntEncoding::Fixed => { |
| 235 |
let mut bytes = [0u8; 2]; |
| 236 |
decoder.reader().read(&mut bytes)?; |
| 237 |
Ok(match D::C::ENDIAN { |
| 238 |
Endianness::Little => i16::from_le_bytes(bytes), |
| 239 |
Endianness::Big => i16::from_be_bytes(bytes), |
| 240 |
}) |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
impl_borrow_decode!(i16); |
| 246 |
|
| 247 |
impl<Context> Decode<Context> for NonZeroI16 { |
| 248 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 249 |
NonZeroI16::new(i16::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 250 |
non_zero_type: IntegerType::I16, |
| 251 |
}) |
| 252 |
} |
| 253 |
} |
| 254 |
impl_borrow_decode!(NonZeroI16); |
| 255 |
|
| 256 |
impl<Context> Decode<Context> for i32 { |
| 257 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 258 |
decoder.claim_bytes_read(4)?; |
| 259 |
match D::C::INT_ENCODING { |
| 260 |
IntEncoding::Variable => { |
| 261 |
crate::varint::varint_decode_i32(decoder.reader(), D::C::ENDIAN) |
| 262 |
} |
| 263 |
IntEncoding::Fixed => { |
| 264 |
let mut bytes = [0u8; 4]; |
| 265 |
decoder.reader().read(&mut bytes)?; |
| 266 |
Ok(match D::C::ENDIAN { |
| 267 |
Endianness::Little => i32::from_le_bytes(bytes), |
| 268 |
Endianness::Big => i32::from_be_bytes(bytes), |
| 269 |
}) |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
impl_borrow_decode!(i32); |
| 275 |
|
| 276 |
impl<Context> Decode<Context> for NonZeroI32 { |
| 277 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 278 |
NonZeroI32::new(i32::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 279 |
non_zero_type: IntegerType::I32, |
| 280 |
}) |
| 281 |
} |
| 282 |
} |
| 283 |
impl_borrow_decode!(NonZeroI32); |
| 284 |
|
| 285 |
impl<Context> Decode<Context> for i64 { |
| 286 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 287 |
decoder.claim_bytes_read(8)?; |
| 288 |
match D::C::INT_ENCODING { |
| 289 |
IntEncoding::Variable => { |
| 290 |
crate::varint::varint_decode_i64(decoder.reader(), D::C::ENDIAN) |
| 291 |
} |
| 292 |
IntEncoding::Fixed => { |
| 293 |
let mut bytes = [0u8; 8]; |
| 294 |
decoder.reader().read(&mut bytes)?; |
| 295 |
Ok(match D::C::ENDIAN { |
| 296 |
Endianness::Little => i64::from_le_bytes(bytes), |
| 297 |
Endianness::Big => i64::from_be_bytes(bytes), |
| 298 |
}) |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
impl_borrow_decode!(i64); |
| 304 |
|
| 305 |
impl<Context> Decode<Context> for NonZeroI64 { |
| 306 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 307 |
NonZeroI64::new(i64::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 308 |
non_zero_type: IntegerType::I64, |
| 309 |
}) |
| 310 |
} |
| 311 |
} |
| 312 |
impl_borrow_decode!(NonZeroI64); |
| 313 |
|
| 314 |
impl<Context> Decode<Context> for i128 { |
| 315 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 316 |
decoder.claim_bytes_read(16)?; |
| 317 |
match D::C::INT_ENCODING { |
| 318 |
IntEncoding::Variable => { |
| 319 |
crate::varint::varint_decode_i128(decoder.reader(), D::C::ENDIAN) |
| 320 |
} |
| 321 |
IntEncoding::Fixed => { |
| 322 |
let mut bytes = [0u8; 16]; |
| 323 |
decoder.reader().read(&mut bytes)?; |
| 324 |
Ok(match D::C::ENDIAN { |
| 325 |
Endianness::Little => i128::from_le_bytes(bytes), |
| 326 |
Endianness::Big => i128::from_be_bytes(bytes), |
| 327 |
}) |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
impl_borrow_decode!(i128); |
| 333 |
|
| 334 |
impl<Context> Decode<Context> for NonZeroI128 { |
| 335 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 336 |
NonZeroI128::new(i128::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 337 |
non_zero_type: IntegerType::I128, |
| 338 |
}) |
| 339 |
} |
| 340 |
} |
| 341 |
impl_borrow_decode!(NonZeroI128); |
| 342 |
|
| 343 |
impl<Context> Decode<Context> for isize { |
| 344 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 345 |
decoder.claim_bytes_read(8)?; |
| 346 |
match D::C::INT_ENCODING { |
| 347 |
IntEncoding::Variable => { |
| 348 |
crate::varint::varint_decode_isize(decoder.reader(), D::C::ENDIAN) |
| 349 |
} |
| 350 |
IntEncoding::Fixed => { |
| 351 |
let mut bytes = [0u8; 8]; |
| 352 |
decoder.reader().read(&mut bytes)?; |
| 353 |
Ok(match D::C::ENDIAN { |
| 354 |
Endianness::Little => i64::from_le_bytes(bytes), |
| 355 |
Endianness::Big => i64::from_be_bytes(bytes), |
| 356 |
} as isize) |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
impl_borrow_decode!(isize); |
| 362 |
|
| 363 |
impl<Context> Decode<Context> for NonZeroIsize { |
| 364 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 365 |
NonZeroIsize::new(isize::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
| 366 |
non_zero_type: IntegerType::Isize, |
| 367 |
}) |
| 368 |
} |
| 369 |
} |
| 370 |
impl_borrow_decode!(NonZeroIsize); |
| 371 |
|
| 372 |
impl<Context> Decode<Context> for f32 { |
| 373 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 374 |
decoder.claim_bytes_read(4)?; |
| 375 |
let mut bytes = [0u8; 4]; |
| 376 |
decoder.reader().read(&mut bytes)?; |
| 377 |
Ok(match D::C::ENDIAN { |
| 378 |
Endianness::Little => f32::from_le_bytes(bytes), |
| 379 |
Endianness::Big => f32::from_be_bytes(bytes), |
| 380 |
}) |
| 381 |
} |
| 382 |
} |
| 383 |
impl_borrow_decode!(f32); |
| 384 |
|
| 385 |
impl<Context> Decode<Context> for f64 { |
| 386 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 387 |
decoder.claim_bytes_read(8)?; |
| 388 |
let mut bytes = [0u8; 8]; |
| 389 |
decoder.reader().read(&mut bytes)?; |
| 390 |
Ok(match D::C::ENDIAN { |
| 391 |
Endianness::Little => f64::from_le_bytes(bytes), |
| 392 |
Endianness::Big => f64::from_be_bytes(bytes), |
| 393 |
}) |
| 394 |
} |
| 395 |
} |
| 396 |
impl_borrow_decode!(f64); |
| 397 |
|
| 398 |
impl<Context, T: Decode<Context>> Decode<Context> for Wrapping<T> { |
| 399 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 400 |
Ok(Wrapping(T::decode(decoder)?)) |
| 401 |
} |
| 402 |
} |
| 403 |
impl<'de, Context, T: BorrowDecode<'de, Context>> BorrowDecode<'de, Context> for Wrapping<T> { |
| 404 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 405 |
decoder: &mut D, |
| 406 |
) -> Result<Self, DecodeError> { |
| 407 |
Ok(Wrapping(T::borrow_decode(decoder)?)) |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
impl<Context, T: Decode<Context>> Decode<Context> for Reverse<T> { |
| 412 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 413 |
Ok(Reverse(T::decode(decoder)?)) |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
impl<'de, Context, T: BorrowDecode<'de, Context>> BorrowDecode<'de, Context> for Reverse<T> { |
| 418 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 419 |
decoder: &mut D, |
| 420 |
) -> Result<Self, DecodeError> { |
| 421 |
Ok(Reverse(T::borrow_decode(decoder)?)) |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
impl<Context> Decode<Context> for char { |
| 426 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 427 |
let mut array = [0u8; 4]; |
| 428 |
|
| 429 |
// Look at the first byte to see how many bytes must be read |
| 430 |
decoder.reader().read(&mut array[..1])?; |
| 431 |
|
| 432 |
let width = utf8_char_width(array[0]); |
| 433 |
if width == 0 { |
| 434 |
return Err(DecodeError::InvalidCharEncoding(array)); |
| 435 |
} |
| 436 |
// Normally we have to `.claim_bytes_read` before reading, however in this |
| 437 |
// case the amount of bytes read from `char` can vary wildly, and it should |
| 438 |
// only read up to 4 bytes too much. |
| 439 |
decoder.claim_bytes_read(width)?; |
| 440 |
if width == 1 { |
| 441 |
return Ok(array[0] as char); |
| 442 |
} |
| 443 |
|
| 444 |
// read the remaining pain |
| 445 |
decoder.reader().read(&mut array[1..width])?; |
| 446 |
let res = core::str::from_utf8(&array[..width]) |
| 447 |
.ok() |
| 448 |
.and_then(|s| s.chars().next()) |
| 449 |
.ok_or(DecodeError::InvalidCharEncoding(array))?; |
| 450 |
Ok(res) |
| 451 |
} |
| 452 |
} |
| 453 |
impl_borrow_decode!(char); |
| 454 |
|
| 455 |
impl<'a, 'de: 'a, Context> BorrowDecode<'de, Context> for &'a [u8] { |
| 456 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 457 |
decoder: &mut D, |
| 458 |
) -> Result<Self, DecodeError> { |
| 459 |
let len = super::decode_slice_len(decoder)?; |
| 460 |
decoder.claim_bytes_read(len)?; |
| 461 |
decoder.borrow_reader().take_bytes(len) |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
impl<'a, 'de: 'a, Context> BorrowDecode<'de, Context> for &'a str { |
| 466 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 467 |
decoder: &mut D, |
| 468 |
) -> Result<Self, DecodeError> { |
| 469 |
let slice = <&[u8]>::borrow_decode(decoder)?; |
| 470 |
core::str::from_utf8(slice).map_err(|inner| DecodeError::Utf8 { inner }) |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
impl<Context, T, const N: usize> Decode<Context> for [T; N] |
| 475 |
where |
| 476 |
T: Decode<Context>, |
| 477 |
{ |
| 478 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 479 |
decoder.claim_bytes_read(core::mem::size_of::<[T; N]>())?; |
| 480 |
|
| 481 |
if unty::type_equal::<T, u8>() { |
| 482 |
let mut buf = [0u8; N]; |
| 483 |
decoder.reader().read(&mut buf)?; |
| 484 |
let ptr = &mut buf as *mut _ as *mut [T; N]; |
| 485 |
|
| 486 |
// Safety: we know that T is a u8, so it is perfectly safe to |
| 487 |
// translate an array of u8 into an array of T |
| 488 |
let res = unsafe { ptr.read() }; |
| 489 |
Ok(res) |
| 490 |
} else { |
| 491 |
let result = super::impl_core::collect_into_array(&mut (0..N).map(|_| { |
| 492 |
// See the documentation on `unclaim_bytes_read` as to why we're doing this here |
| 493 |
decoder.unclaim_bytes_read(core::mem::size_of::<T>()); |
| 494 |
T::decode(decoder) |
| 495 |
})); |
| 496 |
|
| 497 |
// result is only None if N does not match the values of `(0..N)`, which it always should |
| 498 |
// So this unwrap should never occur |
| 499 |
result.unwrap() |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
impl<'de, T, const N: usize, Context> BorrowDecode<'de, Context> for [T; N] |
| 505 |
where |
| 506 |
T: BorrowDecode<'de, Context>, |
| 507 |
{ |
| 508 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 509 |
decoder: &mut D, |
| 510 |
) -> Result<Self, DecodeError> { |
| 511 |
decoder.claim_bytes_read(core::mem::size_of::<[T; N]>())?; |
| 512 |
|
| 513 |
if unty::type_equal::<T, u8>() { |
| 514 |
let mut buf = [0u8; N]; |
| 515 |
decoder.reader().read(&mut buf)?; |
| 516 |
let ptr = &mut buf as *mut _ as *mut [T; N]; |
| 517 |
|
| 518 |
// Safety: we know that T is a u8, so it is perfectly safe to |
| 519 |
// translate an array of u8 into an array of T |
| 520 |
let res = unsafe { ptr.read() }; |
| 521 |
Ok(res) |
| 522 |
} else { |
| 523 |
let result = super::impl_core::collect_into_array(&mut (0..N).map(|_| { |
| 524 |
// See the documentation on `unclaim_bytes_read` as to why we're doing this here |
| 525 |
decoder.unclaim_bytes_read(core::mem::size_of::<T>()); |
| 526 |
T::borrow_decode(decoder) |
| 527 |
})); |
| 528 |
|
| 529 |
// result is only None if N does not match the values of `(0..N)`, which it always should |
| 530 |
// So this unwrap should never occur |
| 531 |
result.unwrap() |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
impl<Context> Decode<Context> for () { |
| 537 |
fn decode<D: Decoder<Context = Context>>(_: &mut D) -> Result<Self, DecodeError> { |
| 538 |
Ok(()) |
| 539 |
} |
| 540 |
} |
| 541 |
impl_borrow_decode!(()); |
| 542 |
|
| 543 |
impl<Context, T> Decode<Context> for core::marker::PhantomData<T> { |
| 544 |
fn decode<D: Decoder<Context = Context>>(_: &mut D) -> Result<Self, DecodeError> { |
| 545 |
Ok(core::marker::PhantomData) |
| 546 |
} |
| 547 |
} |
| 548 |
impl_borrow_decode!(core::marker::PhantomData<T>, T); |
| 549 |
|
| 550 |
impl<Context, T> Decode<Context> for Option<T> |
| 551 |
where |
| 552 |
T: Decode<Context>, |
| 553 |
{ |
| 554 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 555 |
match super::decode_option_variant(decoder, core::any::type_name::<Option<T>>())? { |
| 556 |
Some(_) => { |
| 557 |
let val = T::decode(decoder)?; |
| 558 |
Ok(Some(val)) |
| 559 |
} |
| 560 |
None => Ok(None), |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
impl<'de, T, Context> BorrowDecode<'de, Context> for Option<T> |
| 566 |
where |
| 567 |
T: BorrowDecode<'de, Context>, |
| 568 |
{ |
| 569 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 570 |
decoder: &mut D, |
| 571 |
) -> Result<Self, DecodeError> { |
| 572 |
match super::decode_option_variant(decoder, core::any::type_name::<Option<T>>())? { |
| 573 |
Some(_) => { |
| 574 |
let val = T::borrow_decode(decoder)?; |
| 575 |
Ok(Some(val)) |
| 576 |
} |
| 577 |
None => Ok(None), |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
impl<Context, T, U> Decode<Context> for Result<T, U> |
| 583 |
where |
| 584 |
T: Decode<Context>, |
| 585 |
U: Decode<Context>, |
| 586 |
{ |
| 587 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 588 |
let is_ok = u32::decode(decoder)?; |
| 589 |
match is_ok { |
| 590 |
0 => { |
| 591 |
let t = T::decode(decoder)?; |
| 592 |
Ok(Ok(t)) |
| 593 |
} |
| 594 |
1 => { |
| 595 |
let u = U::decode(decoder)?; |
| 596 |
Ok(Err(u)) |
| 597 |
} |
| 598 |
x => Err(DecodeError::UnexpectedVariant { |
| 599 |
found: x, |
| 600 |
allowed: &crate::error::AllowedEnumVariants::Range { max: 1, min: 0 }, |
| 601 |
type_name: core::any::type_name::<Result<T, U>>(), |
| 602 |
}), |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
impl<'de, T, U, Context> BorrowDecode<'de, Context> for Result<T, U> |
| 608 |
where |
| 609 |
T: BorrowDecode<'de, Context>, |
| 610 |
U: BorrowDecode<'de, Context>, |
| 611 |
{ |
| 612 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 613 |
decoder: &mut D, |
| 614 |
) -> Result<Self, DecodeError> { |
| 615 |
let is_ok = u32::decode(decoder)?; |
| 616 |
match is_ok { |
| 617 |
0 => { |
| 618 |
let t = T::borrow_decode(decoder)?; |
| 619 |
Ok(Ok(t)) |
| 620 |
} |
| 621 |
1 => { |
| 622 |
let u = U::borrow_decode(decoder)?; |
| 623 |
Ok(Err(u)) |
| 624 |
} |
| 625 |
x => Err(DecodeError::UnexpectedVariant { |
| 626 |
found: x, |
| 627 |
allowed: &crate::error::AllowedEnumVariants::Range { max: 1, min: 0 }, |
| 628 |
type_name: core::any::type_name::<Result<T, U>>(), |
| 629 |
}), |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
impl<Context, T> Decode<Context> for Cell<T> |
| 635 |
where |
| 636 |
T: Decode<Context>, |
| 637 |
{ |
| 638 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 639 |
let t = T::decode(decoder)?; |
| 640 |
Ok(Cell::new(t)) |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
impl<'de, T, Context> BorrowDecode<'de, Context> for Cell<T> |
| 645 |
where |
| 646 |
T: BorrowDecode<'de, Context>, |
| 647 |
{ |
| 648 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 649 |
decoder: &mut D, |
| 650 |
) -> Result<Self, DecodeError> { |
| 651 |
let t = T::borrow_decode(decoder)?; |
| 652 |
Ok(Cell::new(t)) |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
impl<Context, T> Decode<Context> for RefCell<T> |
| 657 |
where |
| 658 |
T: Decode<Context>, |
| 659 |
{ |
| 660 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 661 |
let t = T::decode(decoder)?; |
| 662 |
Ok(RefCell::new(t)) |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
impl<'de, T, Context> BorrowDecode<'de, Context> for RefCell<T> |
| 667 |
where |
| 668 |
T: BorrowDecode<'de, Context>, |
| 669 |
{ |
| 670 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 671 |
decoder: &mut D, |
| 672 |
) -> Result<Self, DecodeError> { |
| 673 |
let t = T::borrow_decode(decoder)?; |
| 674 |
Ok(RefCell::new(t)) |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
impl<Context> Decode<Context> for Duration { |
| 679 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 680 |
const NANOS_PER_SEC: u64 = 1_000_000_000; |
| 681 |
let secs: u64 = Decode::decode(decoder)?; |
| 682 |
let nanos: u32 = Decode::decode(decoder)?; |
| 683 |
if secs.checked_add(u64::from(nanos) / NANOS_PER_SEC).is_none() { |
| 684 |
return Err(DecodeError::InvalidDuration { secs, nanos }); |
| 685 |
} |
| 686 |
Ok(Duration::new(secs, nanos)) |
| 687 |
} |
| 688 |
} |
| 689 |
impl_borrow_decode!(Duration); |
| 690 |
|
| 691 |
impl<Context, T> Decode<Context> for Range<T> |
| 692 |
where |
| 693 |
T: Decode<Context>, |
| 694 |
{ |
| 695 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 696 |
let min = T::decode(decoder)?; |
| 697 |
let max = T::decode(decoder)?; |
| 698 |
Ok(min..max) |
| 699 |
} |
| 700 |
} |
| 701 |
impl<'de, T, Context> BorrowDecode<'de, Context> for Range<T> |
| 702 |
where |
| 703 |
T: BorrowDecode<'de, Context>, |
| 704 |
{ |
| 705 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 706 |
decoder: &mut D, |
| 707 |
) -> Result<Self, DecodeError> { |
| 708 |
let min = T::borrow_decode(decoder)?; |
| 709 |
let max = T::borrow_decode(decoder)?; |
| 710 |
Ok(min..max) |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
impl<Context, T> Decode<Context> for RangeInclusive<T> |
| 715 |
where |
| 716 |
T: Decode<Context>, |
| 717 |
{ |
| 718 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 719 |
let min = T::decode(decoder)?; |
| 720 |
let max = T::decode(decoder)?; |
| 721 |
Ok(RangeInclusive::new(min, max)) |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
impl<'de, T, Context> BorrowDecode<'de, Context> for RangeInclusive<T> |
| 726 |
where |
| 727 |
T: BorrowDecode<'de, Context>, |
| 728 |
{ |
| 729 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 730 |
decoder: &mut D, |
| 731 |
) -> Result<Self, DecodeError> { |
| 732 |
let min = T::borrow_decode(decoder)?; |
| 733 |
let max = T::borrow_decode(decoder)?; |
| 734 |
Ok(RangeInclusive::new(min, max)) |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
impl<T, Context> Decode<Context> for Bound<T> |
| 739 |
where |
| 740 |
T: Decode<Context>, |
| 741 |
{ |
| 742 |
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
| 743 |
match u32::decode(decoder)? { |
| 744 |
0 => Ok(Bound::Unbounded), |
| 745 |
1 => Ok(Bound::Included(T::decode(decoder)?)), |
| 746 |
2 => Ok(Bound::Excluded(T::decode(decoder)?)), |
| 747 |
x => Err(DecodeError::UnexpectedVariant { |
| 748 |
allowed: &crate::error::AllowedEnumVariants::Range { max: 2, min: 0 }, |
| 749 |
found: x, |
| 750 |
type_name: core::any::type_name::<Bound<T>>(), |
| 751 |
}), |
| 752 |
} |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
impl<'de, T, Context> BorrowDecode<'de, Context> for Bound<T> |
| 757 |
where |
| 758 |
T: BorrowDecode<'de, Context>, |
| 759 |
{ |
| 760 |
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
| 761 |
decoder: &mut D, |
| 762 |
) -> Result<Self, DecodeError> { |
| 763 |
match u32::decode(decoder)? { |
| 764 |
0 => Ok(Bound::Unbounded), |
| 765 |
1 => Ok(Bound::Included(T::borrow_decode(decoder)?)), |
| 766 |
2 => Ok(Bound::Excluded(T::borrow_decode(decoder)?)), |
| 767 |
x => Err(DecodeError::UnexpectedVariant { |
| 768 |
allowed: &crate::error::AllowedEnumVariants::Range { max: 2, min: 0 }, |
| 769 |
found: x, |
| 770 |
type_name: core::any::type_name::<Bound<T>>(), |
| 771 |
}), |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
const UTF8_CHAR_WIDTH: [u8; 256] = [ |
| 777 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 778 |
1, // 0x1F |
| 779 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 780 |
1, // 0x3F |
| 781 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 782 |
1, // 0x5F |
| 783 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 784 |
1, // 0x7F |
| 785 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 786 |
0, // 0x9F |
| 787 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 788 |
0, // 0xBF |
| 789 |
0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 790 |
2, // 0xDF |
| 791 |
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xEF |
| 792 |
4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xFF |
| 793 |
]; |
| 794 |
|
| 795 |
// This function is a copy of core::str::utf8_char_width |
| 796 |
const fn utf8_char_width(b: u8) -> usize { |
| 797 |
UTF8_CHAR_WIDTH[b as usize] as usize |
| 798 |
} |