@@ -5,7 +5,10 @@ mod impls;
55#[ cfg( feature = "serde" ) ]
66pub mod serde;
77
8+ use crate :: { algebraic_value:: ser:: ValueSerializer , bsatn, buffer:: BufWriter , AlgebraicType } ;
89use core:: fmt;
10+ use ethnum:: { i256, u256} ;
11+ pub use spacetimedb_bindings_macro:: Serialize ;
912
1013/// A data format that can deserialize any data structure supported by SATs.
1114///
@@ -128,7 +131,18 @@ pub trait Serializer: Sized {
128131 ///
129132 /// - `AlgebraicValue::decode(ty, &mut bsatn).is_ok()`.
130133 /// That is, `bsatn` encodes a valid element of `ty`.
131- unsafe fn serialize_bsatn ( self , ty : & AlgebraicType , bsatn : & [ u8 ] ) -> Result < Self :: Ok , Self :: Error > ;
134+ unsafe fn serialize_bsatn ( self , ty : & AlgebraicType , bsatn : & [ u8 ] ) -> Result < Self :: Ok , Self :: Error > {
135+ // TODO(Centril): Consider instead deserializing the `bsatn` through a
136+ // deserializer that serializes into `self` directly.
137+
138+ // First convert the BSATN to an `AlgebraicValue`.
139+ // SAFETY: Forward caller requirements of this method to that we are calling.
140+ let res = unsafe { ValueSerializer . serialize_bsatn ( ty, bsatn) } ;
141+ let value = res. unwrap_or_else ( |x| match x { } ) ;
142+
143+ // Then serialize that.
144+ value. serialize ( self )
145+ }
132146
133147 /// Serialize the given `bsatn` encoded data of type `ty`.
134148 ///
@@ -160,7 +174,19 @@ pub trait Serializer: Sized {
160174 ty : & AlgebraicType ,
161175 total_bsatn_len : usize ,
162176 bsatn : I ,
163- ) -> Result < Self :: Ok , Self :: Error > ;
177+ ) -> Result < Self :: Ok , Self :: Error > {
178+ // TODO(Centril): Unlike above, in this case we must at minimum concatenate `bsatn`
179+ // before we can do the piping mentioned above, but that's better than
180+ // serializing to `AlgebraicValue` first, so consider that.
181+
182+ // First convert the BSATN to an `AlgebraicValue`.
183+ // SAFETY: Forward caller requirements of this method to that we are calling.
184+ let res = unsafe { ValueSerializer . serialize_bsatn_in_chunks ( ty, total_bsatn_len, bsatn) } ;
185+ let value = res. unwrap_or_else ( |x| match x { } ) ;
186+
187+ // Then serialize that.
188+ value. serialize ( self )
189+ }
164190
165191 /// Serialize the given `string`.
166192 ///
@@ -194,14 +220,18 @@ pub trait Serializer: Sized {
194220 self ,
195221 total_len : usize ,
196222 string : I ,
197- ) -> Result < Self :: Ok , Self :: Error > ;
223+ ) -> Result < Self :: Ok , Self :: Error > {
224+ // First convert the `string` to an `AlgebraicValue`.
225+ // SAFETY: Forward caller requirements of this method to that we are calling.
226+ let res = unsafe { ValueSerializer . serialize_str_in_chunks ( total_len, string) } ;
227+ let value = res. unwrap_or_else ( |x| match x { } ) ;
228+
229+ // Then serialize that.
230+ // This incurs a very minor cost of branching on `AlgebraicValue::String`.
231+ value. serialize ( self )
232+ }
198233}
199234
200- use ethnum:: { i256, u256} ;
201- pub use spacetimedb_bindings_macro:: Serialize ;
202-
203- use crate :: { bsatn, buffer:: BufWriter , AlgebraicType } ;
204-
205235/// A **data structure** that can be serialized into any data format supported by
206236/// the SpacetimeDB Algebraic Type System.
207237///
0 commit comments