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

Function write_collection_data

rust/fory-core/src/serializer/collection.rs:53–113  ·  view source on GitHub ↗
(
    iter: I,
    context: &mut WriteContext,
    has_generics: bool,
)

Source from the content-addressed store, hash-verified

51}
52
53pub fn write_collection_data<'a, T, I>(
54 iter: I,
55 context: &mut WriteContext,
56 has_generics: bool,
57) -> Result<(), Error>
58where
59 T: Serializer + 'a,
60 I: IntoIterator<Item = &'a T>,
61 I::IntoIter: ExactSizeIterator + Clone,
62{
63 let iter = iter.into_iter();
64 let len = iter.len();
65 context.writer.write_var_u32(len as u32);
66 if len == 0 {
67 return Ok(());
68 }
69 if T::fory_is_polymorphic() || T::fory_is_shared_ref() {
70 return write_collection_data_dyn_ref(iter, context, has_generics);
71 }
72 let mut header = IS_SAME_TYPE;
73 let mut has_null = false;
74 let elem_static_type_id = T::fory_static_type_id();
75 let is_elem_declared = has_generics && !need_to_write_type_for_field(elem_static_type_id);
76 if T::fory_is_option() {
77 // iter.clone() is zero-copy
78 for item in iter.clone() {
79 if item.fory_is_none() {
80 has_null = true;
81 break;
82 }
83 }
84 }
85 if has_null {
86 header |= HAS_NULL;
87 }
88 if is_elem_declared {
89 header |= DECL_ELEMENT_TYPE;
90 context.writer.write_u8(header);
91 } else {
92 context.writer.write_u8(header);
93 T::fory_write_type_info(context)?;
94 }
95 // Pre-reserve buffer space to avoid per-element capacity checks in the write loop.
96 context.writer.reserve(len * T::fory_reserved_space());
97 if !has_null {
98 for item in iter {
99 item.fory_write_data_generic(context, has_generics)?;
100 }
101 } else {
102 for item in iter {
103 if item.fory_is_none() {
104 context.writer.write_u8(RefFlag::Null as u8);
105 continue;
106 }
107 context.writer.write_u8(RefFlag::NotNullValue as u8);
108 item.fory_write_data_generic(context, has_generics)?;
109 }
110 }

Callers 8

fory_write_dataMethod · 0.85
fory_write_dataMethod · 0.85
fory_write_dataMethod · 0.85
fory_write_dataMethod · 0.85

Calls 11

fory_write_type_infoFunction · 0.85
fory_reserved_spaceFunction · 0.85
lenMethod · 0.80
write_var_u32Method · 0.80
write_u8Method · 0.80
cloneMethod · 0.45
fory_is_noneMethod · 0.45
reserveMethod · 0.45

Tested by

no test coverage detected