blob: d22b47f1080c2817dce62ba7b8222ae7d02591f6 [file] [log] [blame]
Austin Schuh272c6132020-11-14 16:37:52 -08001// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::builder::Builder;
16use crate::private::Sealed;
17use crate::{Blob, IndirectFloat, IndirectInt, IndirectUInt};
18
19impl<'a> Sealed for Blob<'a> {}
20impl Sealed for () {}
21
22// TODO: String interning
23// TODO: Pushable for Map types?
24
25/// Types that implement the Pushable trait can be written into a Flexbuffer.
26///
27/// All Rust's standard numbers, `u8, u16, u32, u64, i8, i16, i32, i64, f32, f64`,
28/// can all be pushed. They are `FlexBufferType::{UInt, Int, Float}`.
29/// Flexbuffers chooses the smallest width that can represent the given number.
30/// Strings can pe pushed, they become `FlexBufferType::String` and are stored
31/// with both a length and null terminator.
32///
33/// * For convenience and speed push typed vectors using rust arrays and slices.
34/// Doing so will immediately serialize the data, skipping the `Builder`'s
35/// internal cache.
36///
37/// * Pushable cannot not be implemented by any downstream crates.
38pub trait Pushable: Sealed + Sized {
39 fn push_to_builder(self, _: &mut Builder) {}
40}
41
42impl Pushable for () {
43 fn push_to_builder(self, builder: &mut Builder) {
44 builder.push_null();
45 }
46}
47impl<'a> Pushable for Blob<'a> {
48 fn push_to_builder(self, builder: &mut Builder) {
49 builder.push_blob(self.0);
50 }
51}
52
53macro_rules! forward_to_builder {
54 ($T: ty, $method: ident) => {
55 impl Sealed for $T {}
56 impl Pushable for $T {
57 fn push_to_builder(self, builder: &mut Builder) {
58 builder.$method(self);
59 }
60 }
61 };
62 ($T: ty, $method: ident, $asT: ty) => {
63 impl Sealed for $T {}
64 impl Pushable for $T {
65 fn push_to_builder(self, builder: &mut Builder) {
66 builder.$method(self as $asT);
67 }
68 }
69 };
70}
71forward_to_builder!(&str, push_str);
72forward_to_builder!(bool, push_bool);
73forward_to_builder!(u8, push_uint);
74forward_to_builder!(u16, push_uint);
75forward_to_builder!(u32, push_uint);
76forward_to_builder!(u64, push_uint);
77forward_to_builder!(i8, push_int);
78forward_to_builder!(i16, push_int);
79forward_to_builder!(i32, push_int);
80forward_to_builder!(i64, push_int);
81forward_to_builder!(f32, push_float);
82forward_to_builder!(f64, push_float);
83forward_to_builder!(&[u8], push_uints);
84forward_to_builder!(&[u16], push_uints);
85forward_to_builder!(&[u32], push_uints);
86forward_to_builder!(&[u64], push_uints);
87forward_to_builder!(&[i8], push_ints);
88forward_to_builder!(&[i16], push_ints);
89forward_to_builder!(&[i32], push_ints);
90forward_to_builder!(&[i64], push_ints);
91forward_to_builder!(&[f32], push_floats);
92forward_to_builder!(&[f64], push_floats);
93forward_to_builder!(&[bool], push_bools);
94forward_to_builder!(&Vec<u8>, push_uints);
95forward_to_builder!(&Vec<u16>, push_uints);
96forward_to_builder!(&Vec<u32>, push_uints);
97forward_to_builder!(&Vec<u64>, push_uints);
98forward_to_builder!(&Vec<i8>, push_ints);
99forward_to_builder!(&Vec<i16>, push_ints);
100forward_to_builder!(&Vec<i32>, push_ints);
101forward_to_builder!(&Vec<i64>, push_ints);
102forward_to_builder!(&Vec<f32>, push_floats);
103forward_to_builder!(&Vec<f64>, push_floats);
104forward_to_builder!(&Vec<bool>, push_bools);
105
106macro_rules! impl_indirects {
107 ($Indirect: ident, $method: ident) => {
108 impl Sealed for $Indirect {}
109 impl Pushable for $Indirect {
110 fn push_to_builder(self, builder: &mut Builder) {
111 builder.$method(self.0);
112 }
113 }
114 };
115}
116impl_indirects!(IndirectInt, push_indirect_int);
117impl_indirects!(IndirectUInt, push_indirect_uint);
118impl_indirects!(IndirectFloat, push_indirect_float);
119
120macro_rules! impl_arrays {
121 ($num: expr) => {
122 forward_to_builder!(&[u8; $num], push_uints, &[u8]);
123 forward_to_builder!(&[u16; $num], push_uints, &[u16]);
124 forward_to_builder!(&[u32; $num], push_uints, &[u32]);
125 forward_to_builder!(&[u64; $num], push_uints, &[u64]);
126 forward_to_builder!(&[i8; $num], push_ints, &[i8]);
127 forward_to_builder!(&[i16; $num], push_ints, &[i16]);
128 forward_to_builder!(&[i32; $num], push_ints, &[i32]);
129 forward_to_builder!(&[i64; $num], push_ints, &[i64]);
130 forward_to_builder!(&[f32; $num], push_floats, &[f32]);
131 forward_to_builder!(&[f64; $num], push_floats, &[f64]);
132 forward_to_builder!(&[bool; $num], push_bools, &[bool]);
133 };
134}
135impl_arrays!(0);
136impl_arrays!(1);
137impl_arrays!(2);
138impl_arrays!(3);
139impl_arrays!(4);
140impl_arrays!(5);
141impl_arrays!(6);
142// impl_arrays!(7);
143// impl_arrays!(8);
144// impl_arrays!(9);
145// impl_arrays!(10);
146// impl_arrays!(11);
147// impl_arrays!(12);
148// impl_arrays!(13);
149// impl_arrays!(14);
150// impl_arrays!(15);
151// impl_arrays!(16);
152// impl_arrays!(17);
153// impl_arrays!(18);
154// impl_arrays!(19);
155// impl_arrays!(20);
156// impl_arrays!(21);
157// impl_arrays!(22);
158// impl_arrays!(23);
159// impl_arrays!(24);
160// impl_arrays!(25);
161// impl_arrays!(26);
162// impl_arrays!(27);
163// impl_arrays!(28);
164// impl_arrays!(29);
165// impl_arrays!(30);
166// impl_arrays!(31);
167// impl_arrays!(32);