rlp_derive: cleanup (#11446)
* rlp_derive: update syn & co * rlp_derive: remove dummy_const * rlp_derive: remove unused attirubutes * rlp-derive: change authors
This commit is contained in:
parent
1b96f98296
commit
e4a4a3cb55
6
Cargo.lock
generated
6
Cargo.lock
generated
@ -4078,10 +4078,10 @@ dependencies = [
|
|||||||
name = "rlp_derive"
|
name = "rlp_derive"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"rlp 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rlp 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)",
|
"syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rlp_derive"
|
name = "rlp_derive"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["debris <marek.kotewicz@gmail.com>"]
|
authors = ["Parity Technologies <admin@parity.io>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "rlp_derive"
|
name = "rlp_derive"
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
syn = "0.15"
|
syn = "1.0.14"
|
||||||
quote = "0.6"
|
quote = "1.0.2"
|
||||||
proc-macro2 = "0.4"
|
proc-macro2 = "1.0.8"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rlp = "0.4.0"
|
rlp = "0.4.0"
|
||||||
|
@ -14,8 +14,8 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use syn;
|
use proc_macro2::TokenStream;
|
||||||
use proc_macro2::{TokenStream, Span};
|
use quote::quote;
|
||||||
|
|
||||||
struct ParseQuotes {
|
struct ParseQuotes {
|
||||||
single: TokenStream,
|
single: TokenStream,
|
||||||
@ -45,10 +45,14 @@ pub fn impl_decodable(ast: &syn::DeriveInput) -> TokenStream {
|
|||||||
_ => panic!("#[derive(RlpDecodable)] is only defined for structs."),
|
_ => panic!("#[derive(RlpDecodable)] is only defined for structs."),
|
||||||
};
|
};
|
||||||
|
|
||||||
let stmts: Vec<_> = body.fields.iter().enumerate().map(decodable_field_map).collect();
|
let stmts: Vec<_> = body
|
||||||
|
.fields
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(decodable_field_map)
|
||||||
|
.collect();
|
||||||
let name = &ast.ident;
|
let name = &ast.ident;
|
||||||
|
|
||||||
let dummy_const = syn::Ident::new(&format!("_IMPL_RLP_DECODABLE_FOR_{}", name), Span::call_site());
|
|
||||||
let impl_block = quote! {
|
let impl_block = quote! {
|
||||||
impl rlp::Decodable for #name {
|
impl rlp::Decodable for #name {
|
||||||
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
|
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
|
||||||
@ -62,8 +66,7 @@ pub fn impl_decodable(ast: &syn::DeriveInput) -> TokenStream {
|
|||||||
};
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
const _: () = {
|
||||||
const #dummy_const: () = {
|
|
||||||
extern crate rlp;
|
extern crate rlp;
|
||||||
#impl_block
|
#impl_block
|
||||||
};
|
};
|
||||||
@ -88,7 +91,6 @@ pub fn impl_decodable_wrapper(ast: &syn::DeriveInput) -> TokenStream {
|
|||||||
|
|
||||||
let name = &ast.ident;
|
let name = &ast.ident;
|
||||||
|
|
||||||
let dummy_const = syn::Ident::new(&format!("_IMPL_RLP_DECODABLE_FOR_{}", name), Span::call_site());
|
|
||||||
let impl_block = quote! {
|
let impl_block = quote! {
|
||||||
impl rlp::Decodable for #name {
|
impl rlp::Decodable for #name {
|
||||||
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
|
fn decode(rlp: &rlp::Rlp) -> Result<Self, rlp::DecoderError> {
|
||||||
@ -102,8 +104,7 @@ pub fn impl_decodable_wrapper(ast: &syn::DeriveInput) -> TokenStream {
|
|||||||
};
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
const _: () = {
|
||||||
const #dummy_const: () = {
|
|
||||||
extern crate rlp;
|
extern crate rlp;
|
||||||
#impl_block
|
#impl_block
|
||||||
};
|
};
|
||||||
@ -130,7 +131,12 @@ fn decodable_field(index: usize, field: &syn::Field, quotes: ParseQuotes) -> Tok
|
|||||||
|
|
||||||
match field.ty {
|
match field.ty {
|
||||||
syn::Type::Path(ref path) => {
|
syn::Type::Path(ref path) => {
|
||||||
let ident = &path.path.segments.first().expect("there must be at least 1 segment").value().ident;
|
let ident = &path
|
||||||
|
.path
|
||||||
|
.segments
|
||||||
|
.first()
|
||||||
|
.expect("there must be at least 1 segment")
|
||||||
|
.ident;
|
||||||
if &ident.to_string() == "Vec" {
|
if &ident.to_string() == "Vec" {
|
||||||
if quotes.takes_index {
|
if quotes.takes_index {
|
||||||
quote! { #id: #list(#index)?, }
|
quote! { #id: #list(#index)?, }
|
||||||
@ -144,7 +150,7 @@ fn decodable_field(index: usize, field: &syn::Field, quotes: ParseQuotes) -> Tok
|
|||||||
quote! { #id: #single()?, }
|
quote! { #id: #single()?, }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
_ => panic!("rlp_derive not supported"),
|
_ => panic!("rlp_derive not supported"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,8 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use syn;
|
use proc_macro2::TokenStream;
|
||||||
use proc_macro2::{TokenStream, Span};
|
use quote::quote;
|
||||||
|
|
||||||
pub fn impl_encodable(ast: &syn::DeriveInput) -> TokenStream {
|
pub fn impl_encodable(ast: &syn::DeriveInput) -> TokenStream {
|
||||||
let body = match ast.data {
|
let body = match ast.data {
|
||||||
@ -23,12 +23,16 @@ pub fn impl_encodable(ast: &syn::DeriveInput) -> TokenStream {
|
|||||||
_ => panic!("#[derive(RlpEncodable)] is only defined for structs."),
|
_ => panic!("#[derive(RlpEncodable)] is only defined for structs."),
|
||||||
};
|
};
|
||||||
|
|
||||||
let stmts: Vec<_> = body.fields.iter().enumerate().map(encodable_field_map).collect();
|
let stmts: Vec<_> = body
|
||||||
|
.fields
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(encodable_field_map)
|
||||||
|
.collect();
|
||||||
let name = &ast.ident;
|
let name = &ast.ident;
|
||||||
|
|
||||||
let stmts_len = stmts.len();
|
let stmts_len = stmts.len();
|
||||||
let stmts_len = quote! { #stmts_len };
|
let stmts_len = quote! { #stmts_len };
|
||||||
let dummy_const = syn::Ident::new(&format!("_IMPL_RLP_ENCODABLE_FOR_{}", name), Span::call_site());
|
|
||||||
let impl_block = quote! {
|
let impl_block = quote! {
|
||||||
impl rlp::Encodable for #name {
|
impl rlp::Encodable for #name {
|
||||||
fn rlp_append(&self, stream: &mut rlp::RlpStream) {
|
fn rlp_append(&self, stream: &mut rlp::RlpStream) {
|
||||||
@ -39,8 +43,7 @@ pub fn impl_encodable(ast: &syn::DeriveInput) -> TokenStream {
|
|||||||
};
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
const _: () = {
|
||||||
const #dummy_const: () = {
|
|
||||||
extern crate rlp;
|
extern crate rlp;
|
||||||
#impl_block
|
#impl_block
|
||||||
};
|
};
|
||||||
@ -65,7 +68,6 @@ pub fn impl_encodable_wrapper(ast: &syn::DeriveInput) -> TokenStream {
|
|||||||
|
|
||||||
let name = &ast.ident;
|
let name = &ast.ident;
|
||||||
|
|
||||||
let dummy_const = syn::Ident::new(&format!("_IMPL_RLP_ENCODABLE_FOR_{}", name), Span::call_site());
|
|
||||||
let impl_block = quote! {
|
let impl_block = quote! {
|
||||||
impl rlp::Encodable for #name {
|
impl rlp::Encodable for #name {
|
||||||
fn rlp_append(&self, stream: &mut rlp::RlpStream) {
|
fn rlp_append(&self, stream: &mut rlp::RlpStream) {
|
||||||
@ -75,8 +77,7 @@ pub fn impl_encodable_wrapper(ast: &syn::DeriveInput) -> TokenStream {
|
|||||||
};
|
};
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
const _: () = {
|
||||||
const #dummy_const: () = {
|
|
||||||
extern crate rlp;
|
extern crate rlp;
|
||||||
#impl_block
|
#impl_block
|
||||||
};
|
};
|
||||||
@ -100,24 +101,38 @@ fn encodable_field(index: usize, field: &syn::Field) -> TokenStream {
|
|||||||
|
|
||||||
match field.ty {
|
match field.ty {
|
||||||
syn::Type::Path(ref path) => {
|
syn::Type::Path(ref path) => {
|
||||||
let top_segment = path.path.segments.first().expect("there must be at least 1 segment");
|
let top_segment = path
|
||||||
let ident = &top_segment.value().ident;
|
.path
|
||||||
|
.segments
|
||||||
|
.first()
|
||||||
|
.expect("there must be at least 1 segment");
|
||||||
|
let ident = &top_segment.ident;
|
||||||
if &ident.to_string() == "Vec" {
|
if &ident.to_string() == "Vec" {
|
||||||
let inner_ident = match top_segment.value().arguments {
|
let inner_ident = match top_segment.arguments {
|
||||||
syn::PathArguments::AngleBracketed(ref angle) => {
|
syn::PathArguments::AngleBracketed(ref angle) => {
|
||||||
let ty = angle.args.first().expect("Vec has only one angle bracketed type; qed");
|
let ty = angle
|
||||||
match **ty.value() {
|
.args
|
||||||
syn::GenericArgument::Type(syn::Type::Path(ref path)) => &path.path.segments.first().expect("there must be at least 1 segment").value().ident,
|
.first()
|
||||||
|
.expect("Vec has only one angle bracketed type; qed");
|
||||||
|
match *ty {
|
||||||
|
syn::GenericArgument::Type(syn::Type::Path(ref path)) => {
|
||||||
|
&path
|
||||||
|
.path
|
||||||
|
.segments
|
||||||
|
.first()
|
||||||
|
.expect("there must be at least 1 segment")
|
||||||
|
.ident
|
||||||
|
}
|
||||||
_ => panic!("rlp_derive not supported"),
|
_ => panic!("rlp_derive not supported"),
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
_ => unreachable!("Vec has only one angle bracketed type; qed"),
|
_ => unreachable!("Vec has only one angle bracketed type; qed"),
|
||||||
};
|
};
|
||||||
quote! { stream.append_list::<#inner_ident, _>(&#id); }
|
quote! { stream.append_list::<#inner_ident, _>(&#id); }
|
||||||
} else {
|
} else {
|
||||||
quote! { stream.append(&#id); }
|
quote! { stream.append(&#id); }
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
_ => panic!("rlp_derive not supported"),
|
_ => panic!("rlp_derive not supported"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,17 +15,13 @@
|
|||||||
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
extern crate proc_macro;
|
extern crate proc_macro;
|
||||||
extern crate proc_macro2;
|
|
||||||
extern crate syn;
|
|
||||||
#[macro_use]
|
|
||||||
extern crate quote;
|
|
||||||
|
|
||||||
mod en;
|
|
||||||
mod de;
|
mod de;
|
||||||
|
mod en;
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
|
||||||
use en::{impl_encodable, impl_encodable_wrapper};
|
|
||||||
use de::{impl_decodable, impl_decodable_wrapper};
|
use de::{impl_decodable, impl_decodable_wrapper};
|
||||||
|
use en::{impl_encodable, impl_encodable_wrapper};
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
|
||||||
#[proc_macro_derive(RlpEncodable)]
|
#[proc_macro_derive(RlpEncodable)]
|
||||||
pub fn encodable(input: TokenStream) -> TokenStream {
|
pub fn encodable(input: TokenStream) -> TokenStream {
|
||||||
|
@ -14,11 +14,8 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
extern crate rlp;
|
use rlp::{decode, encode};
|
||||||
#[macro_use]
|
use rlp_derive::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper};
|
||||||
extern crate rlp_derive;
|
|
||||||
|
|
||||||
use rlp::{encode, decode};
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, RlpEncodable, RlpDecodable)]
|
#[derive(Debug, PartialEq, RlpEncodable, RlpDecodable)]
|
||||||
struct Foo {
|
struct Foo {
|
||||||
@ -32,9 +29,7 @@ struct FooWrapper {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_encode_foo() {
|
fn test_encode_foo() {
|
||||||
let foo = Foo {
|
let foo = Foo { a: "cat".into() };
|
||||||
a: "cat".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let expected = vec![0xc4, 0x83, b'c', b'a', b't'];
|
let expected = vec![0xc4, 0x83, b'c', b'a', b't'];
|
||||||
let out = encode(&foo);
|
let out = encode(&foo);
|
||||||
@ -46,9 +41,7 @@ fn test_encode_foo() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_encode_foo_wrapper() {
|
fn test_encode_foo_wrapper() {
|
||||||
let foo = FooWrapper {
|
let foo = FooWrapper { a: "cat".into() };
|
||||||
a: "cat".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let expected = vec![0x83, b'c', b'a', b't'];
|
let expected = vec![0x83, b'c', b'a', b't'];
|
||||||
let out = encode(&foo);
|
let out = encode(&foo);
|
||||||
|
Loading…
Reference in New Issue
Block a user