Trait spacetimedb_sats::ser::Serializer
source · pub trait Serializer: Sized {
type Ok;
type Error: Error;
type SerializeArray: SerializeArray<Ok = Self::Ok, Error = Self::Error>;
type SerializeMap: SerializeMap<Ok = Self::Ok, Error = Self::Error>;
type SerializeSeqProduct: SerializeSeqProduct<Ok = Self::Ok, Error = Self::Error>;
type SerializeNamedProduct: SerializeNamedProduct<Ok = Self::Ok, Error = Self::Error>;
Show 23 methods
// Required methods
fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error>;
fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error>;
fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error>;
fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error>;
fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error>;
fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error>;
fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error>;
fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error>;
fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error>;
fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error>;
fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error>;
fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error>;
fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error>;
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error>;
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error>;
fn serialize_array(
self,
len: usize
) -> Result<Self::SerializeArray, Self::Error>;
fn serialize_map(
self,
len: usize
) -> Result<Self::SerializeMap, Self::Error>;
fn serialize_seq_product(
self,
len: usize
) -> Result<Self::SerializeSeqProduct, Self::Error>;
fn serialize_named_product(
self,
len: usize
) -> Result<Self::SerializeNamedProduct, Self::Error>;
fn serialize_variant<T: Serialize + ?Sized>(
self,
tag: u8,
name: Option<&str>,
value: &T
) -> Result<Self::Ok, Self::Error>;
unsafe fn serialize_bsatn(
self,
ty: &AlgebraicType,
bsatn: &[u8]
) -> Result<Self::Ok, Self::Error>;
unsafe fn serialize_bsatn_in_chunks<'a, I: Clone + Iterator<Item = &'a [u8]>>(
self,
ty: &AlgebraicType,
total_bsatn_len: usize,
bsatn: I
) -> Result<Self::Ok, Self::Error>;
unsafe fn serialize_str_in_chunks<'a, I: Clone + Iterator<Item = &'a [u8]>>(
self,
total_len: usize,
string: I
) -> Result<Self::Ok, Self::Error>;
}
Expand description
A data format that can deserialize any data structure supported by SATs.
The Serializer
trait in SATS performs the same function as serde::Serializer
in serde
.
See the documentation of serde::Serializer
for more information of the data model.
Required Associated Types§
sourcetype Ok
type Ok
The output type produced by this Serializer
during successful serialization.
Most serializers that produce text or binary output should set Ok = ()
and serialize into an io::Write
or buffer contained within the Serializer
instance.
Serializers that build in-memory data structures may be simplified by using Ok
to propagate
the data structure around.
sourcetype SerializeArray: SerializeArray<Ok = Self::Ok, Error = Self::Error>
type SerializeArray: SerializeArray<Ok = Self::Ok, Error = Self::Error>
Type returned from serialize_array
for serializing the contents of the array.
sourcetype SerializeMap: SerializeMap<Ok = Self::Ok, Error = Self::Error>
type SerializeMap: SerializeMap<Ok = Self::Ok, Error = Self::Error>
Type returned from serialize_map
for serializing the contents of the map.
sourcetype SerializeSeqProduct: SerializeSeqProduct<Ok = Self::Ok, Error = Self::Error>
type SerializeSeqProduct: SerializeSeqProduct<Ok = Self::Ok, Error = Self::Error>
Type returned from serialize_seq_product
for serializing the contents of the unnamed product.
sourcetype SerializeNamedProduct: SerializeNamedProduct<Ok = Self::Ok, Error = Self::Error>
type SerializeNamedProduct: SerializeNamedProduct<Ok = Self::Ok, Error = Self::Error>
Type returned from serialize_named_product
for serializing the contents of the named product.
Required Methods§
sourcefn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error>
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error>
Serialize a &str
string slice.
sourcefn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error>
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error>
Serialize a &[u8]
byte slice.
sourcefn serialize_array(
self,
len: usize
) -> Result<Self::SerializeArray, Self::Error>
fn serialize_array( self, len: usize ) -> Result<Self::SerializeArray, Self::Error>
Begin to serialize a variably sized array.
This call must be followed by zero or more calls to SerializeArray::serialize_element
,
then a call to SerializeArray::end
.
The argument is the number of elements in the sequence.
sourcefn serialize_map(self, len: usize) -> Result<Self::SerializeMap, Self::Error>
fn serialize_map(self, len: usize) -> Result<Self::SerializeMap, Self::Error>
Begin to serialize a variably sized map.
This call must be followed by zero or more calls to [SerializeMap::serialize_element
],
then a call to SerializeMap::end
.
The argument is the number of elements in the map.
sourcefn serialize_seq_product(
self,
len: usize
) -> Result<Self::SerializeSeqProduct, Self::Error>
fn serialize_seq_product( self, len: usize ) -> Result<Self::SerializeSeqProduct, Self::Error>
Begin to serialize a product with unnamed fields.
This call must be followed by zero or more calls to SerializeSeqProduct::serialize_element
,
then a call to SerializeSeqProduct::end
.
The argument is the number of fields in the product.
sourcefn serialize_named_product(
self,
len: usize
) -> Result<Self::SerializeNamedProduct, Self::Error>
fn serialize_named_product( self, len: usize ) -> Result<Self::SerializeNamedProduct, Self::Error>
Begin to serialize a product with named fields.
This call must be followed by zero or more calls to SerializeNamedProduct::serialize_element
,
then a call to SerializeNamedProduct::end
.
The argument is the number of fields in the product.
sourcefn serialize_variant<T: Serialize + ?Sized>(
self,
tag: u8,
name: Option<&str>,
value: &T
) -> Result<Self::Ok, Self::Error>
fn serialize_variant<T: Serialize + ?Sized>( self, tag: u8, name: Option<&str>, value: &T ) -> Result<Self::Ok, Self::Error>
Serialize a sum value provided the chosen tag
, name
, and value
.
sourceunsafe fn serialize_bsatn(
self,
ty: &AlgebraicType,
bsatn: &[u8]
) -> Result<Self::Ok, Self::Error>
unsafe fn serialize_bsatn( self, ty: &AlgebraicType, bsatn: &[u8] ) -> Result<Self::Ok, Self::Error>
Serialize the given bsatn
encoded data of type ty
.
This is a concession to performance, allowing some implementations to write the buffer directly.
§Safety
AlgebraicValue::decode(ty, &mut bsatn).is_ok()
. That is,bsatn
encodes a valid element ofty
.
sourceunsafe fn serialize_bsatn_in_chunks<'a, I: Clone + Iterator<Item = &'a [u8]>>(
self,
ty: &AlgebraicType,
total_bsatn_len: usize,
bsatn: I
) -> Result<Self::Ok, Self::Error>
unsafe fn serialize_bsatn_in_chunks<'a, I: Clone + Iterator<Item = &'a [u8]>>( self, ty: &AlgebraicType, total_bsatn_len: usize, bsatn: I ) -> Result<Self::Ok, Self::Error>
Serialize the given bsatn
encoded data of type ty
.
The data is provided as an iterator of chunks, at arbitrary boundaries,
with a total concatenated length of total_bsatn_len
which callers can assume.
An implementation of this method is semantically the same as:
let mut buf = Vec::new();
for chunk in bsatn {
buf.extend(chunk);
}
ser.serialize_bsatn(&buf);
This method is a concession to performance, allowing some implementations to write the buffer directly.
The parameter I
is required to be Clone
only for debug_assert!
purposes.
§Safety
total_bsatn_len == bsatn.map(|c| c.len()).sum() <= isize::MAX
- Let
buf
be defined as above, i.e., the bytes ofbsatn
concatenated. ThenAlgebraicValue::decode(ty, &mut buf).is_ok()
. That is,buf
encodes a valid element ofty
.
sourceunsafe fn serialize_str_in_chunks<'a, I: Clone + Iterator<Item = &'a [u8]>>(
self,
total_len: usize,
string: I
) -> Result<Self::Ok, Self::Error>
unsafe fn serialize_str_in_chunks<'a, I: Clone + Iterator<Item = &'a [u8]>>( self, total_len: usize, string: I ) -> Result<Self::Ok, Self::Error>
Serialize the given string
.
The string is provided as an iterator of chunks, at arbitrary boundaries,
with a total concatenated length of total_len
which callers can trust.
An implementation of this method is semantically the same as:
let mut buf = Vec::new();
for chunk in string {
buf.extend(chunk);
}
let str = unsafe { core::str::from_utf8_unchecked(&buf) };
ser.serialize_str(str);
This method is a concession to performance, allowing some implementations to write the buffer directly.
The parameter I
is required to be Clone
only for debug_assert!
purposes.
§Safety
total_len == string.map(|c| c.len()).sum() <= isize::MAX
- Let
buf
be the bytes ofstring
concatenated. Thencore::str::from_utf8(&buf).is_ok()
. That is,buf
is valid UTF-8. Note however that individual chunks need not be valid UTF-8, as multi-byte characters may be split across chunk boundaries.