Skip to content

Commit df21f42

Browse files
authored
feature: Add TryFrom impls for owned scalars to values (#4234)
Signed-off-by: Adam Gutglick <[email protected]>
1 parent 155abab commit df21f42

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

vortex-scalar/src/list.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ impl<'a> TryFrom<&'a Scalar> for ListScalar<'a> {
214214
}
215215
}
216216

217-
impl<'a, T: for<'b> TryFrom<&'b Scalar, Error = VortexError>> TryFrom<&'a Scalar> for Vec<T> {
217+
impl<'a, T> TryFrom<&'a Scalar> for Vec<T>
218+
where
219+
T: for<'b> TryFrom<&'b Scalar, Error = VortexError>,
220+
{
218221
type Error = VortexError;
219222

220223
fn try_from(value: &'a Scalar) -> Result<Self, Self::Error> {
@@ -230,6 +233,25 @@ impl<'a, T: for<'b> TryFrom<&'b Scalar, Error = VortexError>> TryFrom<&'a Scalar
230233
}
231234
}
232235

236+
impl<T> TryFrom<Scalar> for Vec<T>
237+
where
238+
T: TryFrom<Scalar, Error = VortexError>,
239+
{
240+
type Error = VortexError;
241+
242+
fn try_from(value: Scalar) -> Result<Self, Self::Error> {
243+
let value = ListScalar::try_from(&value)?;
244+
let mut elems = vec![];
245+
for e in value
246+
.elements()
247+
.ok_or_else(|| vortex_err!("Expected non-null list"))?
248+
{
249+
elems.push(T::try_from(e)?);
250+
}
251+
Ok(elems)
252+
}
253+
}
254+
233255
macro_rules! from_vec_for_scalar_value {
234256
($T:ty) => {
235257
impl From<Vec<$T>> for ScalarValue {

vortex-scalar/src/utf8.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ impl<'a> TryFrom<&'a Scalar> for String {
233233
}
234234
}
235235

236+
impl TryFrom<Scalar> for String {
237+
type Error = VortexError;
238+
239+
fn try_from(value: Scalar) -> Result<Self, Self::Error> {
240+
Ok(BufferString::try_from(value)?.to_string())
241+
}
242+
}
243+
236244
impl From<&str> for Scalar {
237245
fn from(value: &str) -> Self {
238246
Self {

0 commit comments

Comments
 (0)