Skip to content

Commit d195672

Browse files
authored
frame/value: simplify SerializedValuesIterator (#414)
It turns our that the implementation of SerializedValuesIterator was needlessly complicated. This commit removes the next_offset field and reimplements .next() in terms of the utility parsing functions in the frame::types module. Fixes #384
1 parent 094dbe6 commit d195672

File tree

1 file changed

+5
-31
lines changed

1 file changed

+5
-31
lines changed

scylla/src/frame/value.rs

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ impl SerializedValues {
179179
pub fn iter(&self) -> impl Iterator<Item = Option<&[u8]>> {
180180
SerializedValuesIterator {
181181
serialized_values: &self.serialized_values,
182-
next_offset: 0,
183182
contains_names: self.contains_names,
184183
}
185184
}
@@ -201,48 +200,23 @@ impl SerializedValues {
201200
#[derive(Clone, Copy)]
202201
pub struct SerializedValuesIterator<'a> {
203202
serialized_values: &'a [u8],
204-
next_offset: usize,
205203
contains_names: bool,
206204
}
207205

208206
impl<'a> Iterator for SerializedValuesIterator<'a> {
209207
type Item = Option<&'a [u8]>;
210208

211209
fn next(&mut self) -> Option<Self::Item> {
212-
// Read next value's 4 byte size, return it, advance.
213-
// In case of named values, skip names
214-
if self.contains_names {
215-
if self.next_offset + 2 > self.serialized_values.len() {
216-
return None;
217-
}
218-
let mut len_bytes = &self.serialized_values[self.next_offset..(self.next_offset + 2)];
219-
let next_name_len: usize = types::read_short_length(&mut len_bytes).ok()?;
220-
self.next_offset += 2 + next_name_len;
221-
}
222-
223-
if self.next_offset + 4 > self.serialized_values.len() {
224-
// Reached the end - nothing more to read
210+
if self.serialized_values.is_empty() {
225211
return None;
226212
}
227213

228-
let len_bytes: [u8; 4] = self.serialized_values[self.next_offset..(self.next_offset + 4)]
229-
.try_into()
230-
.unwrap();
231-
232-
let next_val_len: i32 = i32::from_be_bytes(len_bytes);
233-
234-
if next_val_len < 0 {
235-
// Next value was NULL
236-
self.next_offset += 4;
237-
return Some(None);
214+
// In case of named values, skip names
215+
if self.contains_names {
216+
types::read_short_bytes(&mut self.serialized_values).expect("badly encoded value name");
238217
}
239218

240-
// Found next value - get the slice and return it
241-
let val_len: usize = next_val_len.try_into().unwrap();
242-
let result: &[u8] =
243-
&self.serialized_values[(self.next_offset + 4)..(self.next_offset + 4 + val_len)];
244-
self.next_offset += 4 + val_len;
245-
Some(Some(result))
219+
Some(types::read_bytes_opt(&mut self.serialized_values).expect("badly encoded value"))
246220
}
247221
}
248222

0 commit comments

Comments
 (0)