Skip to content

Commit

Permalink
auto-derive Into<Cow> for datatypes and components
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jun 28, 2023
1 parent 39c1453 commit fad4958
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/re_types/src/components/class_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ClassId(pub u16);

impl<'a> From<ClassId> for ::std::borrow::Cow<'a, ClassId> {
fn from(value: ClassId) -> Self {
std::borrow::Cow::Owned(value)
}
}

impl<'a> From<&'a ClassId> for ::std::borrow::Cow<'a, ClassId> {
fn from(value: &'a ClassId) -> Self {
std::borrow::Cow::Borrowed(value)
}
}

impl crate::Component for ClassId {
#[inline]
fn name() -> crate::ComponentName {
Expand Down
12 changes: 12 additions & 0 deletions crates/re_types/src/components/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
#[repr(transparent)]
pub struct Color(pub u32);

impl<'a> From<Color> for ::std::borrow::Cow<'a, Color> {
fn from(value: Color) -> Self {
std::borrow::Cow::Owned(value)
}
}

impl<'a> From<&'a Color> for ::std::borrow::Cow<'a, Color> {
fn from(value: &'a Color) -> Self {
std::borrow::Cow::Borrowed(value)
}
}

impl crate::Component for Color {
#[inline]
fn name() -> crate::ComponentName {
Expand Down
12 changes: 12 additions & 0 deletions crates/re_types/src/components/draw_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
#[repr(transparent)]
pub struct DrawOrder(pub f32);

impl<'a> From<DrawOrder> for ::std::borrow::Cow<'a, DrawOrder> {
fn from(value: DrawOrder) -> Self {
std::borrow::Cow::Owned(value)
}
}

impl<'a> From<&'a DrawOrder> for ::std::borrow::Cow<'a, DrawOrder> {
fn from(value: &'a DrawOrder) -> Self {
std::borrow::Cow::Borrowed(value)
}
}

impl crate::Component for DrawOrder {
#[inline]
fn name() -> crate::ComponentName {
Expand Down
12 changes: 12 additions & 0 deletions crates/re_types/src/components/instance_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct InstanceKey(pub u64);

impl<'a> From<InstanceKey> for ::std::borrow::Cow<'a, InstanceKey> {
fn from(value: InstanceKey) -> Self {
std::borrow::Cow::Owned(value)
}
}

impl<'a> From<&'a InstanceKey> for ::std::borrow::Cow<'a, InstanceKey> {
fn from(value: &'a InstanceKey) -> Self {
std::borrow::Cow::Borrowed(value)
}
}

impl crate::Component for InstanceKey {
#[inline]
fn name() -> crate::ComponentName {
Expand Down
12 changes: 12 additions & 0 deletions crates/re_types/src/components/keypoint_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct KeypointId(pub u16);

impl<'a> From<KeypointId> for ::std::borrow::Cow<'a, KeypointId> {
fn from(value: KeypointId) -> Self {
std::borrow::Cow::Owned(value)
}
}

impl<'a> From<&'a KeypointId> for ::std::borrow::Cow<'a, KeypointId> {
fn from(value: &'a KeypointId) -> Self {
std::borrow::Cow::Borrowed(value)
}
}

impl crate::Component for KeypointId {
#[inline]
fn name() -> crate::ComponentName {
Expand Down
12 changes: 12 additions & 0 deletions crates/re_types/src/components/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
#[repr(transparent)]
pub struct Label(pub String);

impl<'a> From<Label> for ::std::borrow::Cow<'a, Label> {
fn from(value: Label) -> Self {
std::borrow::Cow::Owned(value)
}
}

impl<'a> From<&'a Label> for ::std::borrow::Cow<'a, Label> {
fn from(value: &'a Label) -> Self {
std::borrow::Cow::Borrowed(value)
}
}

impl crate::Component for Label {
#[inline]
fn name() -> crate::ComponentName {
Expand Down
12 changes: 12 additions & 0 deletions crates/re_types/src/components/point2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ pub struct Point2D {
pub y: f32,
}

impl<'a> From<Point2D> for ::std::borrow::Cow<'a, Point2D> {
fn from(value: Point2D) -> Self {
std::borrow::Cow::Owned(value)
}
}

impl<'a> From<&'a Point2D> for ::std::borrow::Cow<'a, Point2D> {
fn from(value: &'a Point2D) -> Self {
std::borrow::Cow::Borrowed(value)
}
}

impl crate::Component for Point2D {
#[inline]
fn name() -> crate::ComponentName {
Expand Down
12 changes: 12 additions & 0 deletions crates/re_types/src/components/radius.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Radius(pub f32);

impl<'a> From<Radius> for ::std::borrow::Cow<'a, Radius> {
fn from(value: Radius) -> Self {
std::borrow::Cow::Owned(value)
}
}

impl<'a> From<&'a Radius> for ::std::borrow::Cow<'a, Radius> {
fn from(value: &'a Radius) -> Self {
std::borrow::Cow::Borrowed(value)
}
}

impl crate::Component for Radius {
#[inline]
fn name() -> crate::ComponentName {
Expand Down
12 changes: 12 additions & 0 deletions crates/re_types/src/datatypes/vec2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
pub struct Vec2D(pub [f32; 2usize]);

impl<'a> From<Vec2D> for ::std::borrow::Cow<'a, Vec2D> {
fn from(value: Vec2D) -> Self {
std::borrow::Cow::Owned(value)
}
}

impl<'a> From<&'a Vec2D> for ::std::borrow::Cow<'a, Vec2D> {
fn from(value: &'a Vec2D) -> Self {
std::borrow::Cow::Borrowed(value)
}
}

impl crate::Datatype for Vec2D {
#[inline]
fn name() -> crate::DatatypeName {
Expand Down
20 changes: 20 additions & 0 deletions crates/re_types_builder/src/codegen/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,27 @@ fn quote_trait_impls_from_obj(
.try_get_attr::<String>(ATTR_RERUN_LEGACY_FQNAME)
.unwrap_or_else(|| fqname.clone());

let into_cow = quote! {
// NOTE: We need these so end-user code can effortlessly serialize both iterators
// of owned data and iterators of referenced data without ever having to stop and
// think about it.

impl<'a> From<#name> for ::std::borrow::Cow<'a, #name> {
fn from(value: #name) -> Self {
std::borrow::Cow::Owned(value)
}
}

impl<'a> From<&'a #name> for ::std::borrow::Cow<'a, #name> {
fn from(value: &'a #name) -> Self {
std::borrow::Cow::Borrowed(value)
}
}
};

quote! {
#into_cow

impl crate::#kind for #name {
#[inline]
fn name() -> crate::#kind_name {
Expand Down

0 comments on commit fad4958

Please sign in to comment.