scylla_cql/frame/
value.rs

1use crate::frame::types;
2use bytes::BufMut;
3use std::borrow::Cow;
4use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
5use std::convert::TryInto;
6use std::hash::BuildHasher;
7use std::net::IpAddr;
8use thiserror::Error;
9use uuid::Uuid;
10
11use super::response::result::CqlValue;
12use super::types::vint_encode;
13use super::types::RawValue;
14
15#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
16#[error("Value is too large to fit in the CQL type")]
17pub struct ValueOverflow;
18
19/// Represents an unset value
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
21pub struct Unset;
22
23/// Represents an counter value
24#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
25pub struct Counter(pub i64);
26
27/// Enum providing a way to represent a value that might be unset
28#[derive(Clone, Copy, Default)]
29pub enum MaybeUnset<V> {
30    #[default]
31    Unset,
32    Set(V),
33}
34
35/// Represents timeuuid (uuid V1) value
36///
37/// This type has custom comparison logic which follows Scylla/Cassandra semantics.
38/// For details, see [`Ord` implementation](#impl-Ord-for-CqlTimeuuid).
39#[derive(Debug, Clone, Copy, Eq)]
40pub struct CqlTimeuuid(Uuid);
41
42/// [`Uuid`] delegate methods
43impl CqlTimeuuid {
44    pub fn as_bytes(&self) -> &[u8; 16] {
45        self.0.as_bytes()
46    }
47
48    pub fn as_u128(&self) -> u128 {
49        self.0.as_u128()
50    }
51
52    pub fn as_fields(&self) -> (u32, u16, u16, &[u8; 8]) {
53        self.0.as_fields()
54    }
55
56    pub fn as_u64_pair(&self) -> (u64, u64) {
57        self.0.as_u64_pair()
58    }
59
60    pub fn from_slice(b: &[u8]) -> Result<Self, uuid::Error> {
61        Ok(Self(Uuid::from_slice(b)?))
62    }
63
64    pub fn from_slice_le(b: &[u8]) -> Result<Self, uuid::Error> {
65        Ok(Self(Uuid::from_slice_le(b)?))
66    }
67
68    pub fn from_bytes(bytes: [u8; 16]) -> Self {
69        Self(Uuid::from_bytes(bytes))
70    }
71
72    pub fn from_bytes_le(bytes: [u8; 16]) -> Self {
73        Self(Uuid::from_bytes_le(bytes))
74    }
75
76    pub fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Self {
77        Self(Uuid::from_fields(d1, d2, d3, d4))
78    }
79
80    pub fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Self {
81        Self(Uuid::from_fields_le(d1, d2, d3, d4))
82    }
83
84    pub fn from_u128(v: u128) -> Self {
85        Self(Uuid::from_u128(v))
86    }
87
88    pub fn from_u128_le(v: u128) -> Self {
89        Self(Uuid::from_u128_le(v))
90    }
91
92    pub fn from_u64_pair(high_bits: u64, low_bits: u64) -> Self {
93        Self(Uuid::from_u64_pair(high_bits, low_bits))
94    }
95}
96
97impl CqlTimeuuid {
98    /// Read 8 most significant bytes of timeuuid from serialized bytes
99    fn msb(&self) -> u64 {
100        // Scylla and Cassandra use a standard UUID memory layout for MSB:
101        // 4 bytes    2 bytes    2 bytes
102        // time_low - time_mid - time_hi_and_version
103        let bytes = self.0.as_bytes();
104        ((bytes[6] & 0x0F) as u64) << 56
105            | (bytes[7] as u64) << 48
106            | (bytes[4] as u64) << 40
107            | (bytes[5] as u64) << 32
108            | (bytes[0] as u64) << 24
109            | (bytes[1] as u64) << 16
110            | (bytes[2] as u64) << 8
111            | (bytes[3] as u64)
112    }
113
114    fn lsb(&self) -> u64 {
115        let bytes = self.0.as_bytes();
116        (bytes[8] as u64) << 56
117            | (bytes[9] as u64) << 48
118            | (bytes[10] as u64) << 40
119            | (bytes[11] as u64) << 32
120            | (bytes[12] as u64) << 24
121            | (bytes[13] as u64) << 16
122            | (bytes[14] as u64) << 8
123            | (bytes[15] as u64)
124    }
125
126    fn lsb_signed(&self) -> u64 {
127        self.lsb() ^ 0x8080808080808080
128    }
129}
130
131impl std::str::FromStr for CqlTimeuuid {
132    type Err = uuid::Error;
133
134    fn from_str(s: &str) -> Result<Self, Self::Err> {
135        Ok(Self(Uuid::from_str(s)?))
136    }
137}
138
139impl std::fmt::Display for CqlTimeuuid {
140    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
141        write!(f, "{}", self.0)
142    }
143}
144
145impl AsRef<Uuid> for CqlTimeuuid {
146    fn as_ref(&self) -> &Uuid {
147        &self.0
148    }
149}
150
151impl From<CqlTimeuuid> for Uuid {
152    fn from(value: CqlTimeuuid) -> Self {
153        value.0
154    }
155}
156
157impl From<Uuid> for CqlTimeuuid {
158    fn from(value: Uuid) -> Self {
159        Self(value)
160    }
161}
162
163/// Compare two values of timeuuid type.
164///
165/// Cassandra legacy requires:
166/// - converting 8 most significant bytes to date, which is then compared.
167/// - masking off UUID version from the 8 ms-bytes during compare, to
168///   treat possible non-version-1 UUID the same way as UUID.
169/// - using signed compare for least significant bits.
170impl Ord for CqlTimeuuid {
171    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
172        let mut res = self.msb().cmp(&other.msb());
173        if let std::cmp::Ordering::Equal = res {
174            res = self.lsb_signed().cmp(&other.lsb_signed());
175        }
176        res
177    }
178}
179
180impl PartialOrd for CqlTimeuuid {
181    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
182        Some(self.cmp(other))
183    }
184}
185
186impl PartialEq for CqlTimeuuid {
187    fn eq(&self, other: &Self) -> bool {
188        self.cmp(other) == std::cmp::Ordering::Equal
189    }
190}
191
192impl std::hash::Hash for CqlTimeuuid {
193    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
194        self.lsb_signed().hash(state);
195        self.msb().hash(state);
196    }
197}
198
199/// Native CQL `varint` representation.
200///
201/// Represented as two's-complement binary in big-endian order.
202///
203/// This type is a raw representation in bytes. It's the default
204/// implementation of `varint` type - independent of any
205/// external crates and crate features.
206///
207/// The type is not very useful in most use cases.
208/// However, users can make use of more complex types
209/// such as `num_bigint::BigInt` (v0.3/v0.4).
210/// The library support (e.g. conversion from [`CqlValue`]) for these types is
211/// enabled via `num-bigint-03` and `num-bigint-04` crate features.
212///
213/// # DB data format
214/// Notice that [constructors](CqlVarint#impl-CqlVarint)
215/// don't perform any normalization on the provided data.
216/// This means that underlying bytes may contain leading zeros.
217///
218/// Currently, Scylla and Cassandra support non-normalized `varint` values.
219/// Bytes provided by the user via constructor are passed to DB as is.
220///
221/// The implementation of [`PartialEq`], however, normalizes the underlying bytes
222/// before comparison. For details, check [examples](#impl-PartialEq-for-CqlVarint).
223#[derive(Clone, Eq, Debug)]
224pub struct CqlVarint(Vec<u8>);
225
226/// Constructors from bytes
227impl CqlVarint {
228    /// Creates a [`CqlVarint`] from an array of bytes in
229    /// two's complement big-endian binary representation.
230    ///
231    /// See: disclaimer about [non-normalized values](CqlVarint#db-data-format).
232    pub fn from_signed_bytes_be(digits: Vec<u8>) -> Self {
233        Self(digits)
234    }
235
236    /// Creates a [`CqlVarint`] from a slice of bytes in
237    /// two's complement binary big-endian representation.
238    ///
239    /// See: disclaimer about [non-normalized values](CqlVarint#db-data-format).
240    pub fn from_signed_bytes_be_slice(digits: &[u8]) -> Self {
241        Self::from_signed_bytes_be(digits.to_vec())
242    }
243}
244
245/// Conversion to bytes
246impl CqlVarint {
247    /// Converts [`CqlVarint`] to an array of bytes in two's
248    /// complement binary big-endian representation.
249    pub fn into_signed_bytes_be(self) -> Vec<u8> {
250        self.0
251    }
252
253    /// Returns a slice of bytes in two's complement
254    /// binary big-endian representation.
255    pub fn as_signed_bytes_be_slice(&self) -> &[u8] {
256        &self.0
257    }
258}
259
260impl CqlVarint {
261    fn as_normalized_slice(&self) -> &[u8] {
262        let digits = self.0.as_slice();
263        if digits.is_empty() {
264            // num-bigint crate normalizes empty vector to 0.
265            // We will follow the same approach.
266            return &[0];
267        }
268
269        let non_zero_position = match digits.iter().position(|b| *b != 0) {
270            Some(pos) => pos,
271            None => {
272                // Vector is filled with zeros. Represent it as 0.
273                return &[0];
274            }
275        };
276
277        if non_zero_position > 0 {
278            // There were some leading zeros.
279            // Now, there are two cases:
280            let zeros_to_remove = if digits[non_zero_position] > 0x7f {
281                // Most significant bit is 1, so we need to include one of the leading
282                // zeros as originally it represented a positive number.
283                non_zero_position - 1
284            } else {
285                // Most significant bit is 0 - positive number with no leading zeros.
286                non_zero_position
287            };
288            return &digits[zeros_to_remove..];
289        }
290
291        // There were no leading zeros at all - leave as is.
292        digits
293    }
294}
295
296#[cfg(feature = "num-bigint-03")]
297impl From<num_bigint_03::BigInt> for CqlVarint {
298    fn from(value: num_bigint_03::BigInt) -> Self {
299        Self(value.to_signed_bytes_be())
300    }
301}
302
303#[cfg(feature = "num-bigint-03")]
304impl From<CqlVarint> for num_bigint_03::BigInt {
305    fn from(val: CqlVarint) -> Self {
306        num_bigint_03::BigInt::from_signed_bytes_be(&val.0)
307    }
308}
309
310#[cfg(feature = "num-bigint-04")]
311impl From<num_bigint_04::BigInt> for CqlVarint {
312    fn from(value: num_bigint_04::BigInt) -> Self {
313        Self(value.to_signed_bytes_be())
314    }
315}
316
317#[cfg(feature = "num-bigint-04")]
318impl From<CqlVarint> for num_bigint_04::BigInt {
319    fn from(val: CqlVarint) -> Self {
320        num_bigint_04::BigInt::from_signed_bytes_be(&val.0)
321    }
322}
323
324/// Compares two [`CqlVarint`] values after normalization.
325///
326/// # Example
327///
328/// ```rust
329/// # use scylla_cql::frame::value::CqlVarint;
330/// let non_normalized_bytes = vec![0x00, 0x01];
331/// let normalized_bytes = vec![0x01];
332/// assert_eq!(
333///     CqlVarint::from_signed_bytes_be(non_normalized_bytes),
334///     CqlVarint::from_signed_bytes_be(normalized_bytes)
335/// );
336/// ```
337impl PartialEq for CqlVarint {
338    fn eq(&self, other: &Self) -> bool {
339        self.as_normalized_slice() == other.as_normalized_slice()
340    }
341}
342
343/// Computes the hash of normalized [`CqlVarint`].
344impl std::hash::Hash for CqlVarint {
345    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
346        self.as_normalized_slice().hash(state)
347    }
348}
349
350/// Native CQL `decimal` representation.
351///
352/// Represented as a pair:
353/// - a [`CqlVarint`] value
354/// - 32-bit integer which determines the position of the decimal point
355///
356/// The type is not very useful in most use cases.
357/// However, users can make use of more complex types
358/// such as `bigdecimal::BigDecimal` (v0.4).
359/// The library support (e.g. conversion from [`CqlValue`]) for the type is
360/// enabled via `bigdecimal-04` crate feature.
361///
362/// # DB data format
363/// Notice that [constructors](CqlDecimal#impl-CqlDecimal)
364/// don't perform any normalization on the provided data.
365/// For more details, see [`CqlVarint`] documentation.
366#[derive(Clone, PartialEq, Eq, Debug)]
367pub struct CqlDecimal {
368    int_val: CqlVarint,
369    scale: i32,
370}
371
372/// Constructors
373impl CqlDecimal {
374    /// Creates a [`CqlDecimal`] from an array of bytes
375    /// representing [`CqlVarint`] and a 32-bit scale.
376    ///
377    /// See: disclaimer about [non-normalized values](CqlVarint#db-data-format).
378    pub fn from_signed_be_bytes_and_exponent(bytes: Vec<u8>, scale: i32) -> Self {
379        Self {
380            int_val: CqlVarint::from_signed_bytes_be(bytes),
381            scale,
382        }
383    }
384
385    /// Creates a [`CqlDecimal`] from a slice of bytes
386    /// representing [`CqlVarint`] and a 32-bit scale.
387    ///
388    /// See: disclaimer about [non-normalized values](CqlVarint#db-data-format).
389    pub fn from_signed_be_bytes_slice_and_exponent(bytes: &[u8], scale: i32) -> Self {
390        Self::from_signed_be_bytes_and_exponent(bytes.to_vec(), scale)
391    }
392}
393
394/// Conversion to raw bytes
395impl CqlDecimal {
396    /// Returns a slice of bytes in two's complement
397    /// binary big-endian representation and a scale.
398    pub fn as_signed_be_bytes_slice_and_exponent(&self) -> (&[u8], i32) {
399        (self.int_val.as_signed_bytes_be_slice(), self.scale)
400    }
401
402    /// Converts [`CqlDecimal`] to an array of bytes in two's
403    /// complement binary big-endian representation and a scale.
404    pub fn into_signed_be_bytes_and_exponent(self) -> (Vec<u8>, i32) {
405        (self.int_val.into_signed_bytes_be(), self.scale)
406    }
407}
408
409#[cfg(feature = "bigdecimal-04")]
410impl From<CqlDecimal> for bigdecimal_04::BigDecimal {
411    fn from(value: CqlDecimal) -> Self {
412        Self::from((
413            bigdecimal_04::num_bigint::BigInt::from_signed_bytes_be(
414                value.int_val.as_signed_bytes_be_slice(),
415            ),
416            value.scale as i64,
417        ))
418    }
419}
420
421#[cfg(feature = "bigdecimal-04")]
422impl TryFrom<bigdecimal_04::BigDecimal> for CqlDecimal {
423    type Error = <i64 as TryInto<i32>>::Error;
424
425    fn try_from(value: bigdecimal_04::BigDecimal) -> Result<Self, Self::Error> {
426        let (bigint, scale) = value.into_bigint_and_exponent();
427        let bytes = bigint.to_signed_bytes_be();
428        Ok(Self::from_signed_be_bytes_and_exponent(
429            bytes,
430            scale.try_into()?,
431        ))
432    }
433}
434
435/// Native CQL date representation that allows for a bigger range of dates (-262145-1-1 to 262143-12-31).
436///
437/// Represented as number of days since -5877641-06-23 i.e. 2^31 days before unix epoch.
438#[derive(Clone, Copy, PartialEq, Eq, Debug)]
439pub struct CqlDate(pub u32);
440
441/// Native CQL timestamp representation that allows full supported timestamp range.
442///
443/// Represented as signed milliseconds since unix epoch.
444#[derive(Clone, Copy, PartialEq, Eq, Debug)]
445pub struct CqlTimestamp(pub i64);
446
447/// Native CQL time representation.
448///
449/// Represented as nanoseconds since midnight.
450#[derive(Clone, Copy, PartialEq, Eq, Debug)]
451pub struct CqlTime(pub i64);
452
453#[cfg(feature = "chrono-04")]
454impl From<chrono_04::NaiveDate> for CqlDate {
455    fn from(value: chrono_04::NaiveDate) -> Self {
456        let unix_epoch = chrono_04::NaiveDate::from_yo_opt(1970, 1).unwrap();
457
458        // `NaiveDate` range is -262145-01-01 to 262143-12-31
459        // Both values are well within supported range
460        let days = ((1 << 31) + value.signed_duration_since(unix_epoch).num_days()) as u32;
461
462        Self(days)
463    }
464}
465
466#[cfg(feature = "chrono-04")]
467impl TryInto<chrono_04::NaiveDate> for CqlDate {
468    type Error = ValueOverflow;
469
470    fn try_into(self) -> Result<chrono_04::NaiveDate, Self::Error> {
471        let days_since_unix_epoch = self.0 as i64 - (1 << 31);
472
473        // date_days is u32 then converted to i64 then we subtract 2^31;
474        // Max value is 2^31, min value is -2^31. Both values can safely fit in chrono::Duration, this call won't panic
475        let duration_since_unix_epoch =
476            chrono_04::Duration::try_days(days_since_unix_epoch).unwrap();
477
478        chrono_04::NaiveDate::from_yo_opt(1970, 1)
479            .unwrap()
480            .checked_add_signed(duration_since_unix_epoch)
481            .ok_or(ValueOverflow)
482    }
483}
484
485#[cfg(feature = "chrono-04")]
486impl From<chrono_04::DateTime<chrono_04::Utc>> for CqlTimestamp {
487    fn from(value: chrono_04::DateTime<chrono_04::Utc>) -> Self {
488        Self(value.timestamp_millis())
489    }
490}
491
492#[cfg(feature = "chrono-04")]
493impl TryInto<chrono_04::DateTime<chrono_04::Utc>> for CqlTimestamp {
494    type Error = ValueOverflow;
495
496    fn try_into(self) -> Result<chrono_04::DateTime<chrono_04::Utc>, Self::Error> {
497        use chrono_04::TimeZone;
498        match chrono_04::Utc.timestamp_millis_opt(self.0) {
499            chrono_04::LocalResult::Single(datetime) => Ok(datetime),
500            _ => Err(ValueOverflow),
501        }
502    }
503}
504
505#[cfg(feature = "chrono-04")]
506impl TryFrom<chrono_04::NaiveTime> for CqlTime {
507    type Error = ValueOverflow;
508
509    fn try_from(value: chrono_04::NaiveTime) -> Result<Self, Self::Error> {
510        let nanos = value
511            .signed_duration_since(chrono_04::NaiveTime::MIN)
512            .num_nanoseconds()
513            .unwrap();
514
515        // Value can exceed max CQL time in case of leap second
516        if nanos <= 86399999999999 {
517            Ok(Self(nanos))
518        } else {
519            Err(ValueOverflow)
520        }
521    }
522}
523
524#[cfg(feature = "chrono-04")]
525impl TryInto<chrono_04::NaiveTime> for CqlTime {
526    type Error = ValueOverflow;
527
528    fn try_into(self) -> Result<chrono_04::NaiveTime, Self::Error> {
529        let secs = (self.0 / 1_000_000_000)
530            .try_into()
531            .map_err(|_| ValueOverflow)?;
532        let nanos = (self.0 % 1_000_000_000)
533            .try_into()
534            .map_err(|_| ValueOverflow)?;
535        chrono_04::NaiveTime::from_num_seconds_from_midnight_opt(secs, nanos).ok_or(ValueOverflow)
536    }
537}
538
539#[cfg(feature = "time-03")]
540impl From<time_03::Date> for CqlDate {
541    fn from(value: time_03::Date) -> Self {
542        const JULIAN_DAY_OFFSET: i64 =
543            (1 << 31) - time_03::OffsetDateTime::UNIX_EPOCH.date().to_julian_day() as i64;
544
545        // Statically assert that no possible value will ever overflow
546        const _: () = assert!(
547            time_03::Date::MAX.to_julian_day() as i64 + JULIAN_DAY_OFFSET < u32::MAX as i64
548        );
549        const _: () = assert!(
550            time_03::Date::MIN.to_julian_day() as i64 + JULIAN_DAY_OFFSET > u32::MIN as i64
551        );
552
553        let days = value.to_julian_day() as i64 + JULIAN_DAY_OFFSET;
554
555        Self(days as u32)
556    }
557}
558
559#[cfg(feature = "time-03")]
560impl TryInto<time_03::Date> for CqlDate {
561    type Error = ValueOverflow;
562
563    fn try_into(self) -> Result<time_03::Date, Self::Error> {
564        const JULIAN_DAY_OFFSET: i64 =
565            (1 << 31) - time_03::OffsetDateTime::UNIX_EPOCH.date().to_julian_day() as i64;
566
567        let julian_days = (self.0 as i64 - JULIAN_DAY_OFFSET)
568            .try_into()
569            .map_err(|_| ValueOverflow)?;
570
571        time_03::Date::from_julian_day(julian_days).map_err(|_| ValueOverflow)
572    }
573}
574
575#[cfg(feature = "time-03")]
576impl From<time_03::OffsetDateTime> for CqlTimestamp {
577    fn from(value: time_03::OffsetDateTime) -> Self {
578        // Statically assert that no possible value will ever overflow. OffsetDateTime doesn't allow offset to overflow
579        // the UTC PrimitiveDateTime value value
580        const _: () = assert!(
581            time_03::PrimitiveDateTime::MAX
582                .assume_utc()
583                .unix_timestamp_nanos()
584                // Nanos to millis
585                / 1_000_000
586                < i64::MAX as i128
587        );
588        const _: () = assert!(
589            time_03::PrimitiveDateTime::MIN
590                .assume_utc()
591                .unix_timestamp_nanos()
592                / 1_000_000
593                > i64::MIN as i128
594        );
595
596        // Edge cases were statically asserted above, checked math is not required
597        Self(value.unix_timestamp() * 1000 + value.millisecond() as i64)
598    }
599}
600
601#[cfg(feature = "time-03")]
602impl TryInto<time_03::OffsetDateTime> for CqlTimestamp {
603    type Error = ValueOverflow;
604
605    fn try_into(self) -> Result<time_03::OffsetDateTime, Self::Error> {
606        time_03::OffsetDateTime::from_unix_timestamp_nanos(self.0 as i128 * 1_000_000)
607            .map_err(|_| ValueOverflow)
608    }
609}
610
611#[cfg(feature = "time-03")]
612impl From<time_03::Time> for CqlTime {
613    fn from(value: time_03::Time) -> Self {
614        let (h, m, s, n) = value.as_hms_nano();
615
616        // no need for checked arithmetic as all these types are guaranteed to fit in i64 without overflow
617        let nanos = (h as i64 * 3600 + m as i64 * 60 + s as i64) * 1_000_000_000 + n as i64;
618
619        Self(nanos)
620    }
621}
622
623#[cfg(feature = "time-03")]
624impl TryInto<time_03::Time> for CqlTime {
625    type Error = ValueOverflow;
626
627    fn try_into(self) -> Result<time_03::Time, Self::Error> {
628        let h = self.0 / 3_600_000_000_000;
629        let m = self.0 / 60_000_000_000 % 60;
630        let s = self.0 / 1_000_000_000 % 60;
631        let n = self.0 % 1_000_000_000;
632
633        time_03::Time::from_hms_nano(
634            h.try_into().map_err(|_| ValueOverflow)?,
635            m as u8,
636            s as u8,
637            n as u32,
638        )
639        .map_err(|_| ValueOverflow)
640    }
641}
642
643/// Represents a CQL Duration value
644#[derive(Clone, Debug, Copy, PartialEq, Eq)]
645pub struct CqlDuration {
646    pub months: i32,
647    pub days: i32,
648    pub nanoseconds: i64,
649}
650
651#[deprecated(
652    since = "0.15.1",
653    note = "Legacy serialization API is not type-safe and is going to be removed soon"
654)]
655mod legacy {
656    #![allow(deprecated)]
657
658    use super::*;
659
660    /// Every value being sent in a query must implement this trait
661    /// serialize() should write the Value as [bytes] to the provided buffer
662    pub trait Value {
663        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig>;
664    }
665
666    #[derive(Debug, Error, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
667    #[error("Value too big to be sent in a request - max 2GiB allowed")]
668    pub struct ValueTooBig;
669
670    #[derive(Debug, Error, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
671    pub enum SerializeValuesError {
672        #[error("Too many values to add, max 65,535 values can be sent in a request")]
673        TooManyValues,
674        #[error("Mixing named and not named values is not allowed")]
675        MixingNamedAndNotNamedValues,
676        #[error(transparent)]
677        ValueTooBig(#[from] ValueTooBig),
678        #[error("Parsing serialized values failed")]
679        ParseError,
680    }
681
682    /// Keeps a buffer with serialized Values
683    /// Allows adding new Values and iterating over serialized ones
684    #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
685    pub struct LegacySerializedValues {
686        serialized_values: Vec<u8>,
687        values_num: u16,
688        contains_names: bool,
689    }
690
691    pub type SerializedResult<'a> = Result<Cow<'a, LegacySerializedValues>, SerializeValuesError>;
692
693    /// Represents list of values to be sent in a query
694    /// gets serialized and but into request
695    pub trait ValueList {
696        /// Provides a view of ValueList as LegacySerializedValues
697        /// returns `Cow<LegacySerializedValues>` to make impl ValueList for LegacySerializedValues efficient
698        fn serialized(&self) -> SerializedResult<'_>;
699
700        fn write_to_request(&self, buf: &mut impl BufMut) -> Result<(), SerializeValuesError> {
701            let serialized = self.serialized()?;
702            LegacySerializedValues::write_to_request(&serialized, buf);
703
704            Ok(())
705        }
706    }
707
708    impl Default for LegacySerializedValues {
709        fn default() -> Self {
710            Self::new()
711        }
712    }
713
714    impl LegacySerializedValues {
715        /// Creates empty value list
716        pub const fn new() -> Self {
717            LegacySerializedValues {
718                serialized_values: Vec::new(),
719                values_num: 0,
720                contains_names: false,
721            }
722        }
723
724        pub fn with_capacity(capacity: usize) -> Self {
725            LegacySerializedValues {
726                serialized_values: Vec::with_capacity(capacity),
727                values_num: 0,
728                contains_names: false,
729            }
730        }
731
732        pub fn has_names(&self) -> bool {
733            self.contains_names
734        }
735
736        /// A const empty instance, useful for taking references
737        pub const EMPTY: &'static LegacySerializedValues = &LegacySerializedValues::new();
738
739        /// Serializes value and appends it to the list
740        pub fn add_value(&mut self, val: &impl Value) -> Result<(), SerializeValuesError> {
741            if self.contains_names {
742                return Err(SerializeValuesError::MixingNamedAndNotNamedValues);
743            }
744            if self.values_num == u16::MAX {
745                return Err(SerializeValuesError::TooManyValues);
746            }
747
748            let len_before_serialize: usize = self.serialized_values.len();
749
750            if let Err(e) = val.serialize(&mut self.serialized_values) {
751                self.serialized_values.resize(len_before_serialize, 0);
752                return Err(SerializeValuesError::from(e));
753            }
754
755            self.values_num += 1;
756            Ok(())
757        }
758
759        pub fn add_named_value(
760            &mut self,
761            name: &str,
762            val: &impl Value,
763        ) -> Result<(), SerializeValuesError> {
764            if self.values_num > 0 && !self.contains_names {
765                return Err(SerializeValuesError::MixingNamedAndNotNamedValues);
766            }
767            self.contains_names = true;
768            if self.values_num == u16::MAX {
769                return Err(SerializeValuesError::TooManyValues);
770            }
771
772            let len_before_serialize: usize = self.serialized_values.len();
773
774            types::write_string(name, &mut self.serialized_values)
775                .map_err(|_| SerializeValuesError::ParseError)?;
776
777            if let Err(e) = val.serialize(&mut self.serialized_values) {
778                self.serialized_values.resize(len_before_serialize, 0);
779                return Err(SerializeValuesError::from(e));
780            }
781
782            self.values_num += 1;
783            Ok(())
784        }
785
786        pub fn iter(&self) -> impl Iterator<Item = RawValue> {
787            LegacySerializedValuesIterator {
788                serialized_values: &self.serialized_values,
789                contains_names: self.contains_names,
790            }
791        }
792
793        pub fn write_to_request(&self, buf: &mut impl BufMut) {
794            buf.put_u16(self.values_num);
795            buf.put(&self.serialized_values[..]);
796        }
797
798        pub fn is_empty(&self) -> bool {
799            self.values_num == 0
800        }
801
802        pub fn len(&self) -> u16 {
803            self.values_num
804        }
805
806        pub fn size(&self) -> usize {
807            self.serialized_values.len()
808        }
809
810        pub fn iter_name_value_pairs(&self) -> impl Iterator<Item = (Option<&str>, RawValue)> {
811            let mut buf = &self.serialized_values[..];
812            (0..self.values_num).map(move |_| {
813                // `unwrap()`s here are safe, as we assume type-safety: if `LegacySerializedValues` exits,
814                // we have a guarantee that the layout of the serialized values is valid.
815                let name = self
816                    .contains_names
817                    .then(|| types::read_string(&mut buf).unwrap());
818                let serialized = types::read_value(&mut buf).unwrap();
819                (name, serialized)
820            })
821        }
822    }
823
824    #[derive(Clone, Copy)]
825    pub struct LegacySerializedValuesIterator<'a> {
826        serialized_values: &'a [u8],
827        contains_names: bool,
828    }
829
830    impl<'a> Iterator for LegacySerializedValuesIterator<'a> {
831        type Item = RawValue<'a>;
832
833        fn next(&mut self) -> Option<Self::Item> {
834            if self.serialized_values.is_empty() {
835                return None;
836            }
837
838            // In case of named values, skip names
839            if self.contains_names {
840                types::read_short_bytes(&mut self.serialized_values)
841                    .expect("badly encoded value name");
842            }
843
844            Some(types::read_value(&mut self.serialized_values).expect("badly encoded value"))
845        }
846    }
847
848    /// Represents List of ValueList for Batch statement
849    pub trait LegacyBatchValues {
850        /// For some unknown reason, this type, when not resolved to a concrete type for a given async function,
851        /// cannot live across await boundaries while maintaining the corresponding future `Send`, unless `'r: 'static`
852        ///
853        /// See <https://github.com/scylladb/scylla-rust-driver/issues/599> for more details
854        type LegacyBatchValuesIter<'r>: LegacyBatchValuesIterator<'r>
855        where
856            Self: 'r;
857        fn batch_values_iter(&self) -> Self::LegacyBatchValuesIter<'_>;
858    }
859
860    /// An iterator-like for `ValueList`
861    ///
862    /// An instance of this can be easily obtained from `IT: Iterator<Item: ValueList>`: that would be
863    /// `BatchValuesIteratorFromIterator<IT>`
864    ///
865    /// It's just essentially making methods from `ValueList` accessible instead of being an actual iterator because of
866    /// compiler limitations that would otherwise be very complex to overcome.
867    /// (specifically, types being different would require yielding enums for tuple impls)
868    pub trait LegacyBatchValuesIterator<'a> {
869        fn next_serialized(&mut self) -> Option<SerializedResult<'a>>;
870        fn write_next_to_request(
871            &mut self,
872            buf: &mut impl BufMut,
873        ) -> Option<Result<(), SerializeValuesError>>;
874        fn skip_next(&mut self) -> Option<()>;
875        fn count(mut self) -> usize
876        where
877            Self: Sized,
878        {
879            let mut count = 0;
880            while self.skip_next().is_some() {
881                count += 1;
882            }
883            count
884        }
885    }
886
887    /// Implements `BatchValuesIterator` from an `Iterator` over references to things that implement `ValueList`
888    ///
889    /// Essentially used internally by this lib to provide implementers of `BatchValuesIterator` for cases
890    /// that always serialize the same concrete `ValueList` type
891    pub struct LegacyBatchValuesIteratorFromIterator<IT: Iterator> {
892        it: IT,
893    }
894
895    impl<'r, 'a: 'r, IT, VL> LegacyBatchValuesIterator<'r> for LegacyBatchValuesIteratorFromIterator<IT>
896    where
897        IT: Iterator<Item = &'a VL>,
898        VL: ValueList + 'a,
899    {
900        fn next_serialized(&mut self) -> Option<SerializedResult<'r>> {
901            self.it.next().map(|vl| vl.serialized())
902        }
903        fn write_next_to_request(
904            &mut self,
905            buf: &mut impl BufMut,
906        ) -> Option<Result<(), SerializeValuesError>> {
907            self.it.next().map(|vl| vl.write_to_request(buf))
908        }
909        fn skip_next(&mut self) -> Option<()> {
910            self.it.next().map(|_| ())
911        }
912    }
913
914    impl<IT> From<IT> for LegacyBatchValuesIteratorFromIterator<IT>
915    where
916        IT: Iterator,
917        IT::Item: ValueList,
918    {
919        fn from(it: IT) -> Self {
920            LegacyBatchValuesIteratorFromIterator { it }
921        }
922    }
923
924    //
925    //  Value impls
926    //
927
928    // Implement Value for primitive types
929    impl Value for i8 {
930        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
931            buf.put_i32(1);
932            buf.put_i8(*self);
933            Ok(())
934        }
935    }
936
937    impl Value for i16 {
938        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
939            buf.put_i32(2);
940            buf.put_i16(*self);
941            Ok(())
942        }
943    }
944
945    impl Value for i32 {
946        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
947            buf.put_i32(4);
948            buf.put_i32(*self);
949            Ok(())
950        }
951    }
952
953    impl Value for i64 {
954        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
955            buf.put_i32(8);
956            buf.put_i64(*self);
957            Ok(())
958        }
959    }
960
961    impl Value for CqlDecimal {
962        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
963            let (bytes, scale) = self.as_signed_be_bytes_slice_and_exponent();
964
965            if bytes.len() > (i32::MAX - 4) as usize {
966                return Err(ValueTooBig);
967            }
968            let serialized_len: i32 = bytes.len() as i32 + 4;
969
970            buf.put_i32(serialized_len);
971            buf.put_i32(scale);
972            buf.extend_from_slice(bytes);
973
974            Ok(())
975        }
976    }
977
978    #[cfg(feature = "bigdecimal-04")]
979    impl Value for bigdecimal_04::BigDecimal {
980        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
981            let (value, scale) = self.as_bigint_and_exponent();
982
983            let serialized = value.to_signed_bytes_be();
984
985            if serialized.len() > (i32::MAX - 4) as usize {
986                return Err(ValueTooBig);
987            }
988            let serialized_len: i32 = serialized.len() as i32 + 4;
989
990            buf.put_i32(serialized_len);
991            buf.put_i32(scale.try_into().map_err(|_| ValueTooBig)?);
992            buf.extend_from_slice(&serialized);
993
994            Ok(())
995        }
996    }
997
998    #[cfg(feature = "chrono-04")]
999    impl Value for chrono_04::NaiveDate {
1000        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1001            CqlDate::from(*self).serialize(buf)
1002        }
1003    }
1004
1005    impl Value for CqlDate {
1006        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1007            buf.put_i32(4);
1008            buf.put_u32(self.0);
1009            Ok(())
1010        }
1011    }
1012
1013    #[cfg(feature = "time-03")]
1014    impl Value for time_03::Date {
1015        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1016            CqlDate::from(*self).serialize(buf)
1017        }
1018    }
1019
1020    impl Value for CqlTimestamp {
1021        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1022            buf.put_i32(8);
1023            buf.put_i64(self.0);
1024            Ok(())
1025        }
1026    }
1027
1028    impl Value for CqlTime {
1029        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1030            buf.put_i32(8);
1031            buf.put_i64(self.0);
1032            Ok(())
1033        }
1034    }
1035
1036    #[cfg(feature = "chrono-04")]
1037    impl Value for chrono_04::DateTime<chrono_04::Utc> {
1038        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1039            CqlTimestamp::from(*self).serialize(buf)
1040        }
1041    }
1042
1043    #[cfg(feature = "time-03")]
1044    impl Value for time_03::OffsetDateTime {
1045        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1046            CqlTimestamp::from(*self).serialize(buf)
1047        }
1048    }
1049
1050    #[cfg(feature = "chrono-04")]
1051    impl Value for chrono_04::NaiveTime {
1052        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1053            CqlTime::try_from(*self)
1054                .map_err(|_| ValueTooBig)?
1055                .serialize(buf)
1056        }
1057    }
1058
1059    #[cfg(feature = "time-03")]
1060    impl Value for time_03::Time {
1061        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1062            CqlTime::from(*self).serialize(buf)
1063        }
1064    }
1065
1066    #[cfg(feature = "secrecy-08")]
1067    impl<V: Value + secrecy_08::Zeroize> Value for secrecy_08::Secret<V> {
1068        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1069            use secrecy_08::ExposeSecret;
1070            self.expose_secret().serialize(buf)
1071        }
1072    }
1073
1074    impl Value for bool {
1075        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1076            buf.put_i32(1);
1077            let false_bytes: &[u8] = &[0x00];
1078            let true_bytes: &[u8] = &[0x01];
1079            if *self {
1080                buf.put(true_bytes);
1081            } else {
1082                buf.put(false_bytes);
1083            }
1084
1085            Ok(())
1086        }
1087    }
1088
1089    impl Value for f32 {
1090        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1091            buf.put_i32(4);
1092            buf.put_f32(*self);
1093            Ok(())
1094        }
1095    }
1096
1097    impl Value for f64 {
1098        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1099            buf.put_i32(8);
1100            buf.put_f64(*self);
1101            Ok(())
1102        }
1103    }
1104
1105    impl Value for Uuid {
1106        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1107            buf.put_i32(16);
1108            buf.extend_from_slice(self.as_bytes());
1109            Ok(())
1110        }
1111    }
1112
1113    impl Value for CqlTimeuuid {
1114        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1115            self.0.serialize(buf)
1116        }
1117    }
1118
1119    impl Value for CqlVarint {
1120        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1121            let serialized = &self.0;
1122            let serialized_len: i32 = serialized.len().try_into().map_err(|_| ValueTooBig)?;
1123
1124            buf.put_i32(serialized_len);
1125            buf.extend_from_slice(serialized);
1126
1127            Ok(())
1128        }
1129    }
1130
1131    #[cfg(feature = "num-bigint-03")]
1132    impl Value for num_bigint_03::BigInt {
1133        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1134            let serialized = self.to_signed_bytes_be();
1135            let serialized_len: i32 = serialized.len().try_into().map_err(|_| ValueTooBig)?;
1136
1137            buf.put_i32(serialized_len);
1138            buf.extend_from_slice(&serialized);
1139
1140            Ok(())
1141        }
1142    }
1143
1144    #[cfg(feature = "num-bigint-04")]
1145    impl Value for num_bigint_04::BigInt {
1146        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1147            let serialized = self.to_signed_bytes_be();
1148            let serialized_len: i32 = serialized.len().try_into().map_err(|_| ValueTooBig)?;
1149
1150            buf.put_i32(serialized_len);
1151            buf.extend_from_slice(&serialized);
1152
1153            Ok(())
1154        }
1155    }
1156
1157    impl Value for &str {
1158        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1159            let str_bytes: &[u8] = self.as_bytes();
1160            let val_len: i32 = str_bytes.len().try_into().map_err(|_| ValueTooBig)?;
1161
1162            buf.put_i32(val_len);
1163            buf.put(str_bytes);
1164
1165            Ok(())
1166        }
1167    }
1168
1169    impl Value for Vec<u8> {
1170        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1171            <&[u8] as Value>::serialize(&self.as_slice(), buf)
1172        }
1173    }
1174
1175    impl Value for &[u8] {
1176        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1177            let val_len: i32 = self.len().try_into().map_err(|_| ValueTooBig)?;
1178            buf.put_i32(val_len);
1179
1180            buf.extend_from_slice(self);
1181
1182            Ok(())
1183        }
1184    }
1185
1186    impl<const N: usize> Value for [u8; N] {
1187        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1188            let val_len: i32 = self.len().try_into().map_err(|_| ValueTooBig)?;
1189            buf.put_i32(val_len);
1190
1191            buf.extend_from_slice(self);
1192
1193            Ok(())
1194        }
1195    }
1196
1197    impl Value for IpAddr {
1198        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1199            match self {
1200                IpAddr::V4(addr) => {
1201                    buf.put_i32(4);
1202                    buf.extend_from_slice(&addr.octets());
1203                }
1204                IpAddr::V6(addr) => {
1205                    buf.put_i32(16);
1206                    buf.extend_from_slice(&addr.octets());
1207                }
1208            }
1209
1210            Ok(())
1211        }
1212    }
1213
1214    impl Value for String {
1215        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1216            <&str as Value>::serialize(&self.as_str(), buf)
1217        }
1218    }
1219
1220    /// Every `Option<T>` can be serialized as None -> NULL, Some(val) -> val.serialize()
1221    impl<T: Value> Value for Option<T> {
1222        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1223            match self {
1224                Some(val) => <T as Value>::serialize(val, buf),
1225                None => {
1226                    buf.put_i32(-1);
1227                    Ok(())
1228                }
1229            }
1230        }
1231    }
1232
1233    impl Value for Unset {
1234        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1235            // Unset serializes itself to empty value with length = -2
1236            buf.put_i32(-2);
1237            Ok(())
1238        }
1239    }
1240
1241    impl Value for Counter {
1242        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1243            self.0.serialize(buf)
1244        }
1245    }
1246
1247    impl Value for CqlDuration {
1248        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1249            let bytes_num_pos: usize = buf.len();
1250            buf.put_i32(0);
1251
1252            vint_encode(self.months as i64, buf);
1253            vint_encode(self.days as i64, buf);
1254            vint_encode(self.nanoseconds, buf);
1255
1256            let written_bytes: usize = buf.len() - bytes_num_pos - 4;
1257            let written_bytes_i32: i32 = written_bytes.try_into().map_err(|_| ValueTooBig)?;
1258            buf[bytes_num_pos..(bytes_num_pos + 4)]
1259                .copy_from_slice(&written_bytes_i32.to_be_bytes());
1260
1261            Ok(())
1262        }
1263    }
1264
1265    impl<V: Value> Value for MaybeUnset<V> {
1266        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1267            match self {
1268                MaybeUnset::Set(v) => v.serialize(buf),
1269                MaybeUnset::Unset => Unset.serialize(buf),
1270            }
1271        }
1272    }
1273
1274    // Every &impl Value and &dyn Value should also implement Value
1275    impl<T: Value + ?Sized> Value for &T {
1276        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1277            <T as Value>::serialize(*self, buf)
1278        }
1279    }
1280
1281    // Every Boxed Value should also implement Value
1282    impl<T: Value + ?Sized> Value for Box<T> {
1283        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1284            <T as Value>::serialize(self.as_ref(), buf)
1285        }
1286    }
1287
1288    fn serialize_map<K: Value, V: Value>(
1289        kv_iter: impl Iterator<Item = (K, V)>,
1290        kv_count: usize,
1291        buf: &mut Vec<u8>,
1292    ) -> Result<(), ValueTooBig> {
1293        let bytes_num_pos: usize = buf.len();
1294        buf.put_i32(0);
1295
1296        buf.put_i32(kv_count.try_into().map_err(|_| ValueTooBig)?);
1297        for (key, value) in kv_iter {
1298            <K as Value>::serialize(&key, buf)?;
1299            <V as Value>::serialize(&value, buf)?;
1300        }
1301
1302        let written_bytes: usize = buf.len() - bytes_num_pos - 4;
1303        let written_bytes_i32: i32 = written_bytes.try_into().map_err(|_| ValueTooBig)?;
1304        buf[bytes_num_pos..(bytes_num_pos + 4)].copy_from_slice(&written_bytes_i32.to_be_bytes());
1305
1306        Ok(())
1307    }
1308
1309    fn serialize_list_or_set<'a, V: 'a + Value>(
1310        elements_iter: impl Iterator<Item = &'a V>,
1311        element_count: usize,
1312        buf: &mut Vec<u8>,
1313    ) -> Result<(), ValueTooBig> {
1314        let bytes_num_pos: usize = buf.len();
1315        buf.put_i32(0);
1316
1317        buf.put_i32(element_count.try_into().map_err(|_| ValueTooBig)?);
1318        for value in elements_iter {
1319            <V as Value>::serialize(value, buf)?;
1320        }
1321
1322        let written_bytes: usize = buf.len() - bytes_num_pos - 4;
1323        let written_bytes_i32: i32 = written_bytes.try_into().map_err(|_| ValueTooBig)?;
1324        buf[bytes_num_pos..(bytes_num_pos + 4)].copy_from_slice(&written_bytes_i32.to_be_bytes());
1325
1326        Ok(())
1327    }
1328
1329    impl<V: Value, S: BuildHasher + Default> Value for HashSet<V, S> {
1330        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1331            serialize_list_or_set(self.iter(), self.len(), buf)
1332        }
1333    }
1334
1335    impl<K: Value, V: Value, S: BuildHasher> Value for HashMap<K, V, S> {
1336        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1337            serialize_map(self.iter(), self.len(), buf)
1338        }
1339    }
1340
1341    impl<V: Value> Value for BTreeSet<V> {
1342        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1343            serialize_list_or_set(self.iter(), self.len(), buf)
1344        }
1345    }
1346
1347    impl<K: Value, V: Value> Value for BTreeMap<K, V> {
1348        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1349            serialize_map(self.iter(), self.len(), buf)
1350        }
1351    }
1352
1353    impl<T: Value> Value for Vec<T> {
1354        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1355            serialize_list_or_set(self.iter(), self.len(), buf)
1356        }
1357    }
1358
1359    impl<T: Value> Value for &[T] {
1360        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1361            serialize_list_or_set(self.iter(), self.len(), buf)
1362        }
1363    }
1364
1365    fn serialize_tuple<V: Value>(
1366        elem_iter: impl Iterator<Item = V>,
1367        buf: &mut Vec<u8>,
1368    ) -> Result<(), ValueTooBig> {
1369        let bytes_num_pos: usize = buf.len();
1370        buf.put_i32(0);
1371
1372        for elem in elem_iter {
1373            elem.serialize(buf)?;
1374        }
1375
1376        let written_bytes: usize = buf.len() - bytes_num_pos - 4;
1377        let written_bytes_i32: i32 = written_bytes.try_into().map_err(|_| ValueTooBig)?;
1378        buf[bytes_num_pos..(bytes_num_pos + 4)].copy_from_slice(&written_bytes_i32.to_be_bytes());
1379
1380        Ok(())
1381    }
1382
1383    fn serialize_empty(buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1384        buf.put_i32(0);
1385        Ok(())
1386    }
1387
1388    impl Value for CqlValue {
1389        fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1390            match self {
1391                CqlValue::Map(m) => serialize_map(m.iter().map(|p| (&p.0, &p.1)), m.len(), buf),
1392                CqlValue::Tuple(t) => serialize_tuple(t.iter(), buf),
1393
1394                // A UDT value is composed of successive [bytes] values, one for each field of the UDT
1395                // value (in the order defined by the type), so they serialize in a same way tuples do.
1396                CqlValue::UserDefinedType { fields, .. } => {
1397                    serialize_tuple(fields.iter().map(|(_, value)| value), buf)
1398                }
1399
1400                CqlValue::Date(d) => d.serialize(buf),
1401                CqlValue::Duration(d) => d.serialize(buf),
1402                CqlValue::Timestamp(t) => t.serialize(buf),
1403                CqlValue::Time(t) => t.serialize(buf),
1404
1405                CqlValue::Ascii(s) | CqlValue::Text(s) => s.serialize(buf),
1406                CqlValue::List(v) | CqlValue::Set(v) => v.serialize(buf),
1407
1408                CqlValue::Blob(b) => b.serialize(buf),
1409                CqlValue::Boolean(b) => b.serialize(buf),
1410                CqlValue::Counter(c) => c.serialize(buf),
1411                CqlValue::Decimal(d) => d.serialize(buf),
1412                CqlValue::Double(d) => d.serialize(buf),
1413                CqlValue::Float(f) => f.serialize(buf),
1414                CqlValue::Int(i) => i.serialize(buf),
1415                CqlValue::BigInt(i) => i.serialize(buf),
1416                CqlValue::Inet(i) => i.serialize(buf),
1417                CqlValue::SmallInt(s) => s.serialize(buf),
1418                CqlValue::TinyInt(t) => t.serialize(buf),
1419                CqlValue::Timeuuid(t) => t.serialize(buf),
1420                CqlValue::Uuid(u) => u.serialize(buf),
1421                CqlValue::Varint(v) => v.serialize(buf),
1422
1423                CqlValue::Empty => serialize_empty(buf),
1424            }
1425        }
1426    }
1427
1428    macro_rules! impl_value_for_tuple {
1429    ( $($Ti:ident),* ; $($FieldI:tt),* ) => {
1430    impl<$($Ti),+> Value for ($($Ti,)+)
1431        where
1432            $($Ti: Value),+
1433        {
1434            fn serialize(&self, buf: &mut Vec<u8>) -> Result<(), ValueTooBig> {
1435                let bytes_num_pos: usize = buf.len();
1436                buf.put_i32(0);
1437                $(
1438                    <$Ti as Value>::serialize(&self.$FieldI, buf)?;
1439                )*
1440
1441                let written_bytes: usize = buf.len() - bytes_num_pos - 4;
1442                let written_bytes_i32: i32 = written_bytes.try_into().map_err(|_| ValueTooBig) ?;
1443                buf[bytes_num_pos..(bytes_num_pos+4)].copy_from_slice(&written_bytes_i32.to_be_bytes());
1444
1445                Ok(())
1446            }
1447        }
1448    }
1449}
1450
1451    impl_value_for_tuple!(T0; 0);
1452    impl_value_for_tuple!(T0, T1; 0, 1);
1453    impl_value_for_tuple!(T0, T1, T2; 0, 1, 2);
1454    impl_value_for_tuple!(T0, T1, T2, T3; 0, 1, 2, 3);
1455    impl_value_for_tuple!(T0, T1, T2, T3, T4; 0, 1, 2, 3, 4);
1456    impl_value_for_tuple!(T0, T1, T2, T3, T4, T5; 0, 1, 2, 3, 4, 5);
1457    impl_value_for_tuple!(T0, T1, T2, T3, T4, T5, T6; 0, 1, 2, 3, 4, 5, 6);
1458    impl_value_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7; 0, 1, 2, 3, 4, 5, 6, 7);
1459    impl_value_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8; 0, 1, 2, 3, 4, 5, 6, 7, 8);
1460    impl_value_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9;
1461                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
1462    impl_value_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10;
1463                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1464    impl_value_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11;
1465                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
1466    impl_value_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12;
1467                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
1468    impl_value_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13;
1469                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
1470    impl_value_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14;
1471                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
1472    impl_value_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15;
1473                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
1474
1475    //
1476    //  ValueList impls
1477    //
1478
1479    // Implement ValueList for the unit type
1480    impl ValueList for () {
1481        fn serialized(&self) -> SerializedResult<'_> {
1482            Ok(Cow::Owned(LegacySerializedValues::new()))
1483        }
1484    }
1485
1486    // Implement ValueList for &[] - u8 because otherwise rust can't infer type
1487    impl ValueList for [u8; 0] {
1488        fn serialized(&self) -> SerializedResult<'_> {
1489            Ok(Cow::Owned(LegacySerializedValues::new()))
1490        }
1491    }
1492
1493    // Implement ValueList for slices of Value types
1494    impl<T: Value> ValueList for &[T] {
1495        fn serialized(&self) -> SerializedResult<'_> {
1496            let size = std::mem::size_of_val(*self);
1497            let mut result = LegacySerializedValues::with_capacity(size);
1498            for val in *self {
1499                result.add_value(val)?;
1500            }
1501
1502            Ok(Cow::Owned(result))
1503        }
1504    }
1505
1506    // Implement ValueList for Vec<Value>
1507    impl<T: Value> ValueList for Vec<T> {
1508        fn serialized(&self) -> SerializedResult<'_> {
1509            let slice = self.as_slice();
1510            let size = std::mem::size_of_val(slice);
1511            let mut result = LegacySerializedValues::with_capacity(size);
1512            for val in self {
1513                result.add_value(val)?;
1514            }
1515
1516            Ok(Cow::Owned(result))
1517        }
1518    }
1519
1520    // Implement ValueList for maps, which serializes named values
1521    macro_rules! impl_value_list_for_btree_map {
1522        ($key_type:ty) => {
1523            impl<T: Value> ValueList for BTreeMap<$key_type, T> {
1524                fn serialized(&self) -> SerializedResult<'_> {
1525                    let mut result = LegacySerializedValues::with_capacity(self.len());
1526                    for (key, val) in self {
1527                        result.add_named_value(key, val)?;
1528                    }
1529
1530                    Ok(Cow::Owned(result))
1531                }
1532            }
1533        };
1534    }
1535
1536    // Implement ValueList for maps, which serializes named values
1537    macro_rules! impl_value_list_for_hash_map {
1538        ($key_type:ty) => {
1539            impl<T: Value, S: BuildHasher> ValueList for HashMap<$key_type, T, S> {
1540                fn serialized(&self) -> SerializedResult<'_> {
1541                    let mut result = LegacySerializedValues::with_capacity(self.len());
1542                    for (key, val) in self {
1543                        result.add_named_value(key, val)?;
1544                    }
1545
1546                    Ok(Cow::Owned(result))
1547                }
1548            }
1549        };
1550    }
1551
1552    impl_value_list_for_hash_map!(String);
1553    impl_value_list_for_hash_map!(&str);
1554    impl_value_list_for_btree_map!(String);
1555    impl_value_list_for_btree_map!(&str);
1556
1557    // Implement ValueList for tuples of Values of size up to 16
1558
1559    // Here is an example implementation for (T0, )
1560    // Further variants are done using a macro
1561    impl<T0: Value> ValueList for (T0,) {
1562        fn serialized(&self) -> SerializedResult<'_> {
1563            let size = std::mem::size_of_val(self);
1564            let mut result = LegacySerializedValues::with_capacity(size);
1565            result.add_value(&self.0)?;
1566            Ok(Cow::Owned(result))
1567        }
1568    }
1569
1570    macro_rules! impl_value_list_for_tuple {
1571    ( $($Ti:ident),* ; $($FieldI:tt),*) => {
1572        impl<$($Ti),+> ValueList for ($($Ti,)+)
1573        where
1574            $($Ti: Value),+
1575        {
1576            fn serialized(&self) -> SerializedResult<'_> {
1577                let size = std::mem::size_of_val(self);
1578                let mut result = LegacySerializedValues::with_capacity(size);
1579                $(
1580                    result.add_value(&self.$FieldI) ?;
1581                )*
1582                Ok(Cow::Owned(result))
1583            }
1584        }
1585    }
1586}
1587
1588    impl_value_list_for_tuple!(T0, T1; 0, 1);
1589    impl_value_list_for_tuple!(T0, T1, T2; 0, 1, 2);
1590    impl_value_list_for_tuple!(T0, T1, T2, T3; 0, 1, 2, 3);
1591    impl_value_list_for_tuple!(T0, T1, T2, T3, T4; 0, 1, 2, 3, 4);
1592    impl_value_list_for_tuple!(T0, T1, T2, T3, T4, T5; 0, 1, 2, 3, 4, 5);
1593    impl_value_list_for_tuple!(T0, T1, T2, T3, T4, T5, T6; 0, 1, 2, 3, 4, 5, 6);
1594    impl_value_list_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7; 0, 1, 2, 3, 4, 5, 6, 7);
1595    impl_value_list_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8; 0, 1, 2, 3, 4, 5, 6, 7, 8);
1596    impl_value_list_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
1597    impl_value_list_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10;
1598                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1599    impl_value_list_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11;
1600                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
1601    impl_value_list_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12;
1602                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
1603    impl_value_list_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13;
1604                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
1605    impl_value_list_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14;
1606                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
1607    impl_value_list_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15;
1608                           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
1609
1610    // Every &impl ValueList should also implement ValueList
1611    impl<T: ValueList> ValueList for &T {
1612        fn serialized(&self) -> SerializedResult<'_> {
1613            <T as ValueList>::serialized(*self)
1614        }
1615    }
1616
1617    impl ValueList for LegacySerializedValues {
1618        fn serialized(&self) -> SerializedResult<'_> {
1619            Ok(Cow::Borrowed(self))
1620        }
1621    }
1622
1623    impl ValueList for Cow<'_, LegacySerializedValues> {
1624        fn serialized(&self) -> SerializedResult<'_> {
1625            Ok(Cow::Borrowed(self.as_ref()))
1626        }
1627    }
1628
1629    //
1630    // BatchValues impls
1631    //
1632
1633    /// Implements `BatchValues` from an `Iterator` over references to things that implement `ValueList`
1634    ///
1635    /// This is to avoid requiring allocating a new `Vec` containing all the `ValueList`s directly:
1636    /// with this, one can write:
1637    /// `session.batch(&batch, BatchValuesFromIterator::from(lines_to_insert.iter().map(|l| &l.value_list)))`
1638    /// where `lines_to_insert` may also contain e.g. data to pick the statement...
1639    ///
1640    /// The underlying iterator will always be cloned at least once, once to compute the length if it can't be known
1641    /// in advance, and be re-cloned at every retry.
1642    /// It is consequently expected that the provided iterator is cheap to clone (e.g. `slice.iter().map(...)`).
1643    pub struct LegacyBatchValuesFromIter<'a, IT> {
1644        it: IT,
1645        _spooky: std::marker::PhantomData<&'a ()>,
1646    }
1647
1648    impl<'a, IT, VL> LegacyBatchValuesFromIter<'a, IT>
1649    where
1650        IT: Iterator<Item = &'a VL> + Clone,
1651        VL: ValueList + 'a,
1652    {
1653        pub fn new(into_iter: impl IntoIterator<IntoIter = IT>) -> Self {
1654            Self {
1655                it: into_iter.into_iter(),
1656                _spooky: std::marker::PhantomData,
1657            }
1658        }
1659    }
1660
1661    impl<'a, IT, VL> From<IT> for LegacyBatchValuesFromIter<'a, IT>
1662    where
1663        IT: Iterator<Item = &'a VL> + Clone,
1664        VL: ValueList + 'a,
1665    {
1666        fn from(it: IT) -> Self {
1667            Self::new(it)
1668        }
1669    }
1670
1671    impl<'a, IT, VL> LegacyBatchValues for LegacyBatchValuesFromIter<'a, IT>
1672    where
1673        IT: Iterator<Item = &'a VL> + Clone,
1674        VL: ValueList + 'a,
1675    {
1676        type LegacyBatchValuesIter<'r>
1677            = LegacyBatchValuesIteratorFromIterator<IT>
1678        where
1679            Self: 'r;
1680        fn batch_values_iter(&self) -> Self::LegacyBatchValuesIter<'_> {
1681            self.it.clone().into()
1682        }
1683    }
1684
1685    // Implement BatchValues for slices of ValueList types
1686    impl<T: ValueList> LegacyBatchValues for [T] {
1687        type LegacyBatchValuesIter<'r>
1688            = LegacyBatchValuesIteratorFromIterator<std::slice::Iter<'r, T>>
1689        where
1690            Self: 'r;
1691        fn batch_values_iter(&self) -> Self::LegacyBatchValuesIter<'_> {
1692            self.iter().into()
1693        }
1694    }
1695
1696    // Implement BatchValues for Vec<ValueList>
1697    impl<T: ValueList> LegacyBatchValues for Vec<T> {
1698        type LegacyBatchValuesIter<'r>
1699            = LegacyBatchValuesIteratorFromIterator<std::slice::Iter<'r, T>>
1700        where
1701            Self: 'r;
1702        fn batch_values_iter(&self) -> Self::LegacyBatchValuesIter<'_> {
1703            LegacyBatchValues::batch_values_iter(self.as_slice())
1704        }
1705    }
1706
1707    // Here is an example implementation for (T0, )
1708    // Further variants are done using a macro
1709    impl<T0: ValueList> LegacyBatchValues for (T0,) {
1710        type LegacyBatchValuesIter<'r>
1711            = LegacyBatchValuesIteratorFromIterator<std::iter::Once<&'r T0>>
1712        where
1713            Self: 'r;
1714        fn batch_values_iter(&self) -> Self::LegacyBatchValuesIter<'_> {
1715            std::iter::once(&self.0).into()
1716        }
1717    }
1718
1719    pub struct TupleValuesIter<'a, T> {
1720        tuple: &'a T,
1721        idx: usize,
1722    }
1723
1724    macro_rules! impl_batch_values_for_tuple {
1725    ( $($Ti:ident),* ; $($FieldI:tt),* ; $TupleSize:tt) => {
1726        impl<$($Ti),+> LegacyBatchValues for ($($Ti,)+)
1727        where
1728            $($Ti: ValueList),+
1729        {
1730            type LegacyBatchValuesIter<'r> = TupleValuesIter<'r, ($($Ti,)+)> where Self: 'r;
1731            fn batch_values_iter(&self) -> Self::LegacyBatchValuesIter<'_> {
1732                TupleValuesIter {
1733                    tuple: self,
1734                    idx: 0,
1735                }
1736            }
1737        }
1738        impl<'r, $($Ti),+> LegacyBatchValuesIterator<'r> for TupleValuesIter<'r, ($($Ti,)+)>
1739        where
1740            $($Ti: ValueList),+
1741        {
1742            fn next_serialized(&mut self) -> Option<SerializedResult<'r>> {
1743                let serialized_value_res = match self.idx {
1744                    $(
1745                        $FieldI => self.tuple.$FieldI.serialized(),
1746                    )*
1747                    _ => return None,
1748                };
1749                self.idx += 1;
1750                Some(serialized_value_res)
1751            }
1752            fn write_next_to_request(
1753                &mut self,
1754                buf: &mut impl BufMut,
1755            ) -> Option<Result<(), SerializeValuesError>> {
1756                let ret = match self.idx {
1757                    $(
1758                        $FieldI => self.tuple.$FieldI.write_to_request(buf),
1759                    )*
1760                    _ => return None,
1761                };
1762                self.idx += 1;
1763                Some(ret)
1764            }
1765            fn skip_next(&mut self) -> Option<()> {
1766                if self.idx < $TupleSize {
1767                    self.idx += 1;
1768                    Some(())
1769                } else {
1770                    None
1771                }
1772            }
1773        }
1774    }
1775}
1776
1777    impl_batch_values_for_tuple!(T0, T1; 0, 1; 2);
1778    impl_batch_values_for_tuple!(T0, T1, T2; 0, 1, 2; 3);
1779    impl_batch_values_for_tuple!(T0, T1, T2, T3; 0, 1, 2, 3; 4);
1780    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4; 0, 1, 2, 3, 4; 5);
1781    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4, T5; 0, 1, 2, 3, 4, 5; 6);
1782    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4, T5, T6; 0, 1, 2, 3, 4, 5, 6; 7);
1783    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7; 0, 1, 2, 3, 4, 5, 6, 7; 8);
1784    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8; 0, 1, 2, 3, 4, 5, 6, 7, 8; 9);
1785    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9;
1786                             0, 1, 2, 3, 4, 5, 6, 7, 8, 9; 10);
1787    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10;
1788                             0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10; 11);
1789    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11;
1790                             0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11; 12);
1791    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12;
1792                             0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12; 13);
1793    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13;
1794                             0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13; 14);
1795    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14;
1796                             0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14; 15);
1797    impl_batch_values_for_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15;
1798                             0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15; 16);
1799
1800    // Every &impl BatchValues should also implement BatchValues
1801    impl<T: LegacyBatchValues + ?Sized> LegacyBatchValues for &T {
1802        type LegacyBatchValuesIter<'r>
1803            = <T as LegacyBatchValues>::LegacyBatchValuesIter<'r>
1804        where
1805            Self: 'r;
1806        fn batch_values_iter(&self) -> Self::LegacyBatchValuesIter<'_> {
1807            <T as LegacyBatchValues>::batch_values_iter(*self)
1808        }
1809    }
1810
1811    /// Allows reusing already-serialized first value
1812    ///
1813    /// We'll need to build a `LegacySerializedValues` for the first ~`ValueList` of a batch to figure out the shard (#448).
1814    /// Once that is done, we can use that instead of re-serializing.
1815    ///
1816    /// This struct implements both `BatchValues` and `BatchValuesIterator` for that purpose
1817    pub struct LegacyBatchValuesFirstSerialized<'f, T> {
1818        first: Option<&'f LegacySerializedValues>,
1819        rest: T,
1820    }
1821
1822    impl<'f, T: LegacyBatchValues> LegacyBatchValuesFirstSerialized<'f, T> {
1823        pub fn new(
1824            batch_values: T,
1825            already_serialized_first: Option<&'f LegacySerializedValues>,
1826        ) -> Self {
1827            Self {
1828                first: already_serialized_first,
1829                rest: batch_values,
1830            }
1831        }
1832    }
1833
1834    impl<'f, BV: LegacyBatchValues> LegacyBatchValues for LegacyBatchValuesFirstSerialized<'f, BV> {
1835        type LegacyBatchValuesIter<'r>
1836            = LegacyBatchValuesFirstSerialized<
1837            'f,
1838            <BV as LegacyBatchValues>::LegacyBatchValuesIter<'r>,
1839        >
1840        where
1841            Self: 'r;
1842        fn batch_values_iter(&self) -> Self::LegacyBatchValuesIter<'_> {
1843            LegacyBatchValuesFirstSerialized {
1844                first: self.first,
1845                rest: self.rest.batch_values_iter(),
1846            }
1847        }
1848    }
1849
1850    impl<'a, 'f: 'a, IT: LegacyBatchValuesIterator<'a>> LegacyBatchValuesIterator<'a>
1851        for LegacyBatchValuesFirstSerialized<'f, IT>
1852    {
1853        fn next_serialized(&mut self) -> Option<SerializedResult<'a>> {
1854            match self.first.take() {
1855                Some(first) => {
1856                    self.rest.skip_next();
1857                    Some(Ok(Cow::Borrowed(first)))
1858                }
1859                None => self.rest.next_serialized(),
1860            }
1861        }
1862        fn write_next_to_request(
1863            &mut self,
1864            buf: &mut impl BufMut,
1865        ) -> Option<Result<(), SerializeValuesError>> {
1866            match self.first.take() {
1867                Some(first) => {
1868                    self.rest.skip_next();
1869                    first.write_to_request(buf);
1870                    Some(Ok(()))
1871                }
1872                None => self.rest.write_next_to_request(buf),
1873            }
1874        }
1875        fn skip_next(&mut self) -> Option<()> {
1876            self.rest.skip_next();
1877            self.first.take().map(|_| ())
1878        }
1879    }
1880}
1881#[allow(deprecated)]
1882pub use legacy::{
1883    LegacyBatchValues, LegacyBatchValuesFirstSerialized, LegacyBatchValuesFromIter,
1884    LegacyBatchValuesIterator, LegacyBatchValuesIteratorFromIterator, LegacySerializedValues,
1885    LegacySerializedValuesIterator, SerializeValuesError, SerializedResult, TupleValuesIter, Value,
1886    ValueList, ValueTooBig,
1887};