Skip to content

Commit 724b4d9

Browse files
committed
tweak array constructor
1 parent 1007d7f commit 724b4d9

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

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)