Skip to content

Commit 8dca820

Browse files
committed
sats: provide some 'Serialize' method bodies
1 parent 6a8cd7e commit 8dca820

3 files changed

Lines changed: 41 additions & 111 deletions

File tree

crates/sats/src/satn.rs

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
use crate::time_duration::TimeDuration;
22
use crate::timestamp::Timestamp;
3-
use crate::{
4-
algebraic_value::ser::ValueSerializer,
5-
ser::{self, Serialize},
6-
ProductType, ProductTypeElement,
7-
};
83
use crate::{i256, u256};
4+
use crate::{ser, ProductType, ProductTypeElement};
95
use core::fmt;
106
use core::fmt::Write as _;
117
use derive_more::{From, Into};
128

13-
/// An extension trait for [`Serialize`] providing formatting methods.
9+
/// An extension trait for [`Serialize`](ser::Serialize) providing formatting methods.
1410
pub trait Satn: ser::Serialize {
1511
/// Formats the value using the SATN data format into the formatter `f`.
1612
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -349,53 +345,6 @@ impl<'a, 'f> ser::Serializer for SatnFormatter<'a, 'f> {
349345
})?;
350346
write!(self, ")")
351347
}
352-
353-
unsafe fn serialize_bsatn(self, ty: &crate::AlgebraicType, bsatn: &[u8]) -> Result<Self::Ok, Self::Error> {
354-
// TODO(Centril): Consider instead deserializing the `bsatn` through a
355-
// deserializer that serializes into `self` directly.
356-
357-
// First convert the BSATN to an `AlgebraicValue`.
358-
// SAFETY: Forward caller requirements of this method to that we are calling.
359-
let res = unsafe { ValueSerializer.serialize_bsatn(ty, bsatn) };
360-
let value = res.unwrap_or_else(|x| match x {});
361-
362-
// Then serialize that.
363-
value.serialize(self)
364-
}
365-
366-
unsafe fn serialize_bsatn_in_chunks<'c, I: Clone + Iterator<Item = &'c [u8]>>(
367-
self,
368-
ty: &crate::AlgebraicType,
369-
total_bsatn_len: usize,
370-
bsatn: I,
371-
) -> Result<Self::Ok, Self::Error> {
372-
// TODO(Centril): Unlike above, in this case we must at minimum concatenate `bsatn`
373-
// before we can do the piping mentioned above, but that's better than
374-
// serializing to `AlgebraicValue` first, so consider that.
375-
376-
// First convert the BSATN to an `AlgebraicValue`.
377-
// SAFETY: Forward caller requirements of this method to that we are calling.
378-
let res = unsafe { ValueSerializer.serialize_bsatn_in_chunks(ty, total_bsatn_len, bsatn) };
379-
let value = res.unwrap_or_else(|x| match x {});
380-
381-
// Then serialize that.
382-
value.serialize(self)
383-
}
384-
385-
unsafe fn serialize_str_in_chunks<'c, I: Clone + Iterator<Item = &'c [u8]>>(
386-
self,
387-
total_len: usize,
388-
string: I,
389-
) -> Result<Self::Ok, Self::Error> {
390-
// First convert the `string` to an `AlgebraicValue`.
391-
// SAFETY: Forward caller requirements of this method to that we are calling.
392-
let res = unsafe { ValueSerializer.serialize_str_in_chunks(total_len, string) };
393-
let value = res.unwrap_or_else(|x| match x {});
394-
395-
// Then serialize that.
396-
// This incurs a very minor cost of branching on `AlgebraicValue::String`.
397-
value.serialize(self)
398-
}
399348
}
400349

401350
/// Defines the SATN formatting for arrays.

crates/sats/src/ser.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ mod impls;
55
#[cfg(feature = "serde")]
66
pub mod serde;
77

8+
use crate::{algebraic_value::ser::ValueSerializer, bsatn, buffer::BufWriter, AlgebraicType};
89
use 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
///

crates/sats/src/ser/serde.rs

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use super::Serialize as _;
1+
use crate::{i256, u256};
22
use crate::{
3-
algebraic_value::ser::ValueSerializer,
43
ser::{self, Serializer},
54
serde::{SerdeError, SerdeWrapper},
65
};
7-
use crate::{i256, u256};
86
use core::fmt;
97
use serde::ser as serde;
108

@@ -123,53 +121,6 @@ impl<S: serde::Serializer> Serializer for SerdeSerializer<S> {
123121
seq.end().map_err(SerdeError)
124122
}
125123
}
126-
127-
unsafe fn serialize_bsatn(self, ty: &crate::AlgebraicType, bsatn: &[u8]) -> Result<Self::Ok, Self::Error> {
128-
// TODO(Centril): Consider instead deserializing the `bsatn` through a
129-
// deserializer that serializes into `self` directly.
130-
131-
// First convert the BSATN to an `AlgebraicValue`.
132-
// SAFETY: Forward caller requirements of this method to that we are calling.
133-
let res = unsafe { ValueSerializer.serialize_bsatn(ty, bsatn) };
134-
let value = res.unwrap_or_else(|x| match x {});
135-
136-
// Then serialize that.
137-
value.serialize(self)
138-
}
139-
140-
unsafe fn serialize_bsatn_in_chunks<'a, I: Clone + Iterator<Item = &'a [u8]>>(
141-
self,
142-
ty: &crate::AlgebraicType,
143-
total_bsatn_len: usize,
144-
bsatn: I,
145-
) -> Result<Self::Ok, Self::Error> {
146-
// TODO(Centril): Unlike above, in this case we must at minimum concatenate `bsatn`
147-
// before we can do the piping mentioned above, but that's better than
148-
// serializing to `AlgebraicValue` first, so consider that.
149-
150-
// First convert the BSATN to an `AlgebraicValue`.
151-
// SAFETY: Forward caller requirements of this method to that we are calling.
152-
let res = unsafe { ValueSerializer.serialize_bsatn_in_chunks(ty, total_bsatn_len, bsatn) };
153-
let value = res.unwrap_or_else(|x| match x {});
154-
155-
// Then serialize that.
156-
value.serialize(self)
157-
}
158-
159-
unsafe fn serialize_str_in_chunks<'a, I: Clone + Iterator<Item = &'a [u8]>>(
160-
self,
161-
total_len: usize,
162-
string: I,
163-
) -> Result<Self::Ok, Self::Error> {
164-
// First convert the `string` to an `AlgebraicValue`.
165-
// SAFETY: Forward caller requirements of this method to that we are calling.
166-
let res = unsafe { ValueSerializer.serialize_str_in_chunks(total_len, string) };
167-
let value = res.unwrap_or_else(|x| match x {});
168-
169-
// Then serialize that.
170-
// This incurs a very minor cost of branching on `AlgebraicValue::String`.
171-
value.serialize(self)
172-
}
173124
}
174125

175126
/// Serializes array elements by forwarding to `S: serde::SerializeSeq`.

0 commit comments

Comments
 (0)