Skip to content

Commit

Permalink
Fixed structure unit tests and FrcStructure macro
Browse files Browse the repository at this point in the history
  • Loading branch information
oh-yes-0-fps committed Apr 17, 2024
1 parent 9868877 commit 04b5d54
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 32 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ nalgebra = { version = "0.32", optional = true}
num = { version = "0.4", optional = true}
simba = { version = "0.8", optional = true}
ctor = { version = "0.2.5", optional = true}
frclib-structure-macros = { version = "0.1", optional = true}
frclib-structure-macros = { path = "./frclib-structure-macros", optional = true}
# frclib-structure-macros = { version = "0.1", optional = true}
paste = { version = "1.0.14", optional = true }

[dev-dependencies]
Expand Down Expand Up @@ -54,8 +55,7 @@ features = ["full"]

[workspace]
members = [
"frclib-structure-macros",
"frclib-unit-func-macros"
"frclib-structure-macros"
]
resolver = "2"

Expand Down
2 changes: 1 addition & 1 deletion frclib-structure-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "frclib-structure-macros"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
license = "MIT"
description = "A derive macro for implementing the FrcStruct trait on structs"
Expand Down
30 changes: 20 additions & 10 deletions frclib-structure-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,28 @@ fn impl_frc_struct(name: &Ident, fields: &Fields) -> TokenStream2 {
const TYPE: &'static str = stringify!(#name);
const SCHEMA_SUPPLIER: fn() -> String = || #schema;

fn pack(&self, buffer: &mut impl frclib_core::structure::bytes::BufMut) {
fn pack(&self, buffer: &mut Vec<u8>) {
#pack
}

fn unpack(buffer: &mut impl frclib_core::structure::bytes::Buf) -> Self {
fn unpack(buffer: &mut Cursor<&[u8]>) -> Self {
#unpack
}
}
frclib_core::structure::inventory::submit! { <#name as FrcStructure>::DESCRIPTION }
///This isnt a generic impl for every struct because of primitive and unit types
impl Into<frclib_core::value::FrcValue> for #name {
fn into(self) -> frclib_core::value::FrcValue {
let mut buffer = frclib_core::structure::bytes::BytesMut::with_capacity(Self::SIZE);
let mut buffer = Vec::with_capacity(Self::SIZE);
self.pack(&mut buffer);
frclib_core::value::FrcValue::Struct(
&Self::DESCRIPTION,
Box::new(buffer.freeze())
Box::new(
FrcStructureBytes::from_parts(
&Self::DESCRIPTION,
1,
buffer.into_boxed_slice()
)
)
)
}
}
Expand Down Expand Up @@ -290,12 +295,12 @@ fn impl_frc_enum(
const TYPE: &'static str = stringify!(#name);
const SCHEMA_SUPPLIER: fn() -> String = || #schema;

fn pack(&self, buffer: &mut impl frclib_core::structure::bytes::BufMut) {
fn pack(&self, buffer: &mut Vec<u8>) {
let repr = *self as #repr;
<#repr as FrcStructure>::pack(&repr, buffer);
}

fn unpack(buffer: &mut impl frclib_core::structure::bytes::Buf) -> Self {
fn unpack(buffer: &mut Cursor<&[u8]>) -> Self {
let repr = <#repr as FrcStructure>::unpack(buffer);
Self::from_repr(repr).unwrap_or_default()
}
Expand All @@ -304,11 +309,16 @@ fn impl_frc_enum(
///This isnt a generic impl for every struct because of primitive and unit types
impl Into<frclib_core::value::FrcValue> for #name {
fn into(self) -> frclib_core::value::FrcValue {
let mut buffer = frclib_core::structure::bytes::BytesMut::with_capacity(Self::SIZE);
let mut buffer = Vec::with_capacity(Self::SIZE);
self.pack(&mut buffer);
frclib_core::value::FrcValue::Struct(
&Self::DESCRIPTION,
Box::new(buffer.freeze())
Box::new(
FrcStructureBytes::from_parts(
&Self::DESCRIPTION,
1,
buffer.into_boxed_slice()
)
)
)
}
}
Expand Down
27 changes: 27 additions & 0 deletions frclib-unit-func-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@ use proc_macro2::{TokenStream as TokenStream2, TokenTree};
use quote::{quote, ToTokens};
use syn::{Attribute, Fields, Ident, Meta, MetaList, QSelf, Token, Variant};

enum Parameter {
Normal(syn::FnArg),
Unit{
name: Ident,
family_ty: syn::Type,
variant_ty: syn::Type,
},
}

fn get_parameters(item_fn: &syn::ItemFn) -> Vec<Parameter> {
let mut parameters = Vec::new();

for input in &item_fn.sig.inputs {
match input {
syn::FnArg::Typed(pat_type) => {
parameters.push(Parameter::Normal(input.clone()));
}
syn::FnArg::Receiver(_) => {
parameters.push(Parameter::Normal(input.clone()));
}
}
}

parameters
}

#[proc_macro_attribute]
pub fn unit_func(_attr: TokenStream, item: TokenStream) -> TokenStream {
// validate that the item is a function
Expand All @@ -15,3 +41,4 @@ pub fn unit_func(_attr: TokenStream, item: TokenStream) -> TokenStream {

item_fn.into_token_stream().into()
}

4 changes: 2 additions & 2 deletions src/structure/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! This module contains an implementation of [WPIlib struct spec](https://github.com/wpilibsuite/allwpilib/blob/main/wpiutil/doc/struct.adoc)
//! for rust and a macro to generate the trait implementation for a given struct.

// #[cfg(test)]
// mod test;
#[cfg(test)]
mod test;

mod prims;

Expand Down
4 changes: 3 additions & 1 deletion src/structure/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl FrcStructure for SubStruct {
self.value.pack(buffer);
}

fn unpack(buffer: &[u8]) -> Self {
fn unpack(buffer: &mut std::io::Cursor<&[u8]>) -> Self {
Self {
value: <f64 as FrcStructure>::unpack(buffer),
}
Expand All @@ -23,6 +23,8 @@ impl FrcStructure for SubStruct {

inventory::submit! { SubStruct::DESCRIPTION }



#[test]
#[cfg(feature = "value-union")]
fn test_structures() {
Expand Down
21 changes: 6 additions & 15 deletions src/units/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,29 +222,20 @@ macro_rules! unit_conversion {
macro_rules! unit_family {
($family_name:ident ( $standard:ident ): $($unit_name:ident),*) => {
$crate::units::macros::paste::paste! {
#[doc = "A family of units. "]
#[doc = "A family of units representing an `" $standard "` measurement."]
#[doc = ""]
#[doc = "The standard unit is `" $standard "`."]
#[doc = "The other units are `" $($unit_name)"`, `"* "`."]
#[doc = ""]
#[doc = "The other units are `" $($unit_name)"`, `"* "` and other user-defined units."]
pub trait $family_name: Into<$standard> + From<$standard> + Copy {
#[doc = "Converts this unit to the standard unit of the family."]
#[inline]
fn standard(self) -> $standard {
self.into()
}

$(
#[doc = "Converts this unit to `" $unit_name "`."]
#[inline]
fn [<to_ $unit_name:lower s>](self) -> $unit_name {
$unit_name::from(self.standard())
}
)*

#[doc = "Converts this unit to `" $standard "`."]
#[doc = "This is the same as [`standard`](#method.standard)." ]
#[inline]
fn [<to_ $standard:lower s>](self) -> $standard {
self.standard()
fn conv<U: $family_name>(self) -> U {
U::from(self.standard())
}
}
}
Expand Down

0 comments on commit 04b5d54

Please sign in to comment.