Skip to content

Commit

Permalink
Fix #169 (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
TethysSvensson authored Feb 3, 2023
1 parent be40ee0 commit b871e31
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 75 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- \[Rust\]: Add license files to crates
- \[Rust\]: Implement a builder pattern for serializing tables and unions
- \[Rust\]: rustfmt runs twice on generated code to ensure it is formatted correctly
- \[Rust\]: Fix an issue with using structs across different flatbuffer namespaces.
- Add support for docstrings, and add them to the Rust output.
- Update the `README` with information about our Discord server.
- Split up `planus-cli` into multiple crates
Expand Down
2 changes: 1 addition & 1 deletion crates/planus-codegen/src/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ impl Backend for RustBackend {
format_relative_namespace(&relative_namespace, &info.owned_name).to_string();
let ref_name = format_relative_namespace(&relative_namespace, &info.ref_name);
getter_return_type = format!("{ref_name}<'a>");
getter_code = format!("{ref_name}(buffer)");
getter_code = "::core::convert::From::from(buffer)".to_string();
}
ResolvedType::Enum(decl, info, relative_namespace, _) => {
owned_type = format_relative_namespace(&relative_namespace, &info.name).to_string();
Expand Down
6 changes: 6 additions & 0 deletions crates/planus-codegen/src/templates/rust/struct.template
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ impl<'a> ::core::fmt::Debug for {{ info.ref_name }}<'a> {
}
}

impl<'a> ::core::convert::From<::planus::ArrayWithStartOffset<'a, {{ size }}>> for {{ info.ref_name }}<'a> {
fn from(array: ::planus::ArrayWithStartOffset<'a, {{ size }}>) -> Self {
Self(array)
}
}

{% if info.should_do_infallible_conversion -%}
impl<'a> ::core::convert::From<{{ info.ref_name }}<'a>> for {{ info.owned_name}} {
#[allow(unreachable_code)]
Expand Down
6 changes: 6 additions & 0 deletions examples/rust/src/monster_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ mod root {
}
}

impl<'a> ::core::convert::From<::planus::ArrayWithStartOffset<'a, 12>> for Vec3Ref<'a> {
fn from(array: ::planus::ArrayWithStartOffset<'a, 12>) -> Self {
Self(array)
}
}

impl<'a> ::core::convert::From<Vec3Ref<'a>> for Vec3 {
#[allow(unreachable_code)]
fn from(value: Vec3Ref<'a>) -> Self {
Expand Down
48 changes: 39 additions & 9 deletions test/files/valid/include_helpers/A.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,43 @@ include "../include_test2.fbs";

namespace A;

table Obj {
here: Obj;
a: A.Obj;
a_a: A.A.Obj;
a_b: A.B.Obj;
b: B.Obj;
b_a: B.A.Obj;
b_b: B.B.Obj;
c: C.Obj;
table Table {
here: Table;
a: A.Table;
a_a: A.A.Table;
a_b: A.B.Table;
b: B.Table;
b_a: B.A.Table;
b_b: B.B.Table;
c: C.Table;
struct_here: Struct;
struct_a: A.Struct;
struct_a_a: A.A.Struct;
struct_a_b: A.B.Struct;
struct_b: B.Struct;
struct_b_a: B.A.Struct;
struct_b_b: B.B.Struct;
struct_c: C.Struct;
}

table Struct {
here: Struct;
a: A.Struct;
a_a: A.A.Struct;
a_b: A.B.Struct;
b: B.Struct;
b_a: B.A.Struct;
b_b: B.B.Struct;
c: C.Struct;
}

union Union {
Table,
A.Table,
A.A.Table,
A.B.Table,
B.Table,
B.A.Table,
B.B.Table,
C.Table
}
48 changes: 39 additions & 9 deletions test/files/valid/include_helpers/A_A.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,43 @@ include "../include_test2.fbs";

namespace A.A;

table Obj {
here: Obj;
a: A.Obj;
a_a: A.A.Obj;
a_b: A.B.Obj;
b: B.Obj;
b_a: B.A.Obj;
b_b: B.B.Obj;
c: C.Obj;
table Table {
here: Table;
a: A.Table;
a_a: A.A.Table;
a_b: A.B.Table;
b: B.Table;
b_a: B.A.Table;
b_b: B.B.Table;
c: C.Table;
struct_here: Struct;
struct_a: A.Struct;
struct_a_a: A.A.Struct;
struct_a_b: A.B.Struct;
struct_b: B.Struct;
struct_b_a: B.A.Struct;
struct_b_b: B.B.Struct;
struct_c: C.Struct;
}

table Struct {
here: Struct;
a: A.Struct;
a_a: A.A.Struct;
a_b: A.B.Struct;
b: B.Struct;
b_a: B.A.Struct;
b_b: B.B.Struct;
c: C.Struct;
}

union Union {
Table,
A.Table,
A.A.Table,
A.B.Table,
B.Table,
B.A.Table,
B.B.Table,
C.Table
}
48 changes: 39 additions & 9 deletions test/files/valid/include_helpers/A_B.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,43 @@ include "../include_test2.fbs";

namespace A.B;

table Obj {
here: Obj;
a: A.Obj;
a_a: A.A.Obj;
a_b: A.B.Obj;
b: B.Obj;
b_a: B.A.Obj;
b_b: B.B.Obj;
c: C.Obj;
table Table {
here: Table;
a: A.Table;
a_a: A.A.Table;
a_b: A.B.Table;
b: B.Table;
b_a: B.A.Table;
b_b: B.B.Table;
c: C.Table;
struct_here: Struct;
struct_a: A.Struct;
struct_a_a: A.A.Struct;
struct_a_b: A.B.Struct;
struct_b: B.Struct;
struct_b_a: B.A.Struct;
struct_b_b: B.B.Struct;
struct_c: C.Struct;
}

table Struct {
here: Struct;
a: A.Struct;
a_a: A.A.Struct;
a_b: A.B.Struct;
b: B.Struct;
b_a: B.A.Struct;
b_b: B.B.Struct;
c: C.Struct;
}

union Union {
Table,
A.Table,
A.A.Table,
A.B.Table,
B.Table,
B.A.Table,
B.B.Table,
C.Table
}
48 changes: 39 additions & 9 deletions test/files/valid/include_helpers/B.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,43 @@ include "../include_test2.fbs";

namespace B;

table Obj {
here: Obj;
a: A.Obj;
a_a: A.A.Obj;
a_b: A.B.Obj;
b: B.Obj;
b_a: B.A.Obj;
b_b: B.B.Obj;
c: C.Obj;
table Table {
here: Table;
a: A.Table;
a_a: A.A.Table;
a_b: A.B.Table;
b: B.Table;
b_a: B.A.Table;
b_b: B.B.Table;
c: C.Table;
struct_here: Struct;
struct_a: A.Struct;
struct_a_a: A.A.Struct;
struct_a_b: A.B.Struct;
struct_b: B.Struct;
struct_b_a: B.A.Struct;
struct_b_b: B.B.Struct;
struct_c: C.Struct;
}

table Struct {
here: Struct;
a: A.Struct;
a_a: A.A.Struct;
a_b: A.B.Struct;
b: B.Struct;
b_a: B.A.Struct;
b_b: B.B.Struct;
c: C.Struct;
}

union Union {
Table,
A.Table,
A.A.Table,
A.B.Table,
B.Table,
B.A.Table,
B.B.Table,
C.Table
}
48 changes: 39 additions & 9 deletions test/files/valid/include_helpers/B_A.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,43 @@ include "../include_test2.fbs";

namespace B.A;

table Obj {
here: Obj;
a: A.Obj;
a_a: A.A.Obj;
a_b: A.B.Obj;
b: B.Obj;
b_a: B.A.Obj;
b_b: B.B.Obj;
c: C.Obj;
table Table {
here: Table;
a: A.Table;
a_a: A.A.Table;
a_b: A.B.Table;
b: B.Table;
b_a: B.A.Table;
b_b: B.B.Table;
c: C.Table;
struct_here: Struct;
struct_a: A.Struct;
struct_a_a: A.A.Struct;
struct_a_b: A.B.Struct;
struct_b: B.Struct;
struct_b_a: B.A.Struct;
struct_b_b: B.B.Struct;
struct_c: C.Struct;
}

table Struct {
here: Struct;
a: A.Struct;
a_a: A.A.Struct;
a_b: A.B.Struct;
b: B.Struct;
b_a: B.A.Struct;
b_b: B.B.Struct;
c: C.Struct;
}

union Union {
Table,
A.Table,
A.A.Table,
A.B.Table,
B.Table,
B.A.Table,
B.B.Table,
C.Table
}
48 changes: 39 additions & 9 deletions test/files/valid/include_helpers/B_B.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,43 @@ include "../include_test2.fbs";

namespace B.B;

table Obj {
here: Obj;
a: A.Obj;
a_a: A.A.Obj;
a_b: A.B.Obj;
b: B.Obj;
b_a: B.A.Obj;
b_b: B.B.Obj;
c: C.Obj;
table Table {
here: Table;
a: A.Table;
a_a: A.A.Table;
a_b: A.B.Table;
b: B.Table;
b_a: B.A.Table;
b_b: B.B.Table;
c: C.Table;
struct_here: Struct;
struct_a: A.Struct;
struct_a_a: A.A.Struct;
struct_a_b: A.B.Struct;
struct_b: B.Struct;
struct_b_a: B.A.Struct;
struct_b_b: B.B.Struct;
struct_c: C.Struct;
}

table Struct {
here: Struct;
a: A.Struct;
a_a: A.A.Struct;
a_b: A.B.Struct;
b: B.Struct;
b_a: B.A.Struct;
b_b: B.B.Struct;
c: C.Struct;
}

union Union {
Table,
A.Table,
A.A.Table,
A.B.Table,
B.Table,
B.A.Table,
B.B.Table,
C.Table
}
Loading

0 comments on commit b871e31

Please sign in to comment.