Kaynağa Gözat

rustfmt pass.

Kestrel 2 yıl önce
ebeveyn
işleme
e0e93181ff

+ 27 - 28
rustructure-macros/src/lib.rs

@@ -1,5 +1,5 @@
-use proc_macro::{TokenStream};
-use quote::{quote};
+use proc_macro::TokenStream;
+use quote::quote;
 
 #[proc_macro_derive(Walkable, attributes(skip_walk))]
 pub fn walkable(tokens: TokenStream) -> TokenStream {
@@ -7,11 +7,9 @@ pub fn walkable(tokens: TokenStream) -> TokenStream {
 
     if let syn::Data::Struct(s) = &input.data {
         struct_walkable(&input, s)
-    }
-    else if let syn::Data::Enum(e) = &input.data {
+    } else if let syn::Data::Enum(e) = &input.data {
         enum_walkable(&input, e)
-    }
-    else {
+    } else {
         todo!("Unions currently not implemented")
     }
 }
@@ -20,28 +18,27 @@ fn fields_walkable<'i, I: Iterator<Item = &'i syn::Field>>(input: I) -> proc_mac
     let mut fields = Vec::new();
     let mut index = 0;
     for field in input {
+        let skip = field
+            .attrs
+            .iter()
+            .any(|a| a.parse_meta().unwrap().path().is_ident("skip_walk"));
+
+        let ident = if let Some(i) = &field.ident {
+            i.to_string()
+        } else {
+            index += 1;
+            format!("{}", index - 1)
+        };
 
-        let skip = field.attrs.iter().any(|a| a.parse_meta().unwrap().path().is_ident("skip_walk"));
-
-        let ident =
-            if let Some(i) = &field.ident {
-                i.to_string()
-            }
-            else {
-                index += 1;
-                format!("{}", index-1)
-            };
-        
         let ty = &field.ty;
         if skip {
-            fields.push(quote!{ walker.visit_skip_field(#ident) })
-        }
-        else {
-            fields.push(quote!{ walker.visit_field::<#ty>(#ident) })
+            fields.push(quote! { walker.visit_skip_field(#ident) })
+        } else {
+            fields.push(quote! { walker.visit_field::<#ty>(#ident) })
         }
     }
 
-    quote!{ #(#fields);* }
+    quote! { #(#fields);* }
 }
 
 fn struct_walkable(input: &syn::DeriveInput, ds: &syn::DataStruct) -> TokenStream {
@@ -49,12 +46,12 @@ fn struct_walkable(input: &syn::DeriveInput, ds: &syn::DataStruct) -> TokenStrea
     let name_str = input.ident.to_string();
 
     let walk_fields = match &ds.fields {
-        syn::Fields::Unit => quote!{},
+        syn::Fields::Unit => quote! {},
         syn::Fields::Named(u) => fields_walkable(u.named.iter()),
         syn::Fields::Unnamed(u) => fields_walkable(u.unnamed.iter()),
     };
 
-    let ret = quote!{
+    let ret = quote! {
         impl ::rustructure::Walkable for #name {
             fn walk_with<W: ::rustructure::Walker>(walker: &mut W) {
                 struct Fields {};
@@ -67,7 +64,8 @@ fn struct_walkable(input: &syn::DeriveInput, ds: &syn::DataStruct) -> TokenStrea
                 walker.visit_struct::<Fields>(#name_str);
             }
         }
-    }.into();
+    }
+    .into();
 
     ret
 }
@@ -84,7 +82,7 @@ fn enum_walkable(input: &syn::DeriveInput, de: &syn::DataEnum) -> TokenStream {
 
         let visit_fields = fields_walkable(variant.fields.iter());
 
-        walk_variants.push(quote!{
+        walk_variants.push(quote! {
             #[allow(non_camel_case_types)]
             struct #variant_name {};
             impl ::rustructure::Walkable for #variant_name {
@@ -97,7 +95,7 @@ fn enum_walkable(input: &syn::DeriveInput, de: &syn::DataEnum) -> TokenStream {
         });
     }
 
-    quote!{
+    quote! {
         impl ::rustructure::Walkable for #name {
             fn walk_with<W: ::rustructure::Walker>(walker: &mut W) {
 
@@ -111,5 +109,6 @@ fn enum_walkable(input: &syn::DeriveInput, de: &syn::DataEnum) -> TokenStream {
                 walker.visit_enum::<WalkEnum>(#name_str);
             }
         }
-    }.into()
+    }
+    .into()
 }

+ 16 - 15
rustructure-test/src/main.rs

@@ -5,14 +5,14 @@ fn main() {
 #[cfg(test)]
 #[allow(dead_code)]
 mod unnested {
-    use rustructure::{Walkable, StringWalker};
+    use rustructure::{StringWalker, Walkable};
 
     #[derive(Walkable)]
     struct EmptyStruct {}
 
     #[derive(Walkable)]
     struct SingletonStruct {
-        value: u8
+        value: u8,
     }
 
     #[derive(Walkable)]
@@ -32,13 +32,13 @@ mod unnested {
 
     #[derive(Walkable)]
     enum SingleVariantEnum {
-        A
+        A,
     }
 
     #[derive(Walkable)]
     enum DoubleVariantEnum {
         A,
-        B
+        B,
     }
 
     #[test]
@@ -91,16 +91,16 @@ mod unnested {
 #[cfg(test)]
 #[allow(dead_code)]
 mod simple_nested {
-    use rustructure::{Walkable, StringWalker};
+    use rustructure::{StringWalker, Walkable};
 
     #[derive(Walkable)]
     struct Contained {
-        v: u8
+        v: u8,
     }
 
     #[derive(Walkable)]
     struct Container {
-        c: Contained
+        c: Contained,
     }
 
     #[test]
@@ -118,12 +118,12 @@ mod simple_nested {
 
     #[derive(Walkable)]
     enum IntegerHolder {
-        Hold8(u8)
+        Hold8(u8),
     }
 
     #[derive(Walkable)]
     enum StructHolder {
-        HoldContained(Contained)
+        HoldContained(Contained),
     }
 
     #[test]
@@ -147,18 +147,19 @@ mod simple_nested {
         );
 
         assert_eq!(
-            StringWalker::walk::<(u8,u8)>(),
+            StringWalker::walk::<(u8, u8)>(),
             "(tuple (field:0 integer) (field:1 integer))".to_owned()
         );
 
         assert_eq!(
-            StringWalker::walk::<(u8,u8,u8)>(),
+            StringWalker::walk::<(u8, u8, u8)>(),
             "(tuple (field:0 integer) (field:1 integer) (field:2 integer))".to_owned()
         );
 
         assert_eq!(
-            StringWalker::walk::<(u8,u8,u8,u8)>(),
-            "(tuple (field:0 integer) (field:1 integer) (field:2 integer) (field:3 integer))".to_owned()
+            StringWalker::walk::<(u8, u8, u8, u8)>(),
+            "(tuple (field:0 integer) (field:1 integer) (field:2 integer) (field:3 integer))"
+                .to_owned()
         );
 
         assert_eq!(
@@ -171,12 +172,12 @@ mod simple_nested {
 #[cfg(test)]
 #[allow(dead_code)]
 mod skips {
-    use rustructure::{Walkable, StringWalker};
+    use rustructure::{StringWalker, Walkable};
 
     #[derive(Walkable)]
     struct HoldSimpleType {
         #[skip_walk]
-        v: u8
+        v: u8,
     }
 
     #[test]

+ 22 - 14
rustructure/src/impls.rs

@@ -1,4 +1,4 @@
-use crate::{Walkable,Walker};
+use crate::{Walkable, Walker};
 
 impl Walkable for u8 {
     fn walk_with<W: Walker>(walker: &mut W) {
@@ -68,7 +68,9 @@ impl Walkable for () {
 
 impl<T0: Walkable> Walkable for (T0,) {
     fn walk_with<W: Walker>(walker: &mut W) {
-        struct Tuple<T> { pd: core::marker::PhantomData<T> }
+        struct Tuple<T> {
+            pd: core::marker::PhantomData<T>,
+        }
         impl<T0: Walkable> Walkable for Tuple<(T0,)> {
             fn walk_with<W: Walker>(walker: &mut W) {
                 walker.visit_field::<T0>("0");
@@ -78,37 +80,43 @@ impl<T0: Walkable> Walkable for (T0,) {
     }
 }
 
-impl<T0: Walkable, T1: Walkable> Walkable for (T0,T1) {
+impl<T0: Walkable, T1: Walkable> Walkable for (T0, T1) {
     fn walk_with<W: Walker>(walker: &mut W) {
-        struct Tuple<T> { pd: core::marker::PhantomData<T> }
-        impl<T0: Walkable, T1: Walkable> Walkable for Tuple<(T0,T1)> {
+        struct Tuple<T> {
+            pd: core::marker::PhantomData<T>,
+        }
+        impl<T0: Walkable, T1: Walkable> Walkable for Tuple<(T0, T1)> {
             fn walk_with<W: Walker>(walker: &mut W) {
                 walker.visit_field::<T0>("0");
                 walker.visit_field::<T1>("1");
             }
         }
-        walker.visit_tuple::<Tuple<(T0,T1)>>();
+        walker.visit_tuple::<Tuple<(T0, T1)>>();
     }
 }
 
-impl<T0: Walkable, T1: Walkable, T2: Walkable> Walkable for (T0,T1,T2) {
+impl<T0: Walkable, T1: Walkable, T2: Walkable> Walkable for (T0, T1, T2) {
     fn walk_with<W: Walker>(walker: &mut W) {
-        struct Tuple<T> { pd: core::marker::PhantomData<T> }
-        impl<T0: Walkable, T1: Walkable, T2: Walkable> Walkable for Tuple<(T0,T1,T2)> {
+        struct Tuple<T> {
+            pd: core::marker::PhantomData<T>,
+        }
+        impl<T0: Walkable, T1: Walkable, T2: Walkable> Walkable for Tuple<(T0, T1, T2)> {
             fn walk_with<W: Walker>(walker: &mut W) {
                 walker.visit_field::<T0>("0");
                 walker.visit_field::<T1>("1");
                 walker.visit_field::<T2>("2");
             }
         }
-        walker.visit_tuple::<Tuple<(T0,T1,T2)>>();
+        walker.visit_tuple::<Tuple<(T0, T1, T2)>>();
     }
 }
 
-impl<T0: Walkable, T1: Walkable, T2: Walkable, T3: Walkable> Walkable for (T0,T1,T2,T3) {
+impl<T0: Walkable, T1: Walkable, T2: Walkable, T3: Walkable> Walkable for (T0, T1, T2, T3) {
     fn walk_with<W: Walker>(walker: &mut W) {
-        struct Tuple<T> { pd: core::marker::PhantomData<T> }
-        impl<T0: Walkable, T1: Walkable, T2: Walkable, T3: Walkable> Walkable for Tuple<(T0,T1,T2,T3)> {
+        struct Tuple<T> {
+            pd: core::marker::PhantomData<T>,
+        }
+        impl<T0: Walkable, T1: Walkable, T2: Walkable, T3: Walkable> Walkable for Tuple<(T0, T1, T2, T3)> {
             fn walk_with<W: Walker>(walker: &mut W) {
                 walker.visit_field::<T0>("0");
                 walker.visit_field::<T1>("1");
@@ -116,6 +124,6 @@ impl<T0: Walkable, T1: Walkable, T2: Walkable, T3: Walkable> Walkable for (T0,T1
                 walker.visit_field::<T3>("3");
             }
         }
-        walker.visit_tuple::<Tuple<(T0,T1,T2,T3)>>();
+        walker.visit_tuple::<Tuple<(T0, T1, T2, T3)>>();
     }
 }

+ 5 - 3
rustructure/src/lib.rs

@@ -43,15 +43,17 @@ pub use sw::StringWalker;
 
 #[cfg(feature = "std")]
 mod sw {
-    use crate::{Walker,Walkable,IntegerType};
+    use crate::{IntegerType, Walkable, Walker};
 
     pub struct StringWalker {
-        buffer: String
+        buffer: String,
     }
 
     impl StringWalker {
         pub fn walk<T: Walkable>() -> String {
-            let mut sw = Self { buffer: String::new() };
+            let mut sw = Self {
+                buffer: String::new(),
+            };
             T::walk_with(&mut sw);
             // remove inevitable leading space
             if sw.buffer.len() > 0 {