scylla_cql/serialize/
raw_batch.rs1use super::batch::{BatchValues, BatchValuesIterator};
5use super::row::{RowSerializationContext, SerializedValues};
6use super::{RowWriter, SerializationError};
7
8pub trait RawBatchValues {
18 type RawBatchValuesIter<'r>: RawBatchValuesIterator<'r>
24 where
25 Self: 'r;
26
27 fn batch_values_iter(&self) -> Self::RawBatchValuesIter<'_>;
29}
30
31pub trait RawBatchValuesIterator<'a> {
40 fn serialize_next(&mut self, writer: &mut RowWriter) -> Option<Result<(), SerializationError>>;
42
43 fn is_empty_next(&mut self) -> Option<bool>;
45
46 fn skip_next(&mut self) -> Option<()>;
48
49 #[inline]
51 fn count(mut self) -> usize
52 where
53 Self: Sized,
54 {
55 std::iter::from_fn(|| self.skip_next()).count()
56 }
57}
58
59impl RawBatchValues for Vec<SerializedValues> {
61 type RawBatchValuesIter<'r>
62 = std::slice::Iter<'r, SerializedValues>
63 where
64 Self: 'r;
65
66 fn batch_values_iter(&self) -> Self::RawBatchValuesIter<'_> {
67 self.iter()
68 }
69}
70
71impl<'r> RawBatchValuesIterator<'r> for std::slice::Iter<'r, SerializedValues> {
72 #[inline]
73 fn serialize_next(&mut self, writer: &mut RowWriter) -> Option<Result<(), SerializationError>> {
74 self.next().map(|sv| {
75 writer.append_serialize_row(sv);
76 Ok(())
77 })
78 }
79
80 fn is_empty_next(&mut self) -> Option<bool> {
81 self.next().map(|sv| sv.is_empty())
82 }
83
84 #[inline]
85 fn skip_next(&mut self) -> Option<()> {
86 self.next().map(|_| ())
87 }
88
89 #[inline]
90 fn count(self) -> usize {
91 <_ as Iterator>::count(self)
92 }
93}
94
95pub struct RawBatchValuesAdapter<BV, CTX> {
97 batch_values: BV,
98 contexts: CTX,
99}
100
101impl<BV, CTX> RawBatchValuesAdapter<BV, CTX> {
102 #[inline]
104 pub fn new(batch_values: BV, contexts: CTX) -> Self {
105 Self {
106 batch_values,
107 contexts,
108 }
109 }
110}
111
112impl<'ctx, BV, CTX> RawBatchValues for RawBatchValuesAdapter<BV, CTX>
113where
114 BV: BatchValues,
115 CTX: Iterator<Item = RowSerializationContext<'ctx>> + Clone,
116{
117 type RawBatchValuesIter<'r>
118 = RawBatchValuesIteratorAdapter<BV::BatchValuesIter<'r>, CTX>
119 where
120 Self: 'r;
121
122 #[inline]
123 fn batch_values_iter(&self) -> Self::RawBatchValuesIter<'_> {
124 RawBatchValuesIteratorAdapter {
125 batch_values_iterator: self.batch_values.batch_values_iter(),
126 contexts: self.contexts.clone(),
127 }
128 }
129}
130
131pub struct RawBatchValuesIteratorAdapter<BVI, CTX> {
133 batch_values_iterator: BVI,
134 contexts: CTX,
135}
136
137impl<'bvi, 'ctx, BVI, CTX> RawBatchValuesIterator<'bvi> for RawBatchValuesIteratorAdapter<BVI, CTX>
138where
139 BVI: BatchValuesIterator<'bvi>,
140 CTX: Iterator<Item = RowSerializationContext<'ctx>>,
141{
142 #[inline]
143 fn serialize_next(&mut self, writer: &mut RowWriter) -> Option<Result<(), SerializationError>> {
144 let ctx = self
149 .contexts
150 .next()
151 .unwrap_or(RowSerializationContext::empty());
152 self.batch_values_iterator.serialize_next(&ctx, writer)
153 }
154
155 fn is_empty_next(&mut self) -> Option<bool> {
156 let _ = self.contexts.next();
157 let ret = self.batch_values_iterator.is_empty_next()?;
158 Some(ret)
159 }
160
161 #[inline]
162 fn skip_next(&mut self) -> Option<()> {
163 let _ = self.contexts.next();
164 self.batch_values_iterator.skip_next()?;
165 Some(())
166 }
167}