Skip to content

Commit

Permalink
C++ custom result type used for serialization results (#3005)
Browse files Browse the repository at this point in the history
* Depends on #3001
* Part of #2919
* Fixes #2917
* Solves a big chunk of #2873

### What

Introduces a very simple `rerun::Result`. I decided to keep it **a lot**
more simple than the arrow Result type and refrained from the typical
"return or assign" etc. macro since I noticed that they get quite
complicated quickly.

The idea is that the rerun result type won't be needed by a lot of
manual code, so erring on the too lightweight side should be in our
favor.

We use this now accross the entire public serialization path - meaning
that once we move out array->arrow serialization helper into separate
headers we should be pretty much done with not exposing arrow headers!


### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/3005) (if
applicable)

- [PR Build Summary](https://build.rerun.io/pr/3005)
- [Docs
preview](https://rerun.io/preview/pr%3Aandreas%2Fcpp%2Fcustom-result-type/docs)
- [Examples
preview](https://rerun.io/preview/pr%3Aandreas%2Fcpp%2Fcustom-result-type/examples)
  • Loading branch information
Wumpf authored Aug 17, 2023
1 parent ce6829b commit 1a37133
Show file tree
Hide file tree
Showing 106 changed files with 1,300 additions and 854 deletions.
3 changes: 2 additions & 1 deletion .typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ authorise = "authorize"
behaviour = "behavior"
British = "American"
calibre = "caliber"
cancelled = "canceled"
# allow 'cancelled' since Arrow uses it.
#cancelled = "canceled"
candour = "candor"
capitalise = "capitalize"
catalogue = "catalog"
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.

26 changes: 19 additions & 7 deletions crates/re_types_builder/src/codegen/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,7 @@ fn component_to_data_cell_method(
cpp_includes: &mut Includes,
) -> Method {
hpp_includes.local.insert("../data_cell.hpp".to_owned());
hpp_includes.local.insert("../result.hpp".to_owned());
cpp_includes.local.insert("../arrow.hpp".to_owned()); // ipc_from_table
cpp_includes.system.insert("arrow/api.h".to_owned());

Expand All @@ -1131,7 +1132,7 @@ fn component_to_data_cell_method(
docs: format!("Creates a Rerun DataCell from an array of {type_ident} components.").into(),
declaration: MethodDeclaration {
is_static: true,
return_type: quote! { arrow::Result<rerun::DataCell> },
return_type: quote! { Result<rerun::DataCell> },
name_and_parameters: quote! {
to_data_cell(const #type_ident* instances, size_t num_instances)
},
Expand Down Expand Up @@ -1163,7 +1164,11 @@ fn component_to_data_cell_method(
#NEWLINE_TOKEN
rerun::DataCell cell;
cell.component_name = #type_ident::NAME;
ARROW_ASSIGN_OR_RAISE(cell.buffer, rerun::ipc_from_table(*arrow::Table::Make(schema, {array})));
const auto result = rerun::ipc_from_table(*arrow::Table::Make(schema, {array}));
if (result.is_err()) {
return result.error;
}
cell.buffer = std::move(result.value);
#NEWLINE_TOKEN
#NEWLINE_TOKEN
return cell;
Expand All @@ -1178,6 +1183,7 @@ fn archetype_to_data_cells(
cpp_includes: &mut Includes,
) -> Method {
hpp_includes.local.insert("../data_cell.hpp".to_owned());
hpp_includes.local.insert("../result.hpp".to_owned());
cpp_includes.system.insert("arrow/api.h".to_owned());

// TODO(andreas): Splats need to be handled separately.
Expand All @@ -1203,8 +1209,11 @@ fn archetype_to_data_cells(
quote! {
if (#field_name.has_value()) {
const auto& value = #field_name.value();
ARROW_ASSIGN_OR_RAISE(const auto cell, #to_data_cell);
cells.push_back(cell);
const auto result = #to_data_cell;
if (result.is_err()) {
return result.error;
}
cells.emplace_back(std::move(result.value));
}
}
} else {
Expand All @@ -1215,8 +1224,11 @@ fn archetype_to_data_cells(
};
quote! {
{
ARROW_ASSIGN_OR_RAISE(const auto cell, #to_data_cell);
cells.push_back(cell);
const auto result = #to_data_cell;
if (result.is_err()) {
return result.error;
}
cells.emplace_back(std::move(result.value));
}
}
}
Expand All @@ -1226,7 +1238,7 @@ fn archetype_to_data_cells(
docs: "Creates a list of Rerun DataCell from this archetype.".into(),
declaration: MethodDeclaration {
is_static: false,
return_type: quote!(arrow::Result<std::vector<rerun::DataCell>>),
return_type: quote!(Result<std::vector<rerun::DataCell>>),
name_and_parameters: quote!(to_data_cells() const),
},
definition_body: quote! {
Expand Down
8 changes: 4 additions & 4 deletions crates/rerun_c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,22 @@ pub struct CDataRow {
pub enum CErrorCode {
Ok = 0,

_CategoryArgument = 0x000000010,
_CategoryArgument = 0x0000_00010,
UnexpectedNullArgument,
InvalidStringArgument,
InvalidRecordingStreamHandle,
InvalidSocketAddress,
InvalidEntityPath,

_CategoryRecordingStream = 0x000000100,
_CategoryRecordingStream = 0x0000_00100,
RecordingStreamCreationFailure,
RecordingStreamSaveFailure,

_CategoryArrow = 0x000001000,
_CategoryArrow = 0x0000_1000,
ArrowIpcMessageParsingFailure,
ArrowDataCellError,

Unknown = 0xFFFFFFFF,
Unknown = 0xFFFF_FFFF,
}

#[repr(C)]
Expand Down
2 changes: 1 addition & 1 deletion crates/rerun_c/src/rerun.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ enum {
RR_ERROR_CODE_OK = 0,

// Invalid argument errors.
_RR_ERROR_CODE_CATEGORY_ARGUMENT = 0x000000010,
_RR_ERROR_CODE_CATEGORY_ARGUMENT = 0x00000010,
RR_ERROR_CODE_UNEXPECTED_NULL_ARGUMENT,
RR_ERROR_CODE_INVALID_STRING_ARGUMENT,
RR_ERROR_CODE_INVALID_RECORDING_STREAM_HANDLE,
Expand Down
1 change: 1 addition & 0 deletions rerun_cpp/src/rerun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
// Rerun API.
#include "rerun/error.hpp"
#include "rerun/recording_stream.hpp"
#include "rerun/result.hpp"
#include "rerun/sdk_info.hpp"
Loading

0 comments on commit 1a37133

Please sign in to comment.