pub mod decode;
pub mod encode;
mod tuple;
use crate::protofile::DescribeField;
use crate::protofile::DescribeMessage;
use crate::protofile::FieldType;
use crate::protofile::MessageDescription;
pub struct TableEncoder;
pub trait DescribeTable {
const DESCRIPTION: MessageDescription<'static>;
}
impl<T: DescribeTable> DescribeMessage<T> for TableEncoder {
const DESCRIPTION: MessageDescription<'static> = T::DESCRIPTION;
}
impl<T: DescribeTable> DescribeField<T> for TableEncoder {
const FIELD_TYPE: FieldType<'static> = FieldType::message(|| T::DESCRIPTION);
}
pub unsafe trait StructMetadata {
const NUMBERS: &'static [u32]; const OFFSETS: &'static [usize]; }
#[cfg(test)]
#[allow(clippy::undocumented_unsafe_blocks)]
mod tests {
use super::decode::ErasedDecoderEntry;
use super::decode::StructDecodeMetadata;
use super::encode::ErasedEncoderEntry;
use super::encode::StructEncodeMetadata;
use super::StructMetadata;
use super::TableEncoder;
use crate::encoding::StringField;
use crate::encoding::VarintField;
use crate::FieldDecode;
use crate::FieldEncode;
use core::mem::offset_of;
struct Foo<'a> {
a: u32,
b: u64,
x: &'a str,
}
unsafe impl<'a> StructMetadata for Foo<'a> {
const NUMBERS: &'static [u32] = &[1, 2, 3];
const OFFSETS: &'static [usize] = &[
offset_of!(Foo<'a>, a),
offset_of!(Foo<'a>, b),
offset_of!(Foo<'a>, x),
];
}
unsafe impl<'a, R> StructEncodeMetadata<R> for Foo<'a> {
const ENCODERS: &'static [ErasedEncoderEntry] = &[
<VarintField as FieldEncode<u32, R>>::ENTRY.erase(),
<VarintField as FieldEncode<u64, R>>::ENTRY.erase(),
<StringField as FieldEncode<&'a str, R>>::ENTRY.erase(),
];
}
unsafe impl<'de, R> StructDecodeMetadata<'de, R> for Foo<'de> {
const DECODERS: &'static [ErasedDecoderEntry] = &[
<VarintField as FieldDecode<'de, u32, R>>::ENTRY.erase(),
<VarintField as FieldDecode<'de, u64, R>>::ENTRY.erase(),
<StringField as FieldDecode<'de, &'de str, R>>::ENTRY.erase(),
];
}
impl<'a> crate::DefaultEncoding for Foo<'a> {
type Encoding = TableEncoder;
}
#[test]
fn test_derived_macro() {
let data = crate::encode(Foo {
a: 1,
b: 2,
x: "hi",
});
let foo = crate::decode::<Foo<'_>>(&data).unwrap();
assert_eq!(foo.a, 1);
assert_eq!(foo.b, 2);
assert_eq!(foo.x, "hi");
}
}