[Yunq] Create repeated field implementations in rust.
This commit is contained in:
parent
4ee8dc924c
commit
f94f56f9ec
14 changed files with 347 additions and 269 deletions
|
|
@ -6,7 +6,6 @@ edition = "2021"
|
|||
[dependencies]
|
||||
mammoth = { path = "../mammoth" }
|
||||
yunq = {path = "../yunq"}
|
||||
yunq-derive = {path = "../yunq-derive"}
|
||||
|
||||
[build-dependencies]
|
||||
yunqc = {path = "../../../yunq/rust"}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ edition = "2021"
|
|||
mammoth = { path = "../mammoth" }
|
||||
yellowstone-yunq = { path = "../yellowstone" }
|
||||
yunq = {path = "../yunq"}
|
||||
yunq-derive = {path = "../yunq-derive"}
|
||||
|
||||
[build-dependencies]
|
||||
yunqc = {path = "../../../yunq/rust"}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ edition = "2021"
|
|||
mammoth = { path = "../mammoth" }
|
||||
yellowstone-yunq = { path = "../yellowstone" }
|
||||
yunq = {path = "../yunq"}
|
||||
yunq-derive = {path = "../yunq-derive"}
|
||||
|
||||
[build-dependencies]
|
||||
yunqc = {path = "../../../yunq/rust"}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ edition = "2021"
|
|||
[dependencies]
|
||||
mammoth = { path = "../mammoth" }
|
||||
yunq = {path = "../yunq"}
|
||||
yunq-derive = {path = "../yunq-derive"}
|
||||
|
||||
[build-dependencies]
|
||||
yunqc = {path = "../../../yunq/rust"}
|
||||
|
|
|
|||
|
|
@ -1,12 +0,0 @@
|
|||
[package]
|
||||
name = "yunq-derive"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0"
|
||||
quote = "1.0"
|
||||
syn = { version = "1.0", features = ["full", "extra-traits", "printing"] }
|
||||
|
|
@ -1,188 +0,0 @@
|
|||
use proc_macro::{self, TokenStream};
|
||||
use quote::{quote, ToTokens};
|
||||
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields, Ident, Path, Type, TypePath};
|
||||
|
||||
fn serialize_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenStream {
|
||||
if path.is_ident("String") {
|
||||
quote! {
|
||||
{
|
||||
let str_offset = next_extension;
|
||||
let str_length = self.#name.len() as u32;
|
||||
|
||||
buf.write_str_at(offset + next_extension as usize, &self.#name)?;
|
||||
next_extension += str_length;
|
||||
|
||||
buf.write_at(yunq::message::field_offset(offset, #ind), str_offset)?;
|
||||
buf.write_at(yunq::message::field_offset(offset, #ind) + 4, str_length)?;
|
||||
}
|
||||
|
||||
}
|
||||
} else if path.is_ident("z_cap_t") {
|
||||
quote! {
|
||||
{
|
||||
let cap_ind = caps.len();
|
||||
caps.push(self.#name);
|
||||
|
||||
buf.write_at(yunq::message::field_offset(offset, #ind), cap_ind as u64)?;
|
||||
}
|
||||
}
|
||||
} else if path.is_ident("u64") {
|
||||
quote! {
|
||||
{
|
||||
buf.write_at(yunq::message::field_offset(offset, #ind), self.#name)?;
|
||||
}
|
||||
}
|
||||
} else if path.is_ident("i64") {
|
||||
quote! {
|
||||
{
|
||||
buf.write_at(yunq::message::field_offset(offset, #ind), self.#name)?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
{
|
||||
let msg_offset = next_extension;
|
||||
let msg_length = self.#name.serialize(buf, offset + next_extension as usize, caps)? as u32;
|
||||
next_extension += msg_length;
|
||||
|
||||
buf.write_at(yunq::message::field_offset(offset, #ind), msg_offset)?;
|
||||
buf.write_at(yunq::message::field_offset(offset, #ind) + 4, msg_length)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenStream {
|
||||
if path.is_ident("String") {
|
||||
quote! {
|
||||
let #name = {
|
||||
let str_offset = buf.at::<u32>(yunq::message::field_offset(offset, #ind))?;
|
||||
let str_length = buf.at::<u32>(yunq::message::field_offset(offset, #ind) + 4)?;
|
||||
|
||||
buf.str_at(offset + str_offset as usize, str_length as usize)?.to_string()
|
||||
};
|
||||
}
|
||||
} else if path.is_ident("z_cap_t") {
|
||||
quote! {
|
||||
let #name = {
|
||||
let cap_ind = buf.at::<u64>(yunq::message::field_offset(offset, #ind))?;
|
||||
caps[cap_ind as usize]
|
||||
};
|
||||
}
|
||||
} else if path.is_ident("u64") {
|
||||
quote! {
|
||||
let #name = buf.at::<u64>(yunq::message::field_offset(offset, #ind))?;
|
||||
}
|
||||
} else if path.is_ident("i64") {
|
||||
quote! {
|
||||
let #name = buf.at::<i64>(yunq::message::field_offset(offset, #ind))?;
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
let #name = {
|
||||
let msg_offset = buf.at::<u32>(yunq::message::field_offset(offset, #ind))? as usize;
|
||||
|
||||
#path::parse(buf, offset + msg_offset, caps)?
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn field_names(name: &Ident, _ind: usize, _path: &Path) -> proc_macro2::TokenStream {
|
||||
quote! { #name }
|
||||
}
|
||||
|
||||
fn apply_to_struct_fields<T>(input: &DeriveInput, func: fn(&Ident, usize, &Path) -> T) -> Vec<T> {
|
||||
match &input.data {
|
||||
Data::Struct(DataStruct {
|
||||
fields: Fields::Named(fields),
|
||||
..
|
||||
}) => fields
|
||||
.named
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(ind, field)| match &field.ty {
|
||||
Type::Path(TypePath { path, .. }) => {
|
||||
func(&field.ident.as_ref().unwrap(), ind, path)
|
||||
}
|
||||
_ => {
|
||||
panic!("Unrecognized type: {:?}", field)
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
_ => {
|
||||
panic!("Unrecognized input (Expected Struct): {:?}", input.data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro_derive(YunqMessage)]
|
||||
pub fn derive_client(input_tokens: TokenStream) -> TokenStream {
|
||||
let input: DeriveInput = parse_macro_input!(input_tokens);
|
||||
let ident = input.ident.clone();
|
||||
|
||||
let prelude = quote! {
|
||||
impl YunqMessage for #ident
|
||||
};
|
||||
|
||||
let num_fields = apply_to_struct_fields(&input, |_, _, _| ()).len();
|
||||
|
||||
let serializers = apply_to_struct_fields(&input, serialize_field);
|
||||
let serialize = quote! {
|
||||
fn serialize<const N: usize>(
|
||||
&self,
|
||||
buf: &mut yunq::ByteBuffer<N>,
|
||||
offset: usize,
|
||||
caps: &mut alloc::vec::Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError> {
|
||||
let num_fields = #num_fields;
|
||||
let core_size: u32 = (yunq::message::MESSAGE_HEADER_SIZE + 8 * num_fields) as u32;
|
||||
let mut next_extension = core_size;
|
||||
|
||||
#(#serializers)*
|
||||
|
||||
buf.write_at(offset + 0, yunq::message::MESSAGE_IDENT)?;
|
||||
buf.write_at(offset + 4, core_size)?;
|
||||
buf.write_at(offset + 8, next_extension)?;
|
||||
buf.write_at(offset + 12, 0 as u32)?;
|
||||
Ok(next_extension as usize)
|
||||
}
|
||||
};
|
||||
|
||||
let field_names = apply_to_struct_fields(&input, field_names);
|
||||
let parsers = apply_to_struct_fields(&input, parse_field);
|
||||
let parse = quote! {
|
||||
fn parse<const N: usize>(
|
||||
buf: &yunq::ByteBuffer<N>,
|
||||
offset: usize,
|
||||
caps: &alloc::vec::Vec<z_cap_t>,
|
||||
) -> Result<Self, ZError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if buf.at::<u32>(offset + 0)? != yunq::message::MESSAGE_IDENT {
|
||||
return Err(ZError::INVALID_ARGUMENT);
|
||||
}
|
||||
// TODO: Parse core size.
|
||||
// TODO: Parse extension size.
|
||||
// TODO: Check CRC32
|
||||
// TODO: Parse options.
|
||||
|
||||
#(#parsers)*
|
||||
|
||||
Ok(Self {
|
||||
#(#field_names),*
|
||||
})
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
let output = quote! {
|
||||
#prelude {
|
||||
#serialize
|
||||
|
||||
#parse
|
||||
}
|
||||
};
|
||||
output.into()
|
||||
}
|
||||
|
|
@ -6,9 +6,7 @@ edition = "2021"
|
|||
[dependencies]
|
||||
mammoth = { path = "../mammoth", default-features = false}
|
||||
yunq = {path = "../yunq", default-features = false}
|
||||
yunq-derive = {path = "../yunq-derive"}
|
||||
|
||||
[build-dependencies]
|
||||
yunqc = {path = "../../../yunq/rust"}
|
||||
|
||||
[dev-dependencies]
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ mod tests {
|
|||
use super::*;
|
||||
|
||||
extern crate std;
|
||||
use std::println;
|
||||
use std::vec;
|
||||
|
||||
#[test]
|
||||
|
|
@ -59,4 +60,21 @@ mod tests {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_serialization() -> Result<(), ZError> {
|
||||
let rep = Repeated { unsigned_ints: vec![0, 1, 3],};
|
||||
|
||||
let mut buf = ByteBuffer::<1024>::new();
|
||||
let mut caps = Vec::new();
|
||||
rep.serialize(&mut buf, 0, &mut caps)?;
|
||||
|
||||
let parsed = Repeated::parse(&buf, 0, &caps)?;
|
||||
|
||||
println!("{:?}", parsed.unsigned_ints);
|
||||
|
||||
assert!(parsed == rep);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
mammoth = {path = "../mammoth", default-features = false}
|
||||
yunq-derive = {path = "../yunq-derive"}
|
||||
|
||||
[features]
|
||||
default = ["hosted"]
|
||||
|
|
|
|||
|
|
@ -12,6 +12,57 @@ pub fn field_offset(offset: usize, field_index: usize) -> usize {
|
|||
offset + MESSAGE_HEADER_SIZE + (8 * field_index)
|
||||
}
|
||||
|
||||
pub fn parse_repeated<T: Copy, const N: usize>(
|
||||
buf: &ByteBuffer<N>,
|
||||
offset: usize,
|
||||
len: usize,
|
||||
) -> Result<Vec<T>, ZError> {
|
||||
let mut repeated = Vec::new();
|
||||
for i in 0..len {
|
||||
repeated.push(buf.at::<T>(offset + (i * size_of::<T>()))?);
|
||||
}
|
||||
Ok(repeated)
|
||||
}
|
||||
|
||||
pub fn parse_repeated_message<T: YunqMessage, const N: usize>(
|
||||
buf: &ByteBuffer<N>,
|
||||
mut offset: usize,
|
||||
len: usize,
|
||||
caps: &Vec<z_cap_t>,
|
||||
) -> Result<Vec<T>, ZError> {
|
||||
let mut repeated = Vec::new();
|
||||
for _ in 0..len {
|
||||
// FIXME: This is a bad way to get the length.
|
||||
let msg_len = buf.at::<u32>(offset + 8)? as usize;
|
||||
repeated.push(T::parse(buf, offset, caps)?);
|
||||
offset += msg_len;
|
||||
}
|
||||
Ok(repeated)
|
||||
}
|
||||
|
||||
pub fn serialize_repeated<T: Copy, const N: usize>(
|
||||
buf: &mut ByteBuffer<N>,
|
||||
offset: usize,
|
||||
data: &Vec<T>,
|
||||
) -> Result<usize, ZError> {
|
||||
for i in 0..data.len() {
|
||||
buf.write_at(offset + (i * size_of::<T>()), data[i])?;
|
||||
}
|
||||
Ok(offset + (data.len() * size_of::<T>()))
|
||||
}
|
||||
|
||||
pub fn serialize_repeated_message<T: YunqMessage, const N: usize>(
|
||||
buf: &mut ByteBuffer<N>,
|
||||
mut offset: usize,
|
||||
data: &Vec<T>,
|
||||
caps: &mut Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError> {
|
||||
for item in data {
|
||||
offset = item.serialize(buf, offset, caps)?;
|
||||
}
|
||||
Ok(offset)
|
||||
}
|
||||
|
||||
pub fn serialize_error<const N: usize>(buf: &mut ByteBuffer<N>, err: ZError) {
|
||||
buf.write_at(0, SENTINEL)
|
||||
.expect("Failed to serialize SENTINEL");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue