Skip to content

Commit 9c4ecb6

Browse files
authored
feat: into_struct_fields_* (#5128)
I find myself wanting this in Spiral. Is it reasonable? --------- Signed-off-by: Daniel King <dan@spiraldb.com>
1 parent e1ca80a commit 9c4ecb6

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

vortex-dtype/src/dtype.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,15 @@ impl DType {
334334
}
335335
}
336336

337+
/// Owned version of [Self::as_decimal_opt].
338+
pub fn into_decimal_opt(self) -> Option<DecimalDType> {
339+
if let Decimal(decimal, _) = self {
340+
Some(decimal)
341+
} else {
342+
None
343+
}
344+
}
345+
337346
/// Get the inner element dtype if `self` is a [`DType::List`], otherwise returns `None`.
338347
///
339348
/// Note that this does _not_ return `Some` if `self` is a [`DType::FixedSizeList`].
@@ -345,6 +354,15 @@ impl DType {
345354
}
346355
}
347356

357+
/// Owned version of [Self::as_list_element_opt].
358+
pub fn into_list_element_opt(self) -> Option<Arc<DType>> {
359+
if let List(edt, _) = self {
360+
Some(edt)
361+
} else {
362+
None
363+
}
364+
}
365+
348366
/// Get the inner element dtype if `self` is a [`DType::FixedSizeList`], otherwise returns
349367
/// `None`.
350368
///
@@ -357,6 +375,15 @@ impl DType {
357375
}
358376
}
359377

378+
/// Owned version of [Self::as_fixed_size_list_element_opt].
379+
pub fn into_fixed_size_list_element_opt(self) -> Option<Arc<DType>> {
380+
if let FixedSizeList(edt, ..) = self {
381+
Some(edt)
382+
} else {
383+
None
384+
}
385+
}
386+
360387
/// Get the inner element dtype if `self` is **either** a [`DType::List`] or a
361388
/// [`DType::FixedSizeList`], otherwise returns `None`
362389
pub fn as_any_size_list_element_opt(&self) -> Option<&Arc<DType>> {
@@ -369,6 +396,17 @@ impl DType {
369396
}
370397
}
371398

399+
/// Owned version of [Self::as_any_size_list_element_opt].
400+
pub fn into_any_size_list_element_opt(self) -> Option<Arc<DType>> {
401+
if let FixedSizeList(edt, ..) = self {
402+
Some(edt)
403+
} else if let List(edt, ..) = self {
404+
Some(edt)
405+
} else {
406+
None
407+
}
408+
}
409+
372410
/// Returns the [`StructFields`] from a struct [`DType`].
373411
///
374412
/// # Panics
@@ -381,6 +419,14 @@ impl DType {
381419
vortex_panic!("DType is not a Struct")
382420
}
383421

422+
/// Owned version of [Self::as_struct_fields].
423+
pub fn into_struct_fields(self) -> StructFields {
424+
if let Struct(f, _) = self {
425+
return f;
426+
}
427+
vortex_panic!("DType is not a Struct")
428+
}
429+
384430
/// Get the `StructDType` if `self` is a `StructDType`, otherwise `None`
385431
pub fn as_struct_fields_opt(&self) -> Option<&StructFields> {
386432
if let Struct(f, _) = self {
@@ -390,6 +436,15 @@ impl DType {
390436
}
391437
}
392438

439+
/// Owned version of [Self::as_struct_fields_opt].
440+
pub fn into_struct_fields_opt(self) -> Option<StructFields> {
441+
if let Struct(f, _) = self {
442+
Some(f)
443+
} else {
444+
None
445+
}
446+
}
447+
393448
/// Convenience method for creating a [`DType::List`].
394449
pub fn list(dtype: impl Into<DType>, nullability: Nullability) -> Self {
395450
List(Arc::new(dtype.into()), nullability)

0 commit comments

Comments
 (0)