From 2026c55cdbb77f1b9634b45da80c29a71bf47d31 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Fri, 5 Jul 2024 18:23:25 +0200 Subject: [PATCH] use bytemuck cast_vec for `from_arrow` when possible --- crates/re_types/src/components/class_id.rs | 3 +-- crates/re_types/src/components/color.rs | 3 +-- crates/re_types/src/components/fill_ratio.rs | 3 +-- .../re_types/src/components/gamma_correction.rs | 3 +-- crates/re_types/src/components/keypoint_id.rs | 3 +-- crates/re_types/src/components/position2d.rs | 3 +-- crates/re_types/src/components/position3d.rs | 3 +-- crates/re_types/src/components/range1d.rs | 3 +-- crates/re_types/src/components/texcoord2d.rs | 3 +-- .../re_types/src/components/triangle_indices.rs | 3 +-- crates/re_types/src/components/vector2d.rs | 3 +-- crates/re_types/src/components/vector3d.rs | 3 +-- crates/re_types_builder/src/codegen/rust/api.rs | 17 ++++++++++++++--- 13 files changed, 26 insertions(+), 27 deletions(-) diff --git a/crates/re_types/src/components/class_id.rs b/crates/re_types/src/components/class_id.rs index 40aaece1ddff..2086d4bc4ef9 100644 --- a/crates/re_types/src/components/class_id.rs +++ b/crates/re_types/src/components/class_id.rs @@ -141,7 +141,6 @@ impl ::re_types_core::Loggable for ClassId { where Self: Sized, { - crate::datatypes::ClassId::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::ClassId::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types/src/components/color.rs b/crates/re_types/src/components/color.rs index 69163958ba3a..88f4ad2c87eb 100644 --- a/crates/re_types/src/components/color.rs +++ b/crates/re_types/src/components/color.rs @@ -139,7 +139,6 @@ impl ::re_types_core::Loggable for Color { where Self: Sized, { - crate::datatypes::Rgba32::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::Rgba32::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types/src/components/fill_ratio.rs b/crates/re_types/src/components/fill_ratio.rs index b10754cf8703..a839f8c26ea5 100644 --- a/crates/re_types/src/components/fill_ratio.rs +++ b/crates/re_types/src/components/fill_ratio.rs @@ -141,7 +141,6 @@ impl ::re_types_core::Loggable for FillRatio { where Self: Sized, { - crate::datatypes::Float32::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::Float32::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types/src/components/gamma_correction.rs b/crates/re_types/src/components/gamma_correction.rs index 59f421807243..90e2d08aa99d 100644 --- a/crates/re_types/src/components/gamma_correction.rs +++ b/crates/re_types/src/components/gamma_correction.rs @@ -142,7 +142,6 @@ impl ::re_types_core::Loggable for GammaCorrection { where Self: Sized, { - crate::datatypes::Float32::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::Float32::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types/src/components/keypoint_id.rs b/crates/re_types/src/components/keypoint_id.rs index d86d58367aed..b2165da73efc 100644 --- a/crates/re_types/src/components/keypoint_id.rs +++ b/crates/re_types/src/components/keypoint_id.rs @@ -153,7 +153,6 @@ impl ::re_types_core::Loggable for KeypointId { where Self: Sized, { - crate::datatypes::KeypointId::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::KeypointId::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types/src/components/position2d.rs b/crates/re_types/src/components/position2d.rs index c5fc88c9b075..5037fa277951 100644 --- a/crates/re_types/src/components/position2d.rs +++ b/crates/re_types/src/components/position2d.rs @@ -158,7 +158,6 @@ impl ::re_types_core::Loggable for Position2D { where Self: Sized, { - crate::datatypes::Vec2D::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::Vec2D::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types/src/components/position3d.rs b/crates/re_types/src/components/position3d.rs index d42105cfe756..f94c053b7ff3 100644 --- a/crates/re_types/src/components/position3d.rs +++ b/crates/re_types/src/components/position3d.rs @@ -158,7 +158,6 @@ impl ::re_types_core::Loggable for Position3D { where Self: Sized, { - crate::datatypes::Vec3D::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::Vec3D::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types/src/components/range1d.rs b/crates/re_types/src/components/range1d.rs index 7f942e9b81ac..8e232e7609a1 100644 --- a/crates/re_types/src/components/range1d.rs +++ b/crates/re_types/src/components/range1d.rs @@ -158,7 +158,6 @@ impl ::re_types_core::Loggable for Range1D { where Self: Sized, { - crate::datatypes::Range1D::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::Range1D::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types/src/components/texcoord2d.rs b/crates/re_types/src/components/texcoord2d.rs index c1cdbdba2d22..74163e13c8ac 100644 --- a/crates/re_types/src/components/texcoord2d.rs +++ b/crates/re_types/src/components/texcoord2d.rs @@ -173,7 +173,6 @@ impl ::re_types_core::Loggable for Texcoord2D { where Self: Sized, { - crate::datatypes::Vec2D::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::Vec2D::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types/src/components/triangle_indices.rs b/crates/re_types/src/components/triangle_indices.rs index f6ec858bcce8..c8c6125375ed 100644 --- a/crates/re_types/src/components/triangle_indices.rs +++ b/crates/re_types/src/components/triangle_indices.rs @@ -158,7 +158,6 @@ impl ::re_types_core::Loggable for TriangleIndices { where Self: Sized, { - crate::datatypes::UVec3D::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::UVec3D::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types/src/components/vector2d.rs b/crates/re_types/src/components/vector2d.rs index 4ff582adffce..2835b4af7772 100644 --- a/crates/re_types/src/components/vector2d.rs +++ b/crates/re_types/src/components/vector2d.rs @@ -158,7 +158,6 @@ impl ::re_types_core::Loggable for Vector2D { where Self: Sized, { - crate::datatypes::Vec2D::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::Vec2D::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types/src/components/vector3d.rs b/crates/re_types/src/components/vector3d.rs index 7bd6c050f996..2f96e3382d9e 100644 --- a/crates/re_types/src/components/vector3d.rs +++ b/crates/re_types/src/components/vector3d.rs @@ -158,7 +158,6 @@ impl ::re_types_core::Loggable for Vector3D { where Self: Sized, { - crate::datatypes::Vec3D::from_arrow(arrow_data) - .map(|v| v.into_iter().map(|v| Self(v)).collect()) + crate::datatypes::Vec3D::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) } } diff --git a/crates/re_types_builder/src/codegen/rust/api.rs b/crates/re_types_builder/src/codegen/rust/api.rs index f0f6a72ab0fb..62135eb920f9 100644 --- a/crates/re_types_builder/src/codegen/rust/api.rs +++ b/crates/re_types_builder/src/codegen/rust/api.rs @@ -798,9 +798,20 @@ fn quote_trait_impls_from_obj( let from_arrow_body = if forward_serialization { let forwarded_type = quote_field_type_from_typ(&obj.fields[0].typ, true).0; - // TODO: Do #forwarded_type::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) if PODsible. - quote! { - #forwarded_type::from_arrow(arrow_data).map(|v| v.into_iter().map(|v| Self(v)).collect()) + if obj + .try_get_attr::(ATTR_RUST_DERIVE) + .map_or(false, |d| d.contains("bytemuck::Pod")) + || obj + .try_get_attr::(ATTR_RUST_DERIVE_ONLY) + .map_or(false, |d| d.contains("bytemuck::Pod")) + { + quote! { + #forwarded_type::from_arrow(arrow_data).map(|v| bytemuck::cast_vec(v)) + } + } else { + quote! { + #forwarded_type::from_arrow(arrow_data).map(|v| v.into_iter().map(|v| Self(v)).collect()) + } } } else { let quoted_deserializer =