frame/response: add impl CqlFromValue for HashSet
Adds support of CqlFromValue for HashSet.
@@ -4,6 +4,7 @@ use bigdecimal::BigDecimal;
use chrono::{Duration, NaiveDate};
use num_bigint::BigInt;
use std::collections::HashMap;
+use std::collections::HashSet;
use std::hash::Hash;
use std::net::IpAddr;
use thiserror::Error;
@@ -115,6 +116,17 @@ impl<T1: FromCqlVal<CqlValue> + Eq + Hash, T2: FromCqlVal<CqlValue>> FromCqlVal<
}
}
+impl<T: FromCqlVal<CqlValue> + Eq + Hash> FromCqlVal<CqlValue> for HashSet<T> {
+ fn from_cql(cql_val: CqlValue) -> Result<Self, FromCqlValError> {
+ cql_val
+ .into_vec()
+ .ok_or(FromCqlValError::BadCqlType)?
+ .into_iter()
+ .map(T::from_cql)
+ .collect::<Result<HashSet<T>, FromCqlValError>>()
+ }
+}
+
macro_rules! replace_expr {
($_t:tt $sub:expr) => {
$sub
@@ -232,6 +244,7 @@ mod tests {
use bigdecimal::BigDecimal;
use chrono::{Duration, NaiveDate};
use num_bigint::{BigInt, ToBigInt};
+ use std::collections::HashSet;
use std::net::{IpAddr, Ipv4Addr};
use std::str::FromStr;
use uuid::Uuid;
@@ -375,6 +388,26 @@ mod tests {
assert_eq!(Ok(vec![1, 2, 3]), Vec::<i32>::from_cql(cql_val));
}
+ #[test]
+ fn set_from_cql() {
+ let cql_val = CqlValue::Set(vec![
+ CqlValue::Int(1),
+ CqlValue::Int(2),
+ CqlValue::Int(3),
+ CqlValue::Int(1),
+ CqlValue::Int(2),
+ CqlValue::Int(3),
+ ]);
+ assert_eq!(
+ Ok(vec![1, 2, 3]),
+ HashSet::<i32>::from_cql(cql_val).map(|value| {
+ let mut values = value.into_iter().collect::<Vec<_>>();
+ values.sort_unstable();
+ values
+ })
+ );
+ }
+
#[test]
fn tuple_from_row() {
let row = Row {
