scroll_derive-0.10.1/Cargo.toml.orig010064400017500001750000000010141355422640000155320ustar0000000000000000[package] name = "scroll_derive" version = "0.10.1" authors = ["m4b ", "Ted Mielczarek "] readme = "README.md" edition = "2018" keywords = ["derive", "macros", "pread", "pwrite", "bytes"] repository = "https://github.com/m4b/scroll" license = "MIT" documentation = "https://docs.rs/scroll_derive" description = "A macros 1.1 derive implementation for Pread and Pwrite traits from the scroll crate" [lib] proc-macro = true [dependencies] proc-macro2 = "1" quote = "1" syn = "1" scroll_derive-0.10.1/Cargo.toml0000644000000021200000000000000117770ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "scroll_derive" version = "0.10.1" authors = ["m4b ", "Ted Mielczarek "] description = "A macros 1.1 derive implementation for Pread and Pwrite traits from the scroll crate" documentation = "https://docs.rs/scroll_derive" readme = "README.md" keywords = ["derive", "macros", "pread", "pwrite", "bytes"] license = "MIT" repository = "https://github.com/m4b/scroll" [lib] proc-macro = true [dependencies.proc-macro2] version = "1" [dependencies.quote] version = "1" [dependencies.syn] version = "1" scroll_derive-0.10.1/LICENSE010064400017500001750000000020411336647315700136670ustar0000000000000000MIT License Copyright (c) 2017 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. scroll_derive-0.10.1/README.md010064400017500001750000000017341336647315700141510ustar0000000000000000# scroll_derive Macros 1.1 implementing #[derive(Pread, Pwrite)] for https://github.com/m4b/scroll Add derive annotations to your POD seamlessly and easily: ```rust extern crate scroll; #[macro_use] extern crate scroll_derive; #[derive(Debug, PartialEq, Pread, Pwrite, IOread, IOwrite, SizeWith)] #[repr(C)] struct Data { id: u32, timestamp: f64, arr: [u16; 2], } use scroll::{Pread, Pwrite, Cread, LE}; fn main (){ let bytes = [0xefu8, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0, 0, 224, 63, 0xad, 0xde, 0xef, 0xbe]; let data: Data = bytes.pread_with(0, LE).unwrap(); println!("data: {:?}", &data); assert_eq!(data.id, 0xdeadbeefu32); let mut bytes2 = vec![0; ::std::mem::size_of::()]; bytes2.pwrite_with(data, 0, LE).unwrap(); let data: Data = bytes.pread_with(0, LE).unwrap(); let data2: Data = bytes2.pread_with(0, LE).unwrap(); assert_eq!(data, data2); let data: Data = bytes.cread_with(0, LE); assert_eq!(data, data2); } ``` scroll_derive-0.10.1/examples/main.rs010064400017500001750000000014631355067671100157750ustar0000000000000000use scroll_derive::{Pread, Pwrite, IOread, IOwrite, SizeWith}; #[derive(Debug, PartialEq, Pread, Pwrite, IOread, IOwrite, SizeWith)] #[repr(C)] struct Data { id: u32, timestamp: f64, arr: [u16; 2], } use scroll::{Pread, Pwrite, Cread, LE}; fn main () { let bytes = [0xefu8, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0, 0, 224, 63, 0xad, 0xde, 0xef, 0xbe]; let data: Data = bytes.pread_with(0, LE).unwrap(); println!("data: {:?}", &data); assert_eq!(data.id, 0xdeadbeefu32); let mut bytes2 = vec![0; ::std::mem::size_of::()]; bytes2.pwrite_with(data, 0, LE).unwrap(); let data: Data = bytes.pread_with(0, LE).unwrap(); let data2: Data = bytes2.pread_with(0, LE).unwrap(); assert_eq!(data, data2); let data: Data = bytes.cread_with(0, LE); assert_eq!(data, data2); } scroll_derive-0.10.1/src/lib.rs010064400017500001750000000255201355067671500145740ustar0000000000000000#![recursion_limit="1024"] extern crate proc_macro; use proc_macro2; use quote::quote; use proc_macro::TokenStream; fn impl_struct(name: &syn::Ident, fields: &syn::FieldsNamed) -> proc_macro2::TokenStream { let items: Vec<_> = fields.named.iter().map(|f| { let ident = &f.ident; let ty = &f.ty; match *ty { syn::Type::Array(ref array) => { match array.len { syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(ref int), ..}) => { let size = int.base10_parse::().unwrap(); quote! { #ident: { let mut __tmp: #ty = [0; #size]; src.gread_inout_with(offset, &mut __tmp, ctx)?; __tmp } } }, _ => panic!("Pread derive with bad array constexpr") } }, _ => { quote! { #ident: src.gread_with::<#ty>(offset, ctx)? } } } }).collect(); quote! { impl<'a> ::scroll::ctx::TryFromCtx<'a, ::scroll::Endian> for #name where #name: 'a { type Error = ::scroll::Error; #[inline] fn try_from_ctx(src: &'a [u8], ctx: ::scroll::Endian) -> ::scroll::export::result::Result<(Self, usize), Self::Error> { use ::scroll::Pread; let offset = &mut 0; let data = #name { #(#items,)* }; Ok((data, *offset)) } } } } fn impl_try_from_ctx(ast: &syn::DeriveInput) -> proc_macro2::TokenStream { let name = &ast.ident; match ast.data { syn::Data::Struct(ref data) => { match data.fields { syn::Fields::Named(ref fields) => { impl_struct(name, fields) }, _ => { panic!("Pread can only be derived for a regular struct with public fields") } } }, _ => panic!("Pread can only be derived for structs") } } #[proc_macro_derive(Pread)] pub fn derive_pread(input: TokenStream) -> TokenStream { let ast: syn::DeriveInput = syn::parse(input).unwrap(); let gen = impl_try_from_ctx(&ast); gen.into() } fn impl_try_into_ctx(name: &syn::Ident, fields: &syn::FieldsNamed) -> proc_macro2::TokenStream { let items: Vec<_> = fields.named.iter().map(|f| { let ident = &f.ident; let ty = &f.ty; match *ty { syn::Type::Array(_) => { quote! { for i in 0..self.#ident.len() { dst.gwrite_with(&self.#ident[i], offset, ctx)?; } } }, _ => { quote! { dst.gwrite_with(&self.#ident, offset, ctx)? } } } }).collect(); quote! { impl<'a> ::scroll::ctx::TryIntoCtx<::scroll::Endian> for &'a #name { type Error = ::scroll::Error; #[inline] fn try_into_ctx(self, dst: &mut [u8], ctx: ::scroll::Endian) -> ::scroll::export::result::Result { use ::scroll::Pwrite; let offset = &mut 0; #(#items;)*; Ok(*offset) } } impl ::scroll::ctx::TryIntoCtx<::scroll::Endian> for #name { type Error = ::scroll::Error; #[inline] fn try_into_ctx(self, dst: &mut [u8], ctx: ::scroll::Endian) -> ::scroll::export::result::Result { (&self).try_into_ctx(dst, ctx) } } } } fn impl_pwrite(ast: &syn::DeriveInput) -> proc_macro2::TokenStream { let name = &ast.ident; match ast.data { syn::Data::Struct(ref data) => { match data.fields { syn::Fields::Named(ref fields) => { impl_try_into_ctx(name, fields) }, _ => { panic!("Pwrite can only be derived for a regular struct with public fields") } } }, _ => panic!("Pwrite can only be derived for structs") } } #[proc_macro_derive(Pwrite)] pub fn derive_pwrite(input: TokenStream) -> TokenStream { let ast: syn::DeriveInput = syn::parse(input).unwrap(); let gen = impl_pwrite(&ast); gen.into() } fn size_with(name: &syn::Ident, fields: &syn::FieldsNamed) -> proc_macro2::TokenStream { let items: Vec<_> = fields.named.iter().map(|f| { let ty = &f.ty; match *ty { syn::Type::Array(ref array) => { let elem = &array.elem; match array.len { syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(ref int), ..}) => { let size = int.base10_parse::().unwrap(); quote! { (#size * <#elem>::size_with(ctx)) } }, _ => panic!("Pread derive with bad array constexpr") } }, _ => { quote! { <#ty>::size_with(ctx) } } } }).collect(); quote! { impl ::scroll::ctx::SizeWith<::scroll::Endian> for #name { #[inline] fn size_with(ctx: &::scroll::Endian) -> usize { 0 #(+ #items)* } } } } fn impl_size_with(ast: &syn::DeriveInput) -> proc_macro2::TokenStream { let name = &ast.ident; match ast.data { syn::Data::Struct(ref data) => { match data.fields { syn::Fields::Named(ref fields) => { size_with(name, fields) }, _ => { panic!("SizeWith can only be derived for a regular struct with public fields") } } }, _ => panic!("SizeWith can only be derived for structs") } } #[proc_macro_derive(SizeWith)] pub fn derive_sizewith(input: TokenStream) -> TokenStream { let ast: syn::DeriveInput = syn::parse(input).unwrap(); let gen = impl_size_with(&ast); gen.into() } fn impl_cread_struct(name: &syn::Ident, fields: &syn::FieldsNamed) -> proc_macro2::TokenStream { let items: Vec<_> = fields.named.iter().map(|f| { let ident = &f.ident; let ty = &f.ty; match *ty { syn::Type::Array(ref array) => { let arrty = &array.elem; match array.len { syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(ref int), ..}) => { let size = int.base10_parse::().unwrap(); let incr = quote! { ::scroll::export::mem::size_of::<#arrty>() }; quote! { #ident: { let mut __tmp: #ty = [0; #size]; for i in 0..__tmp.len() { __tmp[i] = src.cread_with(*offset, ctx); *offset += #incr; } __tmp } } }, _ => panic!("IOread derive with bad array constexpr") } }, _ => { let size = quote! { ::scroll::export::mem::size_of::<#ty>() }; quote! { #ident: { let res = src.cread_with::<#ty>(*offset, ctx); *offset += #size; res } } } } }).collect(); quote! { impl ::scroll::ctx::FromCtx<::scroll::Endian> for #name { #[inline] fn from_ctx(src: &[u8], ctx: ::scroll::Endian) -> Self { use ::scroll::Cread; let offset = &mut 0; let data = #name { #(#items,)* }; data } } } } fn impl_from_ctx(ast: &syn::DeriveInput) -> proc_macro2::TokenStream { let name = &ast.ident; match ast.data { syn::Data::Struct(ref data) => { match data.fields { syn::Fields::Named(ref fields) => { impl_cread_struct(name, fields) }, _ => { panic!("IOread can only be derived for a regular struct with public fields") } } }, _ => panic!("IOread can only be derived for structs") } } #[proc_macro_derive(IOread)] pub fn derive_ioread(input: TokenStream) -> TokenStream { let ast: syn::DeriveInput = syn::parse(input).unwrap(); let gen = impl_from_ctx(&ast); gen.into() } fn impl_into_ctx(name: &syn::Ident, fields: &syn::FieldsNamed) -> proc_macro2::TokenStream { let items: Vec<_> = fields.named.iter().map(|f| { let ident = &f.ident; let ty = &f.ty; let size = quote! { ::scroll::export::mem::size_of::<#ty>() }; match *ty { syn::Type::Array(ref array) => { let arrty = &array.elem; quote! { let size = ::scroll::export::mem::size_of::<#arrty>(); for i in 0..self.#ident.len() { dst.cwrite_with(self.#ident[i], *offset, ctx); *offset += size; } } }, _ => { quote! { dst.cwrite_with(self.#ident, *offset, ctx); *offset += #size; } } } }).collect(); quote! { impl<'a> ::scroll::ctx::IntoCtx<::scroll::Endian> for &'a #name { #[inline] fn into_ctx(self, dst: &mut [u8], ctx: ::scroll::Endian) { use ::scroll::Cwrite; let offset = &mut 0; #(#items;)*; () } } impl ::scroll::ctx::IntoCtx<::scroll::Endian> for #name { #[inline] fn into_ctx(self, dst: &mut [u8], ctx: ::scroll::Endian) { (&self).into_ctx(dst, ctx) } } } } fn impl_iowrite(ast: &syn::DeriveInput) -> proc_macro2::TokenStream { let name = &ast.ident; match ast.data { syn::Data::Struct(ref data) => { match data.fields { syn::Fields::Named(ref fields) => { impl_into_ctx(name, fields) }, _ => { panic!("IOwrite can only be derived for a regular struct with public fields") } } }, _ => panic!("IOwrite can only be derived for structs") } } #[proc_macro_derive(IOwrite)] pub fn derive_iowrite(input: TokenStream) -> TokenStream { let ast: syn::DeriveInput = syn::parse(input).unwrap(); let gen = impl_iowrite(&ast); gen.into() } scroll_derive-0.10.1/tests/tests.rs010064400017500001750000000100251355067671100155310ustar0000000000000000use scroll_derive::{Pread, Pwrite, SizeWith, IOread, IOwrite}; use scroll::{Pread, Pwrite, Cread, Cwrite, LE}; use scroll::ctx::SizeWith; #[derive(Debug, PartialEq, Pread, Pwrite)] struct Data { id: u32, timestamp: f64, } #[test] fn test_data (){ let bytes = [0xefu8, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0, 0, 224, 63]; let data: Data = bytes.pread_with(0, LE).unwrap(); println!("data: {:?}", &data); assert_eq!(data.id, 0xdeadbeefu32); assert_eq!(data.timestamp, 0.5f64); let mut bytes2 = vec![0; ::std::mem::size_of::()]; bytes2.pwrite_with(data, 0, LE).unwrap(); let data: Data = bytes.pread_with(0, LE).unwrap(); let data2: Data = bytes2.pread_with(0, LE).unwrap(); assert_eq!(data, data2); } #[derive(Debug, PartialEq, Pread, Pwrite)] struct Data2 { name: [u8; 32], } #[test] fn test_array (){ let bytes = [0u8; 64]; let data: Data2 = bytes.pread_with(0, LE).unwrap(); println!("data: {:?}", &data); } #[derive(Debug, PartialEq, Pread, Pwrite, SizeWith)] struct Data3 { name: u32, } #[test] fn test_sizewith (){ let bytes = [0u8; 64]; let data: Data3 = bytes.gread_with(&mut 0, LE).unwrap(); println!("data: {:?}", &data); } #[derive(Debug, PartialEq, IOread, IOwrite, SizeWith)] struct Data4 { name: u32, j: u16, arr: [u8; 2] } #[test] fn test_ioread (){ let bytes = [0, 1, 2, 3, 0xde, 0xed, 0xbe, 0xaf]; let data: Data4 = bytes.cread_with(0, LE); println!("data: {:?}", &data); assert_eq!(data.name, 50462976); assert_eq!(data.j, 0xedde); assert_eq!(data.arr, [0xbe, 0xaf]); } #[test] fn test_iowrite (){ let bytes = [0, 1, 2, 3, 0xde, 0xed, 0xbe, 0xaf]; let data: Data4 = bytes.cread_with(0, LE); println!("data: {:?}", &data); assert_eq!(data.name, 50462976); assert_eq!(data.j, 0xedde); assert_eq!(data.arr, [0xbe, 0xaf]); let mut bytes_null = [0u8; 8]; bytes_null.cwrite_with(&data, 0, LE); println!("bytes_null: {:?}", &bytes_null); println!("bytes : {:?}", &bytes); assert_eq!(bytes_null, bytes); let mut bytes_null = [0u8; 8]; bytes_null.cwrite_with(data, 0, LE); println!("bytes_null: {:?}", &bytes_null); println!("bytes : {:?}", &bytes); assert_eq!(bytes_null, bytes); } #[derive(Debug, PartialEq, Pread, SizeWith)] #[repr(C)] struct Data5 { name: u32, j: u16, arr1: [u8; 2], arr2: [u16; 2] } #[test] fn test_pread_arrays (){ let bytes = [0, 1, 2, 3, 0, 0, 0xde, 0xed, 0xad, 0xde, 0xef, 0xbe]; let data: Data5 = bytes.pread_with(0, LE).unwrap(); println!("data: {:?}", &data); assert_eq!(data.name, 50462976); assert_eq!(data.arr1, [0xde, 0xed]); assert_eq!(data.arr2, [0xdead, 0xbeef]); let offset = &mut 0; let data: Data5 = bytes.gread_with(offset, LE).unwrap(); println!("data: {:?}", &data); assert_eq!(data.name, 50462976); assert_eq!(data.arr1, [0xde, 0xed]); assert_eq!(data.arr2, [0xdead, 0xbeef]); assert_eq!(*offset, ::std::mem::size_of::()); } #[derive(Debug, PartialEq, Pread, SizeWith)] #[repr(C)] struct Data6 { id: u32, name: [u8; 5], } #[test] fn test_array_copy (){ let bytes = [0xde, 0xed, 0xef, 0xbe, 0x68, 0x65, 0x6c, 0x6c, 0x0]; let data: Data6 = bytes.pread_with(0, LE).unwrap(); let name: &str = data.name.pread(0).unwrap(); println!("data: {:?}", &data); println!("data.name: {:?}", name); assert_eq!(data.id, 0xbeefedde); assert_eq!(name, "hell"); } #[derive(Debug, PartialEq, Eq, Pread, Pwrite, SizeWith)] struct Data7A { pub y: u64, pub x: u32, } #[derive(Debug, PartialEq, Eq, Pread, Pwrite, SizeWith)] struct Data7B { pub a: Data7A, } #[test] fn test_nested_struct() { let b = Data7B { a: Data7A { y: 1, x: 2 } }; let size = Data7B::size_with(&LE); assert_eq!(size, 12); let mut bytes = vec![0; size]; let written = bytes.pwrite_with(&b, 0, LE).unwrap(); assert_eq!(written, size); let mut read = 0; let b2: Data7B = bytes.gread_with(&mut read, LE).unwrap(); assert_eq!(read, size); assert_eq!(b, b2); } scroll_derive-0.10.1/Cargo.lock0000644000000035450000000000000117700ustar00# This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "proc-macro2" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "quote" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "scroll_derive" version = "0.10.1" dependencies = [ "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "syn" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "unicode-xid" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27" "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" "checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"