Add macros to derive message serializations.
This commit is contained in:
parent
ccd13fecf1
commit
8f35d38e93
|
@ -34,6 +34,24 @@ dependencies = [
|
|||
"linked_list_allocator",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.86"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
|
@ -49,12 +67,38 @@ dependencies = [
|
|||
"lock_api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "testbed"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"mammoth",
|
||||
"yellowstone",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
||||
[[package]]
|
||||
name = "yellowstone"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"mammoth",
|
||||
"yunq",
|
||||
"yunq-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -62,4 +106,14 @@ name = "yunq"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"mammoth",
|
||||
"yunq-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "yunq-derive"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[workspace]
|
||||
|
||||
members = [
|
||||
"lib/mammoth", "lib/yunq", "usr/testbed",
|
||||
"lib/mammoth", "lib/yellowstone", "lib/yunq", "lib/yunq-derive", "usr/testbed",
|
||||
]
|
||||
|
||||
# the profile used for `cargo build`
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "yellowstone"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
mammoth = { path = "../mammoth" }
|
||||
yunq = {path = "../yunq"}
|
||||
yunq-derive = {path = "../yunq-derive"}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
#![no_std]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::string::String;
|
||||
use alloc::string::ToString;
|
||||
use mammoth::syscall::z_cap_t;
|
||||
use mammoth::syscall::ZError;
|
||||
use yunq::ByteBuffer;
|
||||
use yunq::YunqMessage;
|
||||
use yunq_derive::YunqMessage;
|
||||
|
||||
#[derive(YunqMessage)]
|
||||
pub struct GetEndpointRequest {
|
||||
pub endpoint_name: String,
|
||||
}
|
||||
|
||||
#[derive(YunqMessage)]
|
||||
pub struct Endpoint {
|
||||
pub endpoint: z_cap_t,
|
||||
}
|
||||
|
||||
pub struct YellowstoneClient {
|
||||
endpoint_cap: z_cap_t,
|
||||
byte_buffer: ByteBuffer<0x1000>,
|
||||
}
|
||||
|
||||
impl YellowstoneClient {
|
||||
pub fn new(endpoint_cap: z_cap_t) -> Self {
|
||||
Self {
|
||||
endpoint_cap,
|
||||
byte_buffer: ByteBuffer::new(),
|
||||
}
|
||||
}
|
||||
pub fn get_endpoint(&mut self, req: &GetEndpointRequest) -> Result<Endpoint, ZError> {
|
||||
yunq::client::call_endpoint(req, &mut self.byte_buffer, self.endpoint_cap)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
[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"] }
|
|
@ -0,0 +1,156 @@
|
|||
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 {
|
||||
panic!(
|
||||
"Serialization not implemented for: {}",
|
||||
path.to_token_stream()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
panic!("Parsing not implemented for: {}", path.into_token_stream());
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
|
@ -5,3 +5,4 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
mammoth = {path = "../mammoth"}
|
||||
yunq-derive = {path = "../yunq-derive"}
|
||||
|
|
|
@ -4,110 +4,8 @@
|
|||
extern crate alloc;
|
||||
|
||||
mod buffer;
|
||||
mod client;
|
||||
mod message;
|
||||
pub mod client;
|
||||
pub mod message;
|
||||
|
||||
use alloc::string::String;
|
||||
use alloc::vec::Vec;
|
||||
pub use buffer::ByteBuffer;
|
||||
use mammoth::syscall::z_cap_t;
|
||||
use mammoth::syscall::ZError;
|
||||
pub use message::YunqMessage;
|
||||
|
||||
const MESSAGE_HEADER_SIZE: usize = 24; // 4x uint32, 1x uint64
|
||||
|
||||
pub struct GetEndpointRequest {
|
||||
pub endpoint_name: String,
|
||||
}
|
||||
|
||||
impl YunqMessage for GetEndpointRequest {
|
||||
fn serialize<const N: usize>(
|
||||
&self,
|
||||
buf: &mut buffer::ByteBuffer<N>,
|
||||
offset: usize,
|
||||
_caps: &mut Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError> {
|
||||
let core_size: u32 = (MESSAGE_HEADER_SIZE + 8 * 1) as u32;
|
||||
let mut next_extension = core_size;
|
||||
|
||||
let endpoint_name_offset = next_extension;
|
||||
let endpoint_name_length = self.endpoint_name.len() as u32;
|
||||
|
||||
buf.write_str_at(offset + next_extension as usize, &self.endpoint_name)?;
|
||||
next_extension += endpoint_name_length;
|
||||
|
||||
buf.write_at(field_offset(offset, 0), endpoint_name_offset)?;
|
||||
buf.write_at(field_offset(offset, 0) + 4, endpoint_name_length)?;
|
||||
|
||||
buf.write_at(offset + 0, 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)
|
||||
}
|
||||
|
||||
fn parse<const N: usize>(
|
||||
_buf: &ByteBuffer<N>,
|
||||
_offset: usize,
|
||||
_caps: &Vec<z_cap_t>,
|
||||
) -> Result<Self, ZError>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Endpoint {
|
||||
pub endpoint: z_cap_t,
|
||||
}
|
||||
|
||||
fn field_offset(offset: usize, field_index: usize) -> usize {
|
||||
offset + MESSAGE_HEADER_SIZE + (8 * field_index)
|
||||
}
|
||||
|
||||
impl YunqMessage for Endpoint {
|
||||
fn parse<const N: usize>(
|
||||
buf: &ByteBuffer<N>,
|
||||
offset: usize,
|
||||
caps: &Vec<z_cap_t>,
|
||||
) -> Result<Self, ZError> {
|
||||
if buf.at::<u32>(offset + 0)? != message::MESSAGE_IDENT {
|
||||
return Err(ZError::INVALID_ARGUMENT);
|
||||
}
|
||||
// TODO: Parse core size.
|
||||
// TODO: Parse extension size.
|
||||
// TODO: Check CRC32
|
||||
// TODO: Parse options.
|
||||
|
||||
let endpoint_ptr = buf.at::<u64>(field_offset(offset, 0))?;
|
||||
let endpoint = caps[endpoint_ptr as usize];
|
||||
Ok(Endpoint { endpoint })
|
||||
}
|
||||
|
||||
fn serialize<const N: usize>(
|
||||
&self,
|
||||
_buf: &mut buffer::ByteBuffer<N>,
|
||||
_offset: usize,
|
||||
_caps: &mut Vec<z_cap_t>,
|
||||
) -> Result<usize, ZError> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct YellowstoneClient {
|
||||
endpoint_cap: z_cap_t,
|
||||
byte_buffer: ByteBuffer<0x1000>,
|
||||
}
|
||||
|
||||
impl YellowstoneClient {
|
||||
pub fn new(endpoint_cap: z_cap_t) -> Self {
|
||||
Self {
|
||||
endpoint_cap,
|
||||
byte_buffer: ByteBuffer::new(),
|
||||
}
|
||||
}
|
||||
pub fn get_endpoint(&mut self, req: &GetEndpointRequest) -> Result<Endpoint, ZError> {
|
||||
client::call_endpoint(req, &mut self.byte_buffer, self.endpoint_cap)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,11 @@ use mammoth::syscall::z_cap_t;
|
|||
use mammoth::syscall::ZError;
|
||||
|
||||
pub const MESSAGE_IDENT: u32 = 0x33441122;
|
||||
pub const MESSAGE_HEADER_SIZE: usize = 24; // 4x uint32, 1x uint64
|
||||
|
||||
pub fn field_offset(offset: usize, field_index: usize) -> usize {
|
||||
offset + MESSAGE_HEADER_SIZE + (8 * field_index)
|
||||
}
|
||||
|
||||
pub trait YunqMessage {
|
||||
fn parse<const N: usize>(
|
||||
|
|
|
@ -5,4 +5,4 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
mammoth = { path = "../../lib/mammoth" }
|
||||
yunq = { path = "../../lib/yunq" }
|
||||
yellowstone = { path = "../../lib/yellowstone" }
|
||||
|
|
|
@ -9,6 +9,8 @@ use mammoth::debug;
|
|||
use mammoth::define_entry;
|
||||
use mammoth::syscall::debug;
|
||||
use mammoth::syscall::z_err_t;
|
||||
use yellowstone::GetEndpointRequest;
|
||||
use yellowstone::YellowstoneClient;
|
||||
|
||||
define_entry!();
|
||||
|
||||
|
@ -28,11 +30,11 @@ pub extern "C" fn main() -> z_err_t {
|
|||
|
||||
let mut yellowstone;
|
||||
unsafe {
|
||||
yellowstone = yunq::YellowstoneClient::new(mammoth::INIT_ENDPOINT);
|
||||
yellowstone = YellowstoneClient::new(mammoth::INIT_ENDPOINT);
|
||||
}
|
||||
|
||||
let endpoint = yellowstone
|
||||
.get_endpoint(&yunq::GetEndpointRequest {
|
||||
.get_endpoint(&GetEndpointRequest {
|
||||
endpoint_name: "denali".to_string(),
|
||||
})
|
||||
.expect("Failed to get endpoint");
|
||||
|
|
Loading…
Reference in New Issue