| 497 | } |
| 498 | |
| 499 | fn deserialize_value_depth<R: Read, Bag: MarshalBag>( |
| 500 | rdr: &mut R, |
| 501 | bag: Bag, |
| 502 | depth: usize, |
| 503 | refs: &mut Vec<Option<Bag::Value>>, |
| 504 | ) -> Result<Bag::Value> { |
| 505 | if depth == 0 { |
| 506 | return Err(MarshalError::InvalidBytecode); |
| 507 | } |
| 508 | let raw = rdr.read_u8()?; |
| 509 | let flag = raw & FLAG_REF != 0; |
| 510 | let type_code = raw & !FLAG_REF; |
| 511 | |
| 512 | // TYPE_REF: return previously stored object |
| 513 | if type_code == Type::Ref as u8 { |
| 514 | let idx = rdr.read_u32()? as usize; |
| 515 | return refs |
| 516 | .get(idx) |
| 517 | .and_then(|v| v.clone()) |
| 518 | .ok_or(MarshalError::InvalidBytecode); |
| 519 | } |
| 520 | |
| 521 | // Reserve ref slot before reading (matches write order) |
| 522 | let slot = if flag { |
| 523 | let idx = refs.len(); |
| 524 | refs.push(None); |
| 525 | Some(idx) |
| 526 | } else { |
| 527 | None |
| 528 | }; |
| 529 | |
| 530 | let typ = Type::try_from(type_code)?; |
| 531 | let value = deserialize_value_typed(rdr, bag, depth, refs, typ)?; |
| 532 | |
| 533 | if let Some(idx) = slot { |
| 534 | refs[idx] = Some(value.clone()); |
| 535 | } |
| 536 | Ok(value) |
| 537 | } |
| 538 | |
| 539 | fn deserialize_value_typed<R: Read, Bag: MarshalBag>( |
| 540 | rdr: &mut R, |