diff options
author | Matt Brubeck <mbrubeck@limpet.net> | 2020-06-08 09:47:36 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-08 09:47:36 -0700 |
commit | 53fb453e047367434627ff4cb7625c10e24fd2e3 (patch) | |
tree | d6d1307d5e3e57bce44332ba8b52031201676a51 /rust | |
parent | 17c1f35fa0f1b73744453b1718aa4f77fd4b3cc1 (diff) | |
download | flatbuffers-53fb453e047367434627ff4cb7625c10e24fd2e3.tar.gz flatbuffers-53fb453e047367434627ff4cb7625c10e24fd2e3.tar.bz2 flatbuffers-53fb453e047367434627ff4cb7625c10e24fd2e3.zip |
[rust] Add FlatBufferBuilder::force_defaults API (#5946)
* [rust] Add force_defaults method FlatBufferBuilder
This works just like the same method already available in other
languages.
* Add binary format test for force_defaults
Diffstat (limited to 'rust')
-rw-r--r-- | rust/flatbuffers/src/builder.rs | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/rust/flatbuffers/src/builder.rs b/rust/flatbuffers/src/builder.rs index 0b20f9ee..702b07cb 100644 --- a/rust/flatbuffers/src/builder.rs +++ b/rust/flatbuffers/src/builder.rs @@ -52,6 +52,7 @@ pub struct FlatBufferBuilder<'fbb> { finished: bool, min_align: usize, + force_defaults: bool, _phantom: PhantomData<&'fbb ()>, } @@ -85,6 +86,7 @@ impl<'fbb> FlatBufferBuilder<'fbb> { finished: false, min_align: 0, + force_defaults: false, _phantom: PhantomData, } @@ -148,10 +150,9 @@ impl<'fbb> FlatBufferBuilder<'fbb> { #[inline] pub fn push_slot<X: Push + PartialEq>(&mut self, slotoff: VOffsetT, x: X, default: X) { self.assert_nested("push_slot"); - if x == default { - return; + if x != default || self.force_defaults { + self.push_slot_always(slotoff, x); } - self.push_slot_always(slotoff, x); } /// Push a Push'able value onto the front of the in-progress data, and @@ -327,6 +328,18 @@ impl<'fbb> FlatBufferBuilder<'fbb> { WIPOffset::new(self.push::<UOffsetT>(items.len() as UOffsetT).value()) } + /// Set whether default values are stored. + /// + /// In order to save space, fields that are set to their default value + /// aren't stored in the buffer. Setting `force_defaults` to `true` + /// disables this optimization. + /// + /// By default, `force_defaults` is `false`. + #[inline] + pub fn force_defaults(&mut self, force_defaults: bool) { + self.force_defaults = force_defaults; + } + /// Get the byte slice for the data that has been written, regardless of /// whether it has been finished. #[inline] |