Skip to content

Commit

Permalink
Implement new builder pattern for serializing tables and unions (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
TethysSvensson authored Dec 3, 2022
1 parent 823e7ce commit 1835799
Show file tree
Hide file tree
Showing 17 changed files with 919 additions and 77 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- \[Rust\]: Added more `impl`s for union and struct references
- \[Rust\]: Add an `ENUM_VALUES` const to enums
- \[Rust\]: Make `Vector` more similar to rust slices by adding more methods
Expand All @@ -13,9 +14,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
behind the `vtable-cache`, `bytes-cache` and `string-cache` feature flags
(they are enabled by default)
- \[Rust\]: Bump the Minimum Support Rust Version (MSRV) to 1.61.0.
- \[Rust\]: Add license files to crates
- \[Rust\]: Implement a builder pattern for serializing tables and unions
- Add support for docstrings, and add them to the Rust output.
- Update the `README` with information about our Discord server.
- \[Rust\]: Add license files to crates

### Removed
- \[Rust\]: The old ways of serializing tables and unions using `create`-functions have been removed.

## [0.3.1] - 2022-06-15
### Added
Expand Down
38 changes: 20 additions & 18 deletions examples/rust/examples/api_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,29 @@ fn main() {
let monster_offset = monster.prepare(&mut builder);
let _finished_data = builder.finish(monster_offset, None);

// To avoid using the rust heap for objects and additionally allow sharing of data, use `create` methods instead:
// To avoid using the rust heap for objects and additionally allow sharing of data, use the builder API instead:
let weapons = [
Weapon::create(&mut builder, "Sword", 3),
Weapon::create(&mut builder, "Axe", 5),
Weapon::builder()
.name("Sword")
.damage(3)
.finish(&mut builder),
Weapon::builder().name("Axe").damage(5).finish(&mut builder),
];
let equipped = Equipment::create_weapon(&mut builder, weapons[1]);
let monster = Monster::create(
&mut builder,
Vec3 {
let equipped = Equipment::builder().weapon(weapons[1]).finish(&mut builder);
let monster = Monster::builder()
.pos(Vec3 {
x: 1.0,
y: 2.0,
z: 3.0,
},
150,
80,
"Orc",
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
Color::Red,
weapons,
equipped,
[
})
.mana(150)
.hp(80)
.name("Orc")
.inventory([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
.color(Color::Red)
.weapons(weapons)
.equipped(equipped)
.path([
Vec3 {
x: 1.0,
y: 2.0,
Expand All @@ -86,8 +88,8 @@ fn main() {
y: 5.0,
z: 6.0,
},
],
);
])
.finish(&mut builder);

let finished_data = builder.finish(monster, None);

Expand Down
Loading

0 comments on commit 1835799

Please sign in to comment.