| 1 |
use core::convert::TryInto; |
| 2 |
|
| 3 |
use super::{SINGLE_BYTE_MAX, U128_BYTE, U16_BYTE, U32_BYTE, U64_BYTE}; |
| 4 |
use crate::{ |
| 5 |
config::Endianness, |
| 6 |
de::read::Reader, |
| 7 |
error::{DecodeError, IntegerType}, |
| 8 |
}; |
| 9 |
|
| 10 |
#[inline(never)] |
| 11 |
#[cold] |
| 12 |
fn deserialize_varint_cold_u16<R>(read: &mut R, endian: Endianness) -> Result<u16, DecodeError> |
| 13 |
where |
| 14 |
R: Reader, |
| 15 |
{ |
| 16 |
let mut bytes = [0u8; 1]; |
| 17 |
read.read(&mut bytes)?; |
| 18 |
match bytes[0] { |
| 19 |
byte @ 0..=SINGLE_BYTE_MAX => Ok(byte as u16), |
| 20 |
U16_BYTE => { |
| 21 |
let mut bytes = [0u8; 2]; |
| 22 |
read.read(&mut bytes)?; |
| 23 |
Ok(match endian { |
| 24 |
Endianness::Big => u16::from_be_bytes(bytes), |
| 25 |
Endianness::Little => u16::from_le_bytes(bytes), |
| 26 |
}) |
| 27 |
} |
| 28 |
U32_BYTE => invalid_varint_discriminant(IntegerType::U16, IntegerType::U32), |
| 29 |
U64_BYTE => invalid_varint_discriminant(IntegerType::U16, IntegerType::U64), |
| 30 |
U128_BYTE => invalid_varint_discriminant(IntegerType::U16, IntegerType::U128), |
| 31 |
_ => invalid_varint_discriminant(IntegerType::U16, IntegerType::Reserved), |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
#[inline(never)] |
| 36 |
#[cold] |
| 37 |
fn deserialize_varint_cold_u32<R>(read: &mut R, endian: Endianness) -> Result<u32, DecodeError> |
| 38 |
where |
| 39 |
R: Reader, |
| 40 |
{ |
| 41 |
let mut bytes = [0u8; 1]; |
| 42 |
read.read(&mut bytes)?; |
| 43 |
match bytes[0] { |
| 44 |
byte @ 0..=SINGLE_BYTE_MAX => Ok(byte as u32), |
| 45 |
U16_BYTE => { |
| 46 |
let mut bytes = [0u8; 2]; |
| 47 |
read.read(&mut bytes)?; |
| 48 |
Ok(match endian { |
| 49 |
Endianness::Big => u16::from_be_bytes(bytes) as u32, |
| 50 |
Endianness::Little => u16::from_le_bytes(bytes) as u32, |
| 51 |
}) |
| 52 |
} |
| 53 |
U32_BYTE => { |
| 54 |
let mut bytes = [0u8; 4]; |
| 55 |
read.read(&mut bytes)?; |
| 56 |
Ok(match endian { |
| 57 |
Endianness::Big => u32::from_be_bytes(bytes), |
| 58 |
Endianness::Little => u32::from_le_bytes(bytes), |
| 59 |
}) |
| 60 |
} |
| 61 |
U64_BYTE => invalid_varint_discriminant(IntegerType::U32, IntegerType::U64), |
| 62 |
U128_BYTE => invalid_varint_discriminant(IntegerType::U32, IntegerType::U128), |
| 63 |
_ => invalid_varint_discriminant(IntegerType::U32, IntegerType::Reserved), |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
#[inline(never)] |
| 68 |
#[cold] |
| 69 |
fn deserialize_varint_cold_u64<R>(read: &mut R, endian: Endianness) -> Result<u64, DecodeError> |
| 70 |
where |
| 71 |
R: Reader, |
| 72 |
{ |
| 73 |
let mut bytes = [0u8; 1]; |
| 74 |
read.read(&mut bytes)?; |
| 75 |
match bytes[0] { |
| 76 |
byte @ 0..=SINGLE_BYTE_MAX => Ok(byte as u64), |
| 77 |
U16_BYTE => { |
| 78 |
let mut bytes = [0u8; 2]; |
| 79 |
read.read(&mut bytes)?; |
| 80 |
Ok(match endian { |
| 81 |
Endianness::Big => u16::from_be_bytes(bytes) as u64, |
| 82 |
Endianness::Little => u16::from_le_bytes(bytes) as u64, |
| 83 |
}) |
| 84 |
} |
| 85 |
U32_BYTE => { |
| 86 |
let mut bytes = [0u8; 4]; |
| 87 |
read.read(&mut bytes)?; |
| 88 |
Ok(match endian { |
| 89 |
Endianness::Big => u32::from_be_bytes(bytes) as u64, |
| 90 |
Endianness::Little => u32::from_le_bytes(bytes) as u64, |
| 91 |
}) |
| 92 |
} |
| 93 |
U64_BYTE => { |
| 94 |
let mut bytes = [0u8; 8]; |
| 95 |
read.read(&mut bytes)?; |
| 96 |
Ok(match endian { |
| 97 |
Endianness::Big => u64::from_be_bytes(bytes), |
| 98 |
Endianness::Little => u64::from_le_bytes(bytes), |
| 99 |
}) |
| 100 |
} |
| 101 |
U128_BYTE => invalid_varint_discriminant(IntegerType::U64, IntegerType::U128), |
| 102 |
_ => invalid_varint_discriminant(IntegerType::U64, IntegerType::Reserved), |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
#[inline(never)] |
| 107 |
#[cold] |
| 108 |
fn deserialize_varint_cold_usize<R>(read: &mut R, endian: Endianness) -> Result<usize, DecodeError> |
| 109 |
where |
| 110 |
R: Reader, |
| 111 |
{ |
| 112 |
let mut bytes = [0u8; 1]; |
| 113 |
read.read(&mut bytes)?; |
| 114 |
match bytes[0] { |
| 115 |
byte @ 0..=SINGLE_BYTE_MAX => Ok(byte as usize), |
| 116 |
U16_BYTE => { |
| 117 |
let mut bytes = [0u8; 2]; |
| 118 |
read.read(&mut bytes)?; |
| 119 |
Ok(match endian { |
| 120 |
Endianness::Big => u16::from_be_bytes(bytes) as usize, |
| 121 |
Endianness::Little => u16::from_le_bytes(bytes) as usize, |
| 122 |
}) |
| 123 |
} |
| 124 |
U32_BYTE => { |
| 125 |
let mut bytes = [0u8; 4]; |
| 126 |
read.read(&mut bytes)?; |
| 127 |
Ok(match endian { |
| 128 |
Endianness::Big => u32::from_be_bytes(bytes) as usize, |
| 129 |
Endianness::Little => u32::from_le_bytes(bytes) as usize, |
| 130 |
}) |
| 131 |
} |
| 132 |
U64_BYTE => { |
| 133 |
let mut bytes = [0u8; 8]; |
| 134 |
read.read(&mut bytes)?; |
| 135 |
Ok(match endian { |
| 136 |
Endianness::Big => u64::from_be_bytes(bytes) as usize, |
| 137 |
Endianness::Little => u64::from_le_bytes(bytes) as usize, |
| 138 |
}) |
| 139 |
} |
| 140 |
U128_BYTE => invalid_varint_discriminant(IntegerType::Usize, IntegerType::U128), |
| 141 |
_ => invalid_varint_discriminant(IntegerType::Usize, IntegerType::Reserved), |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
#[inline(never)] |
| 146 |
#[cold] |
| 147 |
fn deserialize_varint_cold_u128<R>(read: &mut R, endian: Endianness) -> Result<u128, DecodeError> |
| 148 |
where |
| 149 |
R: Reader, |
| 150 |
{ |
| 151 |
let mut bytes = [0u8; 1]; |
| 152 |
read.read(&mut bytes)?; |
| 153 |
match bytes[0] { |
| 154 |
byte @ 0..=SINGLE_BYTE_MAX => Ok(byte as u128), |
| 155 |
U16_BYTE => { |
| 156 |
let mut bytes = [0u8; 2]; |
| 157 |
read.read(&mut bytes)?; |
| 158 |
Ok(match endian { |
| 159 |
Endianness::Big => u16::from_be_bytes(bytes) as u128, |
| 160 |
Endianness::Little => u16::from_le_bytes(bytes) as u128, |
| 161 |
}) |
| 162 |
} |
| 163 |
U32_BYTE => { |
| 164 |
let mut bytes = [0u8; 4]; |
| 165 |
read.read(&mut bytes)?; |
| 166 |
Ok(match endian { |
| 167 |
Endianness::Big => u32::from_be_bytes(bytes) as u128, |
| 168 |
Endianness::Little => u32::from_le_bytes(bytes) as u128, |
| 169 |
}) |
| 170 |
} |
| 171 |
U64_BYTE => { |
| 172 |
let mut bytes = [0u8; 8]; |
| 173 |
read.read(&mut bytes)?; |
| 174 |
Ok(match endian { |
| 175 |
Endianness::Big => u64::from_be_bytes(bytes) as u128, |
| 176 |
Endianness::Little => u64::from_le_bytes(bytes) as u128, |
| 177 |
}) |
| 178 |
} |
| 179 |
U128_BYTE => { |
| 180 |
let mut bytes = [0u8; 16]; |
| 181 |
read.read(&mut bytes)?; |
| 182 |
Ok(match endian { |
| 183 |
Endianness::Big => u128::from_be_bytes(bytes), |
| 184 |
Endianness::Little => u128::from_le_bytes(bytes), |
| 185 |
}) |
| 186 |
} |
| 187 |
_ => invalid_varint_discriminant(IntegerType::U128, IntegerType::Reserved), |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
#[inline(never)] |
| 192 |
#[cold] |
| 193 |
const fn invalid_varint_discriminant<T>( |
| 194 |
expected: IntegerType, |
| 195 |
found: IntegerType, |
| 196 |
) -> Result<T, DecodeError> { |
| 197 |
Err(DecodeError::InvalidIntegerType { expected, found }) |
| 198 |
} |
| 199 |
|
| 200 |
pub fn varint_decode_u16<R: Reader>(read: &mut R, endian: Endianness) -> Result<u16, DecodeError> { |
| 201 |
if let Some(bytes) = read.peek_read(3) { |
| 202 |
let (discriminant, bytes) = bytes.split_at(1); |
| 203 |
let (out, used) = match discriminant[0] { |
| 204 |
byte @ 0..=SINGLE_BYTE_MAX => (byte as u16, 1), |
| 205 |
U16_BYTE => { |
| 206 |
let val = match endian { |
| 207 |
Endianness::Big => u16::from_be_bytes(bytes[..2].try_into().unwrap()), |
| 208 |
Endianness::Little => u16::from_le_bytes(bytes[..2].try_into().unwrap()), |
| 209 |
}; |
| 210 |
|
| 211 |
(val, 3) |
| 212 |
} |
| 213 |
U32_BYTE => return invalid_varint_discriminant(IntegerType::U16, IntegerType::U32), |
| 214 |
U64_BYTE => return invalid_varint_discriminant(IntegerType::U16, IntegerType::U64), |
| 215 |
U128_BYTE => return invalid_varint_discriminant(IntegerType::U16, IntegerType::U128), |
| 216 |
_ => return invalid_varint_discriminant(IntegerType::U16, IntegerType::Reserved), |
| 217 |
}; |
| 218 |
|
| 219 |
read.consume(used); |
| 220 |
Ok(out) |
| 221 |
} else { |
| 222 |
deserialize_varint_cold_u16(read, endian) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
pub fn varint_decode_u32<R: Reader>(read: &mut R, endian: Endianness) -> Result<u32, DecodeError> { |
| 227 |
if let Some(bytes) = read.peek_read(5) { |
| 228 |
let (discriminant, bytes) = bytes.split_at(1); |
| 229 |
let (out, used) = match discriminant[0] { |
| 230 |
byte @ 0..=SINGLE_BYTE_MAX => (byte as u32, 1), |
| 231 |
U16_BYTE => { |
| 232 |
let val = match endian { |
| 233 |
Endianness::Big => u16::from_be_bytes(bytes[..2].try_into().unwrap()), |
| 234 |
Endianness::Little => u16::from_le_bytes(bytes[..2].try_into().unwrap()), |
| 235 |
}; |
| 236 |
|
| 237 |
(val as u32, 3) |
| 238 |
} |
| 239 |
U32_BYTE => { |
| 240 |
let val = match endian { |
| 241 |
Endianness::Big => u32::from_be_bytes(bytes[..4].try_into().unwrap()), |
| 242 |
Endianness::Little => u32::from_le_bytes(bytes[..4].try_into().unwrap()), |
| 243 |
}; |
| 244 |
|
| 245 |
(val, 5) |
| 246 |
} |
| 247 |
U64_BYTE => return invalid_varint_discriminant(IntegerType::U32, IntegerType::U64), |
| 248 |
U128_BYTE => return invalid_varint_discriminant(IntegerType::U32, IntegerType::U128), |
| 249 |
_ => return invalid_varint_discriminant(IntegerType::U32, IntegerType::Reserved), |
| 250 |
}; |
| 251 |
|
| 252 |
read.consume(used); |
| 253 |
Ok(out) |
| 254 |
} else { |
| 255 |
deserialize_varint_cold_u32(read, endian) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
pub fn varint_decode_u64<R: Reader>(read: &mut R, endian: Endianness) -> Result<u64, DecodeError> { |
| 260 |
if let Some(bytes) = read.peek_read(9) { |
| 261 |
let (discriminant, bytes) = bytes.split_at(1); |
| 262 |
let (out, used) = match discriminant[0] { |
| 263 |
byte @ 0..=SINGLE_BYTE_MAX => (byte as u64, 1), |
| 264 |
U16_BYTE => { |
| 265 |
let val = match endian { |
| 266 |
Endianness::Big => u16::from_be_bytes(bytes[..2].try_into().unwrap()), |
| 267 |
Endianness::Little => u16::from_le_bytes(bytes[..2].try_into().unwrap()), |
| 268 |
}; |
| 269 |
|
| 270 |
(val as u64, 3) |
| 271 |
} |
| 272 |
U32_BYTE => { |
| 273 |
let val = match endian { |
| 274 |
Endianness::Big => u32::from_be_bytes(bytes[..4].try_into().unwrap()), |
| 275 |
Endianness::Little => u32::from_le_bytes(bytes[..4].try_into().unwrap()), |
| 276 |
}; |
| 277 |
|
| 278 |
(val as u64, 5) |
| 279 |
} |
| 280 |
U64_BYTE => { |
| 281 |
let val = match endian { |
| 282 |
Endianness::Big => u64::from_be_bytes(bytes[..8].try_into().unwrap()), |
| 283 |
Endianness::Little => u64::from_le_bytes(bytes[..8].try_into().unwrap()), |
| 284 |
}; |
| 285 |
|
| 286 |
(val, 9) |
| 287 |
} |
| 288 |
U128_BYTE => return invalid_varint_discriminant(IntegerType::U32, IntegerType::U128), |
| 289 |
_ => return invalid_varint_discriminant(IntegerType::U32, IntegerType::Reserved), |
| 290 |
}; |
| 291 |
|
| 292 |
read.consume(used); |
| 293 |
Ok(out) |
| 294 |
} else { |
| 295 |
deserialize_varint_cold_u64(read, endian) |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
pub fn varint_decode_usize<R: Reader>( |
| 300 |
read: &mut R, |
| 301 |
endian: Endianness, |
| 302 |
) -> Result<usize, DecodeError> { |
| 303 |
if let Some(bytes) = read.peek_read(9) { |
| 304 |
let (discriminant, bytes) = bytes.split_at(1); |
| 305 |
let (out, used) = match discriminant[0] { |
| 306 |
byte @ 0..=SINGLE_BYTE_MAX => (byte as usize, 1), |
| 307 |
U16_BYTE => { |
| 308 |
let val = match endian { |
| 309 |
Endianness::Big => u16::from_be_bytes(bytes[..2].try_into().unwrap()), |
| 310 |
Endianness::Little => u16::from_le_bytes(bytes[..2].try_into().unwrap()), |
| 311 |
}; |
| 312 |
|
| 313 |
(val as usize, 3) |
| 314 |
} |
| 315 |
U32_BYTE => { |
| 316 |
let val = match endian { |
| 317 |
Endianness::Big => u32::from_be_bytes(bytes[..4].try_into().unwrap()), |
| 318 |
Endianness::Little => u32::from_le_bytes(bytes[..4].try_into().unwrap()), |
| 319 |
}; |
| 320 |
|
| 321 |
(val as usize, 5) |
| 322 |
} |
| 323 |
U64_BYTE => { |
| 324 |
let val = match endian { |
| 325 |
Endianness::Big => u64::from_be_bytes(bytes[..8].try_into().unwrap()), |
| 326 |
Endianness::Little => u64::from_le_bytes(bytes[..8].try_into().unwrap()), |
| 327 |
}; |
| 328 |
|
| 329 |
(val as usize, 9) |
| 330 |
} |
| 331 |
U128_BYTE => return invalid_varint_discriminant(IntegerType::Usize, IntegerType::U128), |
| 332 |
_ => return invalid_varint_discriminant(IntegerType::Usize, IntegerType::Reserved), |
| 333 |
}; |
| 334 |
|
| 335 |
read.consume(used); |
| 336 |
Ok(out) |
| 337 |
} else { |
| 338 |
deserialize_varint_cold_usize(read, endian) |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
pub fn varint_decode_u128<R: Reader>( |
| 343 |
read: &mut R, |
| 344 |
endian: Endianness, |
| 345 |
) -> Result<u128, DecodeError> { |
| 346 |
if let Some(bytes) = read.peek_read(17) { |
| 347 |
let (discriminant, bytes) = bytes.split_at(1); |
| 348 |
let (out, used) = match discriminant[0] { |
| 349 |
byte @ 0..=SINGLE_BYTE_MAX => (byte as u128, 1), |
| 350 |
U16_BYTE => { |
| 351 |
let val = match endian { |
| 352 |
Endianness::Big => u16::from_be_bytes(bytes[..2].try_into().unwrap()), |
| 353 |
Endianness::Little => u16::from_le_bytes(bytes[..2].try_into().unwrap()), |
| 354 |
}; |
| 355 |
|
| 356 |
(val as u128, 3) |
| 357 |
} |
| 358 |
U32_BYTE => { |
| 359 |
let val = match endian { |
| 360 |
Endianness::Big => u32::from_be_bytes(bytes[..4].try_into().unwrap()), |
| 361 |
Endianness::Little => u32::from_le_bytes(bytes[..4].try_into().unwrap()), |
| 362 |
}; |
| 363 |
|
| 364 |
(val as u128, 5) |
| 365 |
} |
| 366 |
U64_BYTE => { |
| 367 |
let val = match endian { |
| 368 |
Endianness::Big => u64::from_be_bytes(bytes[..8].try_into().unwrap()), |
| 369 |
Endianness::Little => u64::from_le_bytes(bytes[..8].try_into().unwrap()), |
| 370 |
}; |
| 371 |
|
| 372 |
(val as u128, 9) |
| 373 |
} |
| 374 |
U128_BYTE => { |
| 375 |
let val = match endian { |
| 376 |
Endianness::Big => u128::from_be_bytes(bytes[..16].try_into().unwrap()), |
| 377 |
Endianness::Little => u128::from_le_bytes(bytes[..16].try_into().unwrap()), |
| 378 |
}; |
| 379 |
|
| 380 |
(val, 17) |
| 381 |
} |
| 382 |
_ => return invalid_varint_discriminant(IntegerType::Usize, IntegerType::Reserved), |
| 383 |
}; |
| 384 |
|
| 385 |
read.consume(used); |
| 386 |
Ok(out) |
| 387 |
} else { |
| 388 |
deserialize_varint_cold_u128(read, endian) |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
#[test] |
| 393 |
fn test_decode_u16() { |
| 394 |
let cases: &[(&[u8], u16, u16)] = &[ |
| 395 |
(&[0], 0, 0), |
| 396 |
(&[10], 10, 10), |
| 397 |
(&[U16_BYTE, 0, 10], 2560, 10), |
| 398 |
]; |
| 399 |
for &(slice, expected_le, expected_be) in cases { |
| 400 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 401 |
let found = varint_decode_u16(&mut reader, Endianness::Little).unwrap(); |
| 402 |
assert_eq!(expected_le, found); |
| 403 |
|
| 404 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 405 |
let found = varint_decode_u16(&mut reader, Endianness::Big).unwrap(); |
| 406 |
assert_eq!(expected_be, found); |
| 407 |
} |
| 408 |
|
| 409 |
let errors: &[(&[u8], DecodeError)] = &[ |
| 410 |
( |
| 411 |
&[U32_BYTE], |
| 412 |
DecodeError::InvalidIntegerType { |
| 413 |
expected: IntegerType::U16, |
| 414 |
found: IntegerType::U32, |
| 415 |
}, |
| 416 |
), |
| 417 |
( |
| 418 |
&[U64_BYTE], |
| 419 |
DecodeError::InvalidIntegerType { |
| 420 |
expected: IntegerType::U16, |
| 421 |
found: IntegerType::U64, |
| 422 |
}, |
| 423 |
), |
| 424 |
( |
| 425 |
&[U128_BYTE], |
| 426 |
DecodeError::InvalidIntegerType { |
| 427 |
expected: IntegerType::U16, |
| 428 |
found: IntegerType::U128, |
| 429 |
}, |
| 430 |
), |
| 431 |
(&[U16_BYTE], DecodeError::UnexpectedEnd { additional: 2 }), |
| 432 |
(&[U16_BYTE, 0], DecodeError::UnexpectedEnd { additional: 1 }), |
| 433 |
]; |
| 434 |
|
| 435 |
for (slice, expected) in errors { |
| 436 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 437 |
let found = varint_decode_u16(&mut reader, Endianness::Little).unwrap_err(); |
| 438 |
assert_eq!(std::format!("{:?}", expected), std::format!("{:?}", found)); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
#[test] |
| 443 |
fn test_decode_u32() { |
| 444 |
let cases: &[(&[u8], u32, u32)] = &[ |
| 445 |
(&[0], 0, 0), |
| 446 |
(&[10], 10, 10), |
| 447 |
(&[U16_BYTE, 0, 10], 2560, 10), |
| 448 |
(&[U32_BYTE, 0, 0, 0, 10], 167_772_160, 10), |
| 449 |
]; |
| 450 |
for &(slice, expected_le, expected_be) in cases { |
| 451 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 452 |
let found = varint_decode_u32(&mut reader, Endianness::Little).unwrap(); |
| 453 |
assert_eq!(expected_le, found); |
| 454 |
|
| 455 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 456 |
let found = varint_decode_u32(&mut reader, Endianness::Big).unwrap(); |
| 457 |
assert_eq!(expected_be, found); |
| 458 |
} |
| 459 |
|
| 460 |
let errors: &[(&[u8], DecodeError)] = &[ |
| 461 |
( |
| 462 |
&[U64_BYTE], |
| 463 |
DecodeError::InvalidIntegerType { |
| 464 |
expected: IntegerType::U32, |
| 465 |
found: IntegerType::U64, |
| 466 |
}, |
| 467 |
), |
| 468 |
( |
| 469 |
&[U128_BYTE], |
| 470 |
DecodeError::InvalidIntegerType { |
| 471 |
expected: IntegerType::U32, |
| 472 |
found: IntegerType::U128, |
| 473 |
}, |
| 474 |
), |
| 475 |
(&[U16_BYTE], DecodeError::UnexpectedEnd { additional: 2 }), |
| 476 |
(&[U16_BYTE, 0], DecodeError::UnexpectedEnd { additional: 1 }), |
| 477 |
(&[U32_BYTE], DecodeError::UnexpectedEnd { additional: 4 }), |
| 478 |
(&[U32_BYTE, 0], DecodeError::UnexpectedEnd { additional: 3 }), |
| 479 |
( |
| 480 |
&[U32_BYTE, 0, 0], |
| 481 |
DecodeError::UnexpectedEnd { additional: 2 }, |
| 482 |
), |
| 483 |
( |
| 484 |
&[U32_BYTE, 0, 0, 0], |
| 485 |
DecodeError::UnexpectedEnd { additional: 1 }, |
| 486 |
), |
| 487 |
]; |
| 488 |
|
| 489 |
for (slice, expected) in errors { |
| 490 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 491 |
let found = varint_decode_u32(&mut reader, Endianness::Little).unwrap_err(); |
| 492 |
assert_eq!(std::format!("{:?}", expected), std::format!("{:?}", found)); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
#[test] |
| 497 |
fn test_decode_u64() { |
| 498 |
let cases: &[(&[u8], u64, u64)] = &[ |
| 499 |
(&[0], 0, 0), |
| 500 |
(&[10], 10, 10), |
| 501 |
(&[U16_BYTE, 0, 10], 2560, 10), |
| 502 |
(&[U32_BYTE, 0, 0, 0, 10], 167_772_160, 10), |
| 503 |
( |
| 504 |
&[U64_BYTE, 0, 0, 0, 0, 0, 0, 0, 10], |
| 505 |
720_575_940_379_279_360, |
| 506 |
10, |
| 507 |
), |
| 508 |
]; |
| 509 |
for &(slice, expected_le, expected_be) in cases { |
| 510 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 511 |
let found = varint_decode_u64(&mut reader, Endianness::Little).unwrap(); |
| 512 |
assert_eq!(expected_le, found); |
| 513 |
|
| 514 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 515 |
let found = varint_decode_u64(&mut reader, Endianness::Big).unwrap(); |
| 516 |
assert_eq!(expected_be, found); |
| 517 |
} |
| 518 |
|
| 519 |
let errors: &[(&[u8], DecodeError)] = &[ |
| 520 |
( |
| 521 |
&[U128_BYTE], |
| 522 |
DecodeError::InvalidIntegerType { |
| 523 |
expected: IntegerType::U64, |
| 524 |
found: IntegerType::U128, |
| 525 |
}, |
| 526 |
), |
| 527 |
(&[U16_BYTE], DecodeError::UnexpectedEnd { additional: 2 }), |
| 528 |
(&[U16_BYTE, 0], DecodeError::UnexpectedEnd { additional: 1 }), |
| 529 |
(&[U32_BYTE], DecodeError::UnexpectedEnd { additional: 4 }), |
| 530 |
(&[U32_BYTE, 0], DecodeError::UnexpectedEnd { additional: 3 }), |
| 531 |
( |
| 532 |
&[U32_BYTE, 0, 0], |
| 533 |
DecodeError::UnexpectedEnd { additional: 2 }, |
| 534 |
), |
| 535 |
( |
| 536 |
&[U32_BYTE, 0, 0, 0], |
| 537 |
DecodeError::UnexpectedEnd { additional: 1 }, |
| 538 |
), |
| 539 |
(&[U64_BYTE], DecodeError::UnexpectedEnd { additional: 8 }), |
| 540 |
(&[U64_BYTE, 0], DecodeError::UnexpectedEnd { additional: 7 }), |
| 541 |
( |
| 542 |
&[U64_BYTE, 0, 0], |
| 543 |
DecodeError::UnexpectedEnd { additional: 6 }, |
| 544 |
), |
| 545 |
( |
| 546 |
&[U64_BYTE, 0, 0, 0], |
| 547 |
DecodeError::UnexpectedEnd { additional: 5 }, |
| 548 |
), |
| 549 |
( |
| 550 |
&[U64_BYTE, 0, 0, 0, 0], |
| 551 |
DecodeError::UnexpectedEnd { additional: 4 }, |
| 552 |
), |
| 553 |
( |
| 554 |
&[U64_BYTE, 0, 0, 0, 0, 0], |
| 555 |
DecodeError::UnexpectedEnd { additional: 3 }, |
| 556 |
), |
| 557 |
( |
| 558 |
&[U64_BYTE, 0, 0, 0, 0, 0, 0], |
| 559 |
DecodeError::UnexpectedEnd { additional: 2 }, |
| 560 |
), |
| 561 |
( |
| 562 |
&[U64_BYTE, 0, 0, 0, 0, 0, 0, 0], |
| 563 |
DecodeError::UnexpectedEnd { additional: 1 }, |
| 564 |
), |
| 565 |
]; |
| 566 |
|
| 567 |
for (slice, expected) in errors { |
| 568 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 569 |
let found = varint_decode_u64(&mut reader, Endianness::Little).unwrap_err(); |
| 570 |
assert_eq!(std::format!("{:?}", expected), std::format!("{:?}", found)); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
#[test] |
| 575 |
fn test_decode_u128() { |
| 576 |
let cases: &[(&[u8], u128, u128)] = &[ |
| 577 |
(&[0], 0, 0), |
| 578 |
(&[10], 10, 10), |
| 579 |
(&[U16_BYTE, 0, 10], 2560, 10), |
| 580 |
(&[U32_BYTE, 0, 0, 0, 10], 167_772_160, 10), |
| 581 |
( |
| 582 |
&[U64_BYTE, 0, 0, 0, 0, 0, 0, 0, 10], |
| 583 |
720_575_940_379_279_360, |
| 584 |
10, |
| 585 |
), |
| 586 |
( |
| 587 |
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10], |
| 588 |
13_292_279_957_849_158_729_038_070_602_803_445_760, |
| 589 |
10, |
| 590 |
), |
| 591 |
]; |
| 592 |
for &(slice, expected_le, expected_be) in cases { |
| 593 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 594 |
let found = varint_decode_u128(&mut reader, Endianness::Little).unwrap(); |
| 595 |
assert_eq!(expected_le, found); |
| 596 |
|
| 597 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 598 |
let found = varint_decode_u128(&mut reader, Endianness::Big).unwrap(); |
| 599 |
assert_eq!(expected_be, found); |
| 600 |
} |
| 601 |
|
| 602 |
let errors: &[(&[u8], DecodeError)] = &[ |
| 603 |
(&[U16_BYTE], DecodeError::UnexpectedEnd { additional: 2 }), |
| 604 |
(&[U16_BYTE, 0], DecodeError::UnexpectedEnd { additional: 1 }), |
| 605 |
(&[U32_BYTE], DecodeError::UnexpectedEnd { additional: 4 }), |
| 606 |
(&[U32_BYTE, 0], DecodeError::UnexpectedEnd { additional: 3 }), |
| 607 |
( |
| 608 |
&[U32_BYTE, 0, 0], |
| 609 |
DecodeError::UnexpectedEnd { additional: 2 }, |
| 610 |
), |
| 611 |
( |
| 612 |
&[U32_BYTE, 0, 0, 0], |
| 613 |
DecodeError::UnexpectedEnd { additional: 1 }, |
| 614 |
), |
| 615 |
(&[U64_BYTE], DecodeError::UnexpectedEnd { additional: 8 }), |
| 616 |
(&[U64_BYTE, 0], DecodeError::UnexpectedEnd { additional: 7 }), |
| 617 |
( |
| 618 |
&[U64_BYTE, 0, 0], |
| 619 |
DecodeError::UnexpectedEnd { additional: 6 }, |
| 620 |
), |
| 621 |
( |
| 622 |
&[U64_BYTE, 0, 0, 0], |
| 623 |
DecodeError::UnexpectedEnd { additional: 5 }, |
| 624 |
), |
| 625 |
( |
| 626 |
&[U64_BYTE, 0, 0, 0, 0], |
| 627 |
DecodeError::UnexpectedEnd { additional: 4 }, |
| 628 |
), |
| 629 |
( |
| 630 |
&[U64_BYTE, 0, 0, 0, 0, 0], |
| 631 |
DecodeError::UnexpectedEnd { additional: 3 }, |
| 632 |
), |
| 633 |
( |
| 634 |
&[U64_BYTE, 0, 0, 0, 0, 0, 0], |
| 635 |
DecodeError::UnexpectedEnd { additional: 2 }, |
| 636 |
), |
| 637 |
( |
| 638 |
&[U64_BYTE, 0, 0, 0, 0, 0, 0, 0], |
| 639 |
DecodeError::UnexpectedEnd { additional: 1 }, |
| 640 |
), |
| 641 |
(&[U128_BYTE], DecodeError::UnexpectedEnd { additional: 16 }), |
| 642 |
( |
| 643 |
&[U128_BYTE, 0], |
| 644 |
DecodeError::UnexpectedEnd { additional: 15 }, |
| 645 |
), |
| 646 |
( |
| 647 |
&[U128_BYTE, 0, 0], |
| 648 |
DecodeError::UnexpectedEnd { additional: 14 }, |
| 649 |
), |
| 650 |
( |
| 651 |
&[U128_BYTE, 0, 0, 0], |
| 652 |
DecodeError::UnexpectedEnd { additional: 13 }, |
| 653 |
), |
| 654 |
( |
| 655 |
&[U128_BYTE, 0, 0, 0, 0], |
| 656 |
DecodeError::UnexpectedEnd { additional: 12 }, |
| 657 |
), |
| 658 |
( |
| 659 |
&[U128_BYTE, 0, 0, 0, 0, 0], |
| 660 |
DecodeError::UnexpectedEnd { additional: 11 }, |
| 661 |
), |
| 662 |
( |
| 663 |
&[U128_BYTE, 0, 0, 0, 0, 0, 0], |
| 664 |
DecodeError::UnexpectedEnd { additional: 10 }, |
| 665 |
), |
| 666 |
( |
| 667 |
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0], |
| 668 |
DecodeError::UnexpectedEnd { additional: 9 }, |
| 669 |
), |
| 670 |
( |
| 671 |
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0], |
| 672 |
DecodeError::UnexpectedEnd { additional: 8 }, |
| 673 |
), |
| 674 |
( |
| 675 |
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 676 |
DecodeError::UnexpectedEnd { additional: 7 }, |
| 677 |
), |
| 678 |
( |
| 679 |
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 680 |
DecodeError::UnexpectedEnd { additional: 6 }, |
| 681 |
), |
| 682 |
( |
| 683 |
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 684 |
DecodeError::UnexpectedEnd { additional: 5 }, |
| 685 |
), |
| 686 |
( |
| 687 |
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 688 |
DecodeError::UnexpectedEnd { additional: 4 }, |
| 689 |
), |
| 690 |
( |
| 691 |
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 692 |
DecodeError::UnexpectedEnd { additional: 3 }, |
| 693 |
), |
| 694 |
( |
| 695 |
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 696 |
DecodeError::UnexpectedEnd { additional: 2 }, |
| 697 |
), |
| 698 |
( |
| 699 |
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 700 |
DecodeError::UnexpectedEnd { additional: 1 }, |
| 701 |
), |
| 702 |
]; |
| 703 |
|
| 704 |
for (slice, expected) in errors { |
| 705 |
let mut reader = crate::de::read::SliceReader::new(slice); |
| 706 |
let found = varint_decode_u128(&mut reader, Endianness::Little).unwrap_err(); |
| 707 |
std::dbg!(slice); |
| 708 |
assert_eq!(std::format!("{:?}", expected), std::format!("{:?}", found)); |
| 709 |
} |
| 710 |
} |