Skip to content

Commit bf5b7dc

Browse files
committed
tweak array constructor
1 parent 1007d7f commit bf5b7dc

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/value/hashable_value.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,22 +322,14 @@ mod tests {
322322
#[cfg(feature = "postgres-array")]
323323
#[test]
324324
fn test_hash_value_array() {
325-
use crate::ArrayType;
326-
327325
assert_eq!(
328326
Into::<Value>::into(vec![0i32, 1, 2]),
329-
Value::array(
330-
ArrayType::Int,
331-
vec![Value::int(0), Value::int(1), Value::int(2)]
332-
)
327+
Value::array(vec![0i32, 1, 2])
333328
);
334329

335330
assert_eq!(
336331
Into::<Value>::into(vec![0f32, 1.0, 2.0]),
337-
Value::array(
338-
ArrayType::Float,
339-
vec![Value::float(0f32), Value::float(1.0), Value::float(2.0)]
340-
)
332+
Value::array(vec![0f32, 1.0, 2.0])
341333
);
342334

343335
let hash_set: std::collections::HashSet<Value> = [

src/value/tests.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,21 @@ fn test_decimal_value() {
410410
fn test_array_value() {
411411
let array = vec![1, 2, 3, 4, 5];
412412
let v: Value = array.into();
413+
assert_eq!(v, Value::array(vec![1, 2, 3, 4, 5]));
413414
let out: Vec<i32> = v.unwrap();
414415
assert_eq!(out, vec![1, 2, 3, 4, 5]);
416+
417+
let array = vec!["1".to_owned(), "2".to_owned()];
418+
let v: Value = array.clone().into();
419+
assert_eq!(v, Value::array(array));
415420
}
416421

417422
#[test]
418423
#[cfg(feature = "postgres-array")]
419424
fn test_option_array_value() {
420-
let v: Value = Value::array(ArrayType::Int, None);
425+
let v: Value = Option::<Vec<i32>>::None.into();
426+
assert_eq!(v, Value::array_null::<i32>());
427+
assert!(matches!(v, Value::Array(_, _)));
421428
let out: Option<Vec<i32>> = v.unwrap();
422429
assert_eq!(out, None);
423430
}

src/value/with_array.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ where
8181
T: Into<Value> + NotU8 + ValueType,
8282
{
8383
fn from(x: Vec<T>) -> Value {
84-
Value::array(
85-
T::array_type(),
86-
x.into_iter().map(|e| e.into()).collect::<Vec<_>>(),
87-
)
84+
Value::array(x)
8885
}
8986
}
9087

@@ -93,7 +90,7 @@ where
9390
T: Into<Value> + NotU8 + ValueType,
9491
{
9592
fn null() -> Value {
96-
Value::array(T::array_type(), None)
93+
Value::array_null::<T>()
9794
}
9895
}
9996

@@ -126,8 +123,22 @@ where
126123

127124
impl Value {
128125
#[inline]
129-
pub fn array<T: Into<Option<Vec<Value>>>>(array_type: ArrayType, value: T) -> Self {
130-
Self::Array(array_type, value.into().map(Box::new))
126+
pub fn array<T: Into<Value> + NotU8 + ValueType, I: IntoIterator<Item = T>>(values: I) -> Self {
127+
Self::Array(
128+
T::array_type(),
129+
Some(
130+
values
131+
.into_iter()
132+
.map(|v| v.into())
133+
.collect::<Vec<_>>()
134+
.into(),
135+
),
136+
)
137+
}
138+
139+
#[inline]
140+
pub fn array_null<T: Into<Value> + NotU8 + ValueType>() -> Self {
141+
Self::Array(T::array_type(), None)
131142
}
132143

133144
pub fn is_array(&self) -> bool {

0 commit comments

Comments
 (0)