MCPcopy Create free account
hub / github.com/apache/arrow-rs / from_iter

Method from_iter

arrow-buffer/src/buffer/mutable.rs:1143–1193  ·  view source on GitHub ↗
(iter: I)

Source from the content-addressed store, hash-verified

1141/// Creating a `MutableBuffer` instance by setting bits according to the boolean values
1142impl std::iter::FromIterator<bool> for MutableBuffer {
1143 fn from_iter<I>(iter: I) -> Self
1144 where
1145 I: IntoIterator<Item = bool>,
1146 {
1147 let mut iterator = iter.into_iter();
1148 let mut result = {
1149 let byte_capacity: usize = iterator.size_hint().0.saturating_add(7) / 8;
1150 MutableBuffer::new(byte_capacity)
1151 };
1152
1153 loop {
1154 let mut exhausted = false;
1155 let mut byte_accum: u8 = 0;
1156 let mut mask: u8 = 1;
1157
1158 //collect (up to) 8 bits into a byte
1159 while mask != 0 {
1160 if let Some(value) = iterator.next() {
1161 byte_accum |= match value {
1162 true => mask,
1163 false => 0,
1164 };
1165 mask <<= 1;
1166 } else {
1167 exhausted = true;
1168 break;
1169 }
1170 }
1171
1172 // break if the iterator was exhausted before it provided a bool for this byte
1173 if exhausted && mask == 1 {
1174 break;
1175 }
1176
1177 //ensure we have capacity to write the byte
1178 if result.len() == result.capacity() {
1179 //no capacity for new byte, allocate 1 byte more (plus however many more the iterator advertises)
1180 let additional_byte_capacity = 1usize.saturating_add(
1181 iterator.size_hint().0.saturating_add(7) / 8, //convert bit count to byte count, rounding up
1182 );
1183 result.reserve(additional_byte_capacity)
1184 }
1185
1186 // Soundness: capacity was allocated above
1187 unsafe { result.push_unchecked(byte_accum) };
1188 if exhausted {
1189 break;
1190 }
1191 }
1192 result
1193 }
1194}
1195
1196impl<T: ArrowNativeType> std::iter::FromIterator<T> for MutableBuffer {

Callers

nothing calls this directly

Calls 9

defaultFunction · 0.85
push_uncheckedMethod · 0.80
into_iterMethod · 0.45
size_hintMethod · 0.45
nextMethod · 0.45
lenMethod · 0.45
capacityMethod · 0.45
reserveMethod · 0.45
extend_from_iterMethod · 0.45

Tested by

no test coverage detected