MCPcopy Create free account
hub / github.com/apache/fory / write_map_dynamic

Function write_map_dynamic

rust/fory-core/src/serializer/codec.rs:2681–2804  ·  view source on GitHub ↗
(
    value: &HashMap<K, V>,
    context: &mut WriteContext,
)

Source from the content-addressed store, hash-verified

2679}
2680
2681fn write_map_dynamic<K, V, KC, VC>(
2682 value: &HashMap<K, V>,
2683 context: &mut WriteContext,
2684) -> Result<(), Error>
2685where
2686 K: Eq + Hash + 'static,
2687 V: 'static,
2688 KC: Codec<K>,
2689 VC: Codec<V>,
2690{
2691 let key_declared = !need_to_write_type_for_field(KC::static_type_id());
2692 let value_declared = !need_to_write_type_for_field(VC::static_type_id());
2693 let key_is_polymorphic = KC::is_polymorphic();
2694 let value_is_polymorphic = VC::is_polymorphic();
2695 let key_is_shared_ref = KC::is_shared_ref();
2696 let value_is_shared_ref = VC::is_shared_ref();
2697 let mut current_key_type_id: Option<std::any::TypeId> = None;
2698 let mut current_value_type_id: Option<std::any::TypeId> = None;
2699 let mut header_offset = 0;
2700 let mut pair_counter: u8 = 0;
2701 let mut need_write_header = true;
2702
2703 for (key, value) in value {
2704 let key_is_none = KC::is_none(key);
2705 let value_is_none = VC::is_none(value);
2706 if key_is_none || value_is_none {
2707 if pair_counter > 0 {
2708 context.writer.set_bytes(header_offset + 1, &[pair_counter]);
2709 pair_counter = 0;
2710 need_write_header = true;
2711 }
2712 if key_is_none && value_is_none {
2713 context.writer.write_u8(KEY_NULL | VALUE_NULL);
2714 continue;
2715 }
2716 if value_is_none {
2717 let mut header = VALUE_NULL;
2718 if key_is_shared_ref {
2719 header |= TRACKING_KEY_REF;
2720 }
2721 if key_declared && !key_is_polymorphic {
2722 header |= DECL_KEY_TYPE;
2723 context.writer.write_u8(header);
2724 } else {
2725 context.writer.write_u8(header);
2726 write_map_entry_type::<K, KC>(key, context)?;
2727 }
2728 write_map_entry_data::<K, KC>(key, context, key_is_shared_ref)?;
2729 continue;
2730 }
2731 let mut header = KEY_NULL;
2732 if value_is_shared_ref {
2733 header |= TRACKING_VALUE_REF;
2734 }
2735 if value_declared && !value_is_polymorphic {
2736 header |= DECL_VALUE_TYPE;
2737 context.writer.write_u8(header);
2738 } else {

Callers

nothing calls this directly

Calls 8

is_polymorphicClass · 0.85
is_shared_refClass · 0.85
SomeFunction · 0.85
set_bytesMethod · 0.80
write_u8Method · 0.80
lenMethod · 0.80
write_i16Method · 0.80

Tested by

no test coverage detected