@@ -279,12 +279,13 @@ impl<'a> StructScalar<'a> {
279279}
280280
281281impl Scalar {
282- /// Creates a new struct scalar with the given fields.
283- pub fn struct_ ( dtype : DType , children : Vec < Scalar > ) -> Self {
282+ /// Creates a new struct scalar with the given fields, checking dtypes at runtime .
283+ pub fn struct_ ( dtype : DType , children : impl IntoIterator < Item = Scalar > ) -> Self {
284284 let DType :: Struct ( struct_fields, _) = & dtype else {
285285 vortex_panic ! ( "Expected struct dtype, found {}" , dtype) ;
286286 } ;
287287
288+ let children: Vec < Scalar > = children. into_iter ( ) . collect ( ) ;
288289 let field_dtypes = struct_fields. fields ( ) ;
289290 if children. len ( ) != field_dtypes. len ( ) {
290291 vortex_panic ! (
@@ -305,9 +306,24 @@ impl Scalar {
305306 }
306307 }
307308
308- let mut value_children = Vec :: with_capacity ( children. len ( ) ) ;
309- value_children. extend ( children. into_iter ( ) . map ( |x| x. into_value ( ) ) ) ;
309+ let value_children: Vec < _ > = children. into_iter ( ) . map ( |x| x. into_value ( ) ) . collect ( ) ;
310+ Self :: try_new ( dtype, Some ( ScalarValue :: List ( value_children) ) )
311+ . vortex_expect ( "unable to construct a struct `Scalar`" )
312+ }
310313
314+ /// Creates a new struct scalar from an iterator of field scalars, skipping dtype checks.
315+ ///
316+ /// # Safety
317+ ///
318+ /// Caller must ensure:
319+ /// - `dtype` is `DType::Struct`
320+ /// - The iterator yields exactly as many scalars as `dtype` has fields
321+ /// - Each scalar's dtype matches the corresponding field dtype in `dtype`
322+ pub unsafe fn struct_unchecked (
323+ dtype : DType ,
324+ children : impl IntoIterator < Item = Scalar > ,
325+ ) -> Self {
326+ let value_children: Vec < _ > = children. into_iter ( ) . map ( |s| s. into_value ( ) ) . collect ( ) ;
311327 Self :: try_new ( dtype, Some ( ScalarValue :: List ( value_children) ) )
312328 . vortex_expect ( "unable to construct a struct `Scalar`" )
313329 }
0 commit comments