Skip to content

Commit

Permalink
Codegen/IDL 8: handwritten Rust tests and extensions for Points2D (#…
Browse files Browse the repository at this point in the history
…2432)

- First commit bring back all existing extensions (and maybe add one or
two extremely trivial ones)
- Second commit adds a regression test, which is much simpler than
Python since we have the type system to rely for 95% of the stuff.

Closes #2241 

---

Codegen/IDL PR series:
- #2362
- #2363
- #2369
- #2370 
- #2374 
- #2375 
- #2410
- #2432

---------

Co-authored-by: Andreas Reich <r_andreas2@web.de>
  • Loading branch information
teh-cmc and Wumpf committed Jun 14, 2023
1 parent 7c59e05 commit 1e49d63
Show file tree
Hide file tree
Showing 16 changed files with 413 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/re_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ macaw = { workspace = true, optional = true }
# External
glam.workspace = true
itertools.workspace = true
similar-asserts = "1.4.2"


[build-dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct DrawOrder (
"attr.arrow.transparent",
"attr.python.aliases": "float",
"attr.python.array_aliases": "npt.NDArray[np.float32]",
"attr.rust.derive": "Debug, Clone, Copy, PartialEq, PartialOrd",
"attr.rust.derive": "Debug, Clone, Copy",
"attr.rust.repr": "transparent",
"attr.rust.tuple_struct",
order: 100
Expand Down
7 changes: 7 additions & 0 deletions crates/re_types/src/components/class_id_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use super::ClassId;

impl From<u16> for ClassId {
fn from(value: u16) -> Self {
Self(value)
}
}
50 changes: 50 additions & 0 deletions crates/re_types/src/components/color_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use super::Color;

impl Color {
#[inline]
pub fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Self::from([r, g, b, 255])
}

#[inline]
pub fn from_unmultiplied_rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self::from([r, g, b, a])
}

#[inline]
pub fn to_array(&self) -> [u8; 4] {
[
(self.0 >> 24) as u8,
(self.0 >> 16) as u8,
(self.0 >> 8) as u8,
self.0 as u8,
]
}
}

impl From<u32> for Color {
#[inline]
fn from(c: u32) -> Self {
Self(c)
}
}

impl From<[u8; 4]> for Color {
#[inline]
fn from(bytes: [u8; 4]) -> Self {
Self(
(bytes[0] as u32) << 24
| (bytes[1] as u32) << 16
| (bytes[2] as u32) << 8
| (bytes[3] as u32),
)
}
}

#[cfg(feature = "ecolor")]
impl From<Color> for ecolor::Color32 {
fn from(color: Color) -> Self {
let [r, g, b, a] = color.to_array();
Self::from_rgba_premultiplied(r, g, b, a)
}
}
2 changes: 1 addition & 1 deletion crates/re_types/src/components/draw_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/// Within an entity draw order is governed by the order of the components.
///
/// Draw order for entities with the same draw order is generally undefined.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct DrawOrder(pub f32);

Expand Down
60 changes: 60 additions & 0 deletions crates/re_types/src/components/draw_order_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use super::DrawOrder;

// TODO(cmc): come up with some DSL in our flatbuffers definitions so that we can declare these
// constants directly in there.
impl DrawOrder {
/// Draw order used for images if no draw order was specified.
pub const DEFAULT_IMAGE: DrawOrder = DrawOrder(-10.0);

/// Draw order used for 2D boxes if no draw order was specified.
pub const DEFAULT_BOX2D: DrawOrder = DrawOrder(10.0);

/// Draw order used for 2D lines if no draw order was specified.
pub const DEFAULT_LINES2D: DrawOrder = DrawOrder(20.0);

/// Draw order used for 2D points if no draw order was specified.
pub const DEFAULT_POINTS2D: DrawOrder = DrawOrder(30.0);
}

impl std::cmp::PartialEq for DrawOrder {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.0.is_nan() && other.0.is_nan() || self.0 == other.0
}
}

impl std::cmp::Eq for DrawOrder {}

impl std::cmp::PartialOrd for DrawOrder {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
if other == self {
Some(std::cmp::Ordering::Equal)
} else if other.0.is_nan() || self.0 < other.0 {
Some(std::cmp::Ordering::Less)
} else {
Some(std::cmp::Ordering::Greater)
}
}
}

impl std::cmp::Ord for DrawOrder {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other).unwrap()
}
}

impl From<f32> for DrawOrder {
#[inline]
fn from(value: f32) -> Self {
Self(value)
}
}

impl From<DrawOrder> for f32 {
#[inline]
fn from(value: DrawOrder) -> Self {
value.0
}
}
22 changes: 22 additions & 0 deletions crates/re_types/src/components/instance_key_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::InstanceKey;

// TODO(cmc): come up with some DSL in our flatbuffers definitions so that we can declare these
// constants directly in there.
impl InstanceKey {
/// Draw order used for images if no draw order was specified.
pub const SPLAT: Self = Self(u64::MAX);
}

impl From<u64> for InstanceKey {
#[inline]
fn from(value: u64) -> Self {
Self(value)
}
}

impl From<InstanceKey> for u64 {
#[inline]
fn from(value: InstanceKey) -> Self {
value.0
}
}
7 changes: 7 additions & 0 deletions crates/re_types/src/components/keypoint_id_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use super::KeypointId;

impl From<u16> for KeypointId {
fn from(value: u16) -> Self {
Self(value)
}
}
51 changes: 51 additions & 0 deletions crates/re_types/src/components/label_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use super::Label;

impl Label {
#[inline]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}

impl From<String> for Label {
#[inline]
fn from(value: String) -> Self {
Self(value)
}
}

impl From<&str> for Label {
#[inline]
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}

impl From<Label> for String {
#[inline]
fn from(value: Label) -> Self {
value.0
}
}

impl AsRef<str> for Label {
#[inline]
fn as_ref(&self) -> &str {
self.as_str()
}
}

impl std::borrow::Borrow<str> for Label {
#[inline]
fn borrow(&self) -> &str {
self.as_str()
}
}

impl std::ops::Deref for Label {
type Target = str;
#[inline]
fn deref(&self) -> &str {
self.as_str()
}
}
8 changes: 8 additions & 0 deletions crates/re_types/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// NOTE: This file was autogenerated by re_types_builder; DO NOT EDIT.

mod class_id;
mod class_id_ext;
mod color;
mod color_ext;
mod draw_order;
mod draw_order_ext;
mod instance_key;
mod instance_key_ext;
mod keypoint_id;
mod keypoint_id_ext;
mod label;
mod label_ext;
mod point2d;
mod point2d_ext;
mod radius;
mod radius_ext;

pub use self::class_id::ClassId;
pub use self::color::Color;
Expand Down
57 changes: 57 additions & 0 deletions crates/re_types/src/components/point2d_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use super::Point2D;

impl Point2D {
pub const ZERO: Self = Self::new(0.0, 0.0);
pub const ONE: Self = Self::new(1.0, 1.0);

#[inline]
pub const fn new(x: f32, y: f32) -> Self {
Self(crate::datatypes::Vec2D::new(x, y))
}
}

impl std::ops::Deref for Point2D {
type Target = crate::datatypes::Vec2D;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl From<(f32, f32)> for Point2D {
#[inline]
fn from(xy: (f32, f32)) -> Self {
Self(xy.into())
}
}

impl From<[f32; 2]> for Point2D {
#[inline]
fn from(p: [f32; 2]) -> Self {
Self(p.into())
}
}

#[cfg(feature = "glam")]
impl From<glam::Vec2> for Point2D {
#[inline]
fn from(pt: glam::Vec2) -> Self {
Self::new(pt.x, pt.y)
}
}

#[cfg(feature = "glam")]
impl From<Point2D> for glam::Vec2 {
#[inline]
fn from(pt: Point2D) -> Self {
Self::new(pt.x(), pt.y())
}
}

#[cfg(feature = "glam")]
impl From<Point2D> for glam::Vec3 {
#[inline]
fn from(pt: Point2D) -> Self {
Self::new(pt.x(), pt.y(), 0.0)
}
}
24 changes: 24 additions & 0 deletions crates/re_types/src/components/radius_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use super::Radius;

impl Radius {
pub const ZERO: Self = Self::new(0.0);
pub const ONE: Self = Self::new(1.0);

#[inline]
pub const fn new(r: f32) -> Self {
Self(r)
}
}

impl From<f32> for Radius {
#[inline]
fn from(r: f32) -> Self {
Self::new(r)
}
}

impl std::fmt::Display for Radius {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:.prec$}", self.0, prec = crate::DISPLAY_PRECISION,)
}
}
1 change: 1 addition & 0 deletions crates/re_types/src/datatypes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// NOTE: This file was autogenerated by re_types_builder; DO NOT EDIT.

mod vec2d;
mod vec2d_ext;

pub use self::vec2d::Vec2D;

1 comment on commit 1e49d63

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Rust Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.25.

Benchmark suite Current: 1e49d63 Previous: 7c59e05 Ratio
tuid/Tuid::random 44 ns/iter (± 0) 34 ns/iter (± 9) 1.29

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.