Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass through strings using arrow2::Buffers #2931

Merged
merged 6 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/re_data_ui/src/annotation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn class_description_ui(ui: &mut egui::Ui, class: &ClassDescription, id: ClassId
.and_then(|info| info.label.as_ref())
.map_or_else(
|| format!("id {id:?}"),
|label| label.0.clone(),
|label| label.0.as_str().to_owned(),
),
);
});
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/arrows3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Arrows3DPart {
.filter_map(
move |(annotation_info, vector, origin, label, color, labeled_instance)| {
let origin = origin.unwrap_or_default();
let label = annotation_info.label(label.map(|l| l.0).as_ref());
let label = annotation_info.label(label.as_ref().map(|l| l.as_str()));
match (vector, label) {
(vector, Some(label)) => {
let midpoint =
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/boxes2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Boxes2DPart {
let color =
annotation_info.color(color.map(move |c| c.to_array()).as_ref(), default_color);
let radius = radius.map_or(Size::AUTO, |r| Size::new_scene(r.0));
let label = annotation_info.label(label.map(|l| l.0).as_ref());
let label = annotation_info.label(label.as_ref().map(|l| l.as_str()));

self.0.extend_bounding_box(
macaw::BoundingBox {
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/boxes3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Boxes3DPart {
box_lines.outline_mask_ids(*outline_mask_ids);
}

if let Some(label) = annotation_info.label(label.as_ref().map(|s| &s.0)) {
if let Some(label) = annotation_info.label(label.as_ref().map(|s| s.as_str())) {
self.0.ui_labels.push(UiLabel {
text: label,
target: UiLabelTarget::Position3D(
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/lines2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Lines2DPart {
)
.filter_map(
move |(annotation_info, strip, label, color, labeled_instance)| {
let label = annotation_info.label(label.map(|l| l.0).as_ref());
let label = annotation_info.label(label.as_ref().map(|l| l.as_str()));
match (strip, label) {
(strip, Some(label)) => {
let midpoint = strip
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/lines3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Lines3DPart {
)
.filter_map(
move |(annotation_info, strip, label, color, labeled_instance)| {
let label = annotation_info.label(label.map(|l| l.0).as_ref());
let label = annotation_info.label(label.as_ref().map(|l| l.as_str()));
match (strip, label) {
(strip, Some(label)) => {
let midpoint = strip
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/points2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Points2DPart {
)
.filter_map(
move |(annotation_info, point, label, color, labeled_instance)| {
let label = annotation_info.label(label.map(|l| l.0).as_ref());
let label = annotation_info.label(label.as_ref().map(|l| l.as_str()));
match (point, label) {
(point, Some(label)) => Some(UiLabel {
text: label,
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/points3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Points3DPart {
)
.filter_map(
move |(annotation_info, point, label, color, labeled_instance)| {
let label = annotation_info.label(label.map(|l| l.0).as_ref());
let label = annotation_info.label(label.as_ref().map(|l| l.as_str()));
match (point, label) {
(point, Some(label)) => Some(UiLabel {
text: label,
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_time_series/src/view_part_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl TimeSeriesSystem {
// TODO(andreas): Support entity path
let color = annotation_info
.color(color.map(|c| c.to_array()).as_ref(), default_color);
let label = annotation_info.label(label.map(|l| l.into()).as_ref());
let label = annotation_info.label(label.as_ref().map(|l| l.as_str()));

const DEFAULT_RADIUS: f32 = 0.75;

Expand Down
2 changes: 1 addition & 1 deletion crates/re_types/source_hash.txt

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

63 changes: 63 additions & 0 deletions crates/re_types/src/arrow_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use arrow2::buffer::Buffer;

/// Convenience-wrapper around an arrow [`Buffer`] that is known to contain a
/// string.
///
/// The arrow2 [`Buffer`] object is internally reference-counted and can be
/// easily converted back to a `&str` referencing the underlying storage.
/// This avoids some of the lifetime complexities that would otherwise
/// arise from returning a `&str` directly, but is significantly more
/// performant than doing the full allocation necessary to return a `String`.
#[derive(Clone, Debug, Default)]
pub struct ArrowString(pub Buffer<u8>);

impl PartialEq for ArrowString {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_str() == other.as_str()
}
}

impl Eq for ArrowString {}

impl PartialOrd for ArrowString {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}

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

impl std::hash::Hash for ArrowString {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}

impl ArrowString {
#[inline]
pub fn as_str(&self) -> &str {
std::str::from_utf8(self.0.as_ref()).unwrap()
}
}

impl From<String> for ArrowString {
#[inline]
fn from(value: String) -> Self {
Self(value.as_bytes().to_vec().into())
}
}

impl From<&str> for ArrowString {
#[inline]
fn from(value: &str) -> Self {
Self(value.as_bytes().to_vec().into())
}
}
4 changes: 2 additions & 2 deletions crates/re_types/src/components/body_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ impl Body {
impl From<String> for Body {
#[inline]
fn from(value: String) -> Self {
Self(value)
Self(value.into())
}
}

impl From<&str> for Body {
#[inline]
fn from(value: &str) -> Self {
Self(value.to_owned())
Self(value.into())
}
}
42 changes: 23 additions & 19 deletions crates/re_types/src/components/label.rs

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

6 changes: 3 additions & 3 deletions crates/re_types/src/components/label_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ impl Label {
impl From<String> for Label {
#[inline]
fn from(value: String) -> Self {
Self(value)
Self(value.into())
}
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions crates/re_types/src/components/level_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ impl Level {
impl From<String> for Level {
#[inline]
fn from(value: String) -> Self {
Self(value)
Self(value.into())
}
}

impl From<&str> for Level {
#[inline]
fn from(value: &str) -> Self {
Self(value.to_owned())
Self(value.into())
}
}
19 changes: 12 additions & 7 deletions crates/re_types/src/datatypes/annotation_info.rs

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

3 changes: 3 additions & 0 deletions crates/re_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,5 +502,8 @@ mod size_bytes;
pub use component_name::ComponentName;
pub use size_bytes::SizeBytes;

mod arrow_string;
pub use arrow_string::ArrowString;

#[cfg(feature = "testing")]
pub mod testing;
Loading