Skip to content

Commit

Permalink
Add ability to re-export parity-scale-codec crate (#325)
Browse files Browse the repository at this point in the history
* use codec_crate_path for all derivable macros

* update README

* bump versions

* use `codec` attribute in tests

* remove redundant call to parse_quote
  • Loading branch information
gshep authored Mar 4, 2022
1 parent 6f13466 commit b860411
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 99 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "parity-scale-codec"
description = "SCALE - Simple Concatenating Aggregated Little Endians"
version = "3.0.0"
version = "3.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
license = "Apache-2.0"
repository = "https://github.com/paritytech/parity-scale-codec"
Expand All @@ -12,7 +12,7 @@ rust-version = "1.56.1"
[dependencies]
arrayvec = { version = "0.7", default-features = false }
serde = { version = "1.0.102", optional = true }
parity-scale-codec-derive = { path = "derive", version = "3.0.0", default-features = false, optional = true }
parity-scale-codec-derive = { path = "derive", version = "3.1.0", default-features = false, optional = true }
bitvec = { version = "1", default-features = false, features = [ "alloc" ], optional = true }
bytes = { version = "1", default-features = false, optional = true }
byte-slice-cast = { version = "1.0.0", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ The derive implementation supports the following attributes:
- `codec(index = 0)`: Needs to be placed above an enum variant to make the variant use the given
index when encoded. By default the index is determined by counting from `0` beginning wth the
first variant.
- `codec(crate = path::to::crate)`: Specify a path to the parity-scale-codec crate instance to use
when referring to Codec APIs from generated code. This is normally only applicable when invoking
re-exported Codec derives from a public macro in a different crate.


License: Apache-2.0
2 changes: 1 addition & 1 deletion derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "parity-scale-codec-derive"
description = "Serialization and deserialization derive macro for Parity SCALE Codec"
version = "3.0.0"
version = "3.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
license = "Apache-2.0"
edition = "2021"
Expand Down
20 changes: 10 additions & 10 deletions derive/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn quote(
type_name: &Ident,
type_generics: &TokenStream,
input: &TokenStream,
crate_ident: &TokenStream,
crate_path: &syn::Path,
) -> TokenStream {
match *data {
Data::Struct(ref data) => match data.fields {
Expand All @@ -40,7 +40,7 @@ pub fn quote(
&type_name.to_string(),
input,
&data.fields,
crate_ident,
crate_path,
),
Fields::Unit => {
quote_spanned! { data.fields.span() =>
Expand All @@ -67,7 +67,7 @@ pub fn quote(
&format!("{}::{}", type_name, name),
input,
&v.fields,
crate_ident,
crate_path,
);

quote_spanned! { v.span() =>
Expand Down Expand Up @@ -101,7 +101,7 @@ pub fn quote(
}
}

fn create_decode_expr(field: &Field, name: &str, input: &TokenStream, crate_ident: &TokenStream) -> TokenStream {
fn create_decode_expr(field: &Field, name: &str, input: &TokenStream, crate_path: &syn::Path) -> TokenStream {
let encoded_as = utils::get_encoded_as_type(field);
let compact = utils::is_compact(field);
let skip = utils::should_skip(&field.attrs);
Expand All @@ -122,7 +122,7 @@ fn create_decode_expr(field: &Field, name: &str, input: &TokenStream, crate_iden
quote_spanned! { field.span() =>
{
let #res = <
<#field_type as #crate_ident::HasCompact>::Type as #crate_ident::Decode
<#field_type as #crate_path::HasCompact>::Type as #crate_path::Decode
>::decode(#input);
match #res {
::core::result::Result::Err(e) => return ::core::result::Result::Err(e.chain(#err_msg)),
Expand All @@ -133,7 +133,7 @@ fn create_decode_expr(field: &Field, name: &str, input: &TokenStream, crate_iden
} else if let Some(encoded_as) = encoded_as {
quote_spanned! { field.span() =>
{
let #res = <#encoded_as as #crate_ident::Decode>::decode(#input);
let #res = <#encoded_as as #crate_path::Decode>::decode(#input);
match #res {
::core::result::Result::Err(e) => return ::core::result::Result::Err(e.chain(#err_msg)),
::core::result::Result::Ok(#res) => #res.into(),
Expand All @@ -146,7 +146,7 @@ fn create_decode_expr(field: &Field, name: &str, input: &TokenStream, crate_iden
let field_type = &field.ty;
quote_spanned! { field.span() =>
{
let #res = <#field_type as #crate_ident::Decode>::decode(#input);
let #res = <#field_type as #crate_path::Decode>::decode(#input);
match #res {
::core::result::Result::Err(e) => return ::core::result::Result::Err(e.chain(#err_msg)),
::core::result::Result::Ok(#res) => #res,
Expand All @@ -161,7 +161,7 @@ fn create_instance(
name_str: &str,
input: &TokenStream,
fields: &Fields,
crate_ident: &TokenStream,
crate_path: &syn::Path,
) -> TokenStream {
match *fields {
Fields::Named(ref fields) => {
Expand All @@ -171,7 +171,7 @@ fn create_instance(
Some(a) => format!("{}::{}", name_str, a),
None => format!("{}", name_str), // Should never happen, fields are named.
};
let decode = create_decode_expr(f, &field_name, input, crate_ident);
let decode = create_decode_expr(f, &field_name, input, crate_path);

quote_spanned! { f.span() =>
#name_ident: #decode
Expand All @@ -188,7 +188,7 @@ fn create_instance(
let recurse = fields.unnamed.iter().enumerate().map(|(i, f) | {
let field_name = format!("{}.{}", name_str, i);

create_decode_expr(f, &field_name, input, crate_ident)
create_decode_expr(f, &field_name, input, crate_path)
});

quote_spanned! { fields.span() =>
Expand Down
56 changes: 28 additions & 28 deletions derive/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type FieldsList = Punctuated<Field, Comma>;
fn encode_single_field(
field: &Field,
field_name: TokenStream,
crate_ident: &TokenStream,
crate_path: &syn::Path,
) -> TokenStream {
let encoded_as = utils::get_encoded_as_type(field);
let compact = utils::is_compact(field);
Expand All @@ -53,16 +53,16 @@ fn encode_single_field(
let field_type = &field.ty;
quote_spanned! {
field.span() => {
<<#field_type as #crate_ident::HasCompact>::Type as
#crate_ident::EncodeAsRef<'_, #field_type>>::RefType::from(#field_name)
<<#field_type as #crate_path::HasCompact>::Type as
#crate_path::EncodeAsRef<'_, #field_type>>::RefType::from(#field_name)
}
}
} else if let Some(encoded_as) = encoded_as {
let field_type = &field.ty;
quote_spanned! {
field.span() => {
<#encoded_as as
#crate_ident::EncodeAsRef<'_, #field_type>>::RefType::from(#field_name)
#crate_path::EncodeAsRef<'_, #field_type>>::RefType::from(#field_name)
}
}
} else {
Expand All @@ -75,19 +75,19 @@ fn encode_single_field(
let i_self = quote! { self };

quote_spanned! { field.span() =>
fn encode_to<__CodecOutputEdqy: #crate_ident::Output + ?::core::marker::Sized>(
fn encode_to<__CodecOutputEdqy: #crate_path::Output + ?::core::marker::Sized>(
&#i_self,
__codec_dest_edqy: &mut __CodecOutputEdqy
) {
#crate_ident::Encode::encode_to(&#final_field_variable, __codec_dest_edqy)
#crate_path::Encode::encode_to(&#final_field_variable, __codec_dest_edqy)
}

fn encode(&#i_self) -> #crate_ident::alloc::vec::Vec<::core::primitive::u8> {
#crate_ident::Encode::encode(&#final_field_variable)
fn encode(&#i_self) -> #crate_path::alloc::vec::Vec<::core::primitive::u8> {
#crate_path::Encode::encode(&#final_field_variable)
}

fn using_encoded<R, F: ::core::ops::FnOnce(&[::core::primitive::u8]) -> R>(&#i_self, f: F) -> R {
#crate_ident::Encode::using_encoded(&#final_field_variable, f)
#crate_path::Encode::using_encoded(&#final_field_variable, f)
}
}
}
Expand All @@ -96,7 +96,7 @@ fn encode_fields<F>(
dest: &TokenStream,
fields: &FieldsList,
field_name: F,
crate_ident: &TokenStream,
crate_path: &syn::Path,
) -> TokenStream where
F: Fn(usize, &Option<Ident>) -> TokenStream,
{
Expand All @@ -119,10 +119,10 @@ fn encode_fields<F>(
let field_type = &f.ty;
quote_spanned! {
f.span() => {
#crate_ident::Encode::encode_to(
#crate_path::Encode::encode_to(
&<
<#field_type as #crate_ident::HasCompact>::Type as
#crate_ident::EncodeAsRef<'_, #field_type>
<#field_type as #crate_path::HasCompact>::Type as
#crate_path::EncodeAsRef<'_, #field_type>
>::RefType::from(#field),
#dest,
);
Expand All @@ -132,10 +132,10 @@ fn encode_fields<F>(
let field_type = &f.ty;
quote_spanned! {
f.span() => {
#crate_ident::Encode::encode_to(
#crate_path::Encode::encode_to(
&<
#encoded_as as
#crate_ident::EncodeAsRef<'_, #field_type>
#crate_path::EncodeAsRef<'_, #field_type>
>::RefType::from(#field),
#dest,
);
Expand All @@ -147,7 +147,7 @@ fn encode_fields<F>(
}
} else {
quote_spanned! { f.span() =>
#crate_ident::Encode::encode_to(#field, #dest);
#crate_path::Encode::encode_to(#field, #dest);
}
}
});
Expand All @@ -157,7 +157,7 @@ fn encode_fields<F>(
}
}

fn try_impl_encode_single_field_optimisation(data: &Data, crate_ident: &TokenStream) -> Option<TokenStream> {
fn try_impl_encode_single_field_optimisation(data: &Data, crate_path: &syn::Path) -> Option<TokenStream> {
match *data {
Data::Struct(ref data) => {
match data.fields {
Expand All @@ -167,7 +167,7 @@ fn try_impl_encode_single_field_optimisation(data: &Data, crate_ident: &TokenStr
Some(encode_single_field(
field,
quote!(&self.#name),
crate_ident,
crate_path,
))
},
Fields::Unnamed(ref fields) if utils::filter_skip_unnamed(fields).count() == 1 => {
Expand All @@ -177,7 +177,7 @@ fn try_impl_encode_single_field_optimisation(data: &Data, crate_ident: &TokenStr
Some(encode_single_field(
field,
quote!(&self.#id),
crate_ident,
crate_path,
))
},
_ => None,
Expand All @@ -187,7 +187,7 @@ fn try_impl_encode_single_field_optimisation(data: &Data, crate_ident: &TokenStr
}
}

fn impl_encode(data: &Data, type_name: &Ident, crate_ident: &TokenStream) -> TokenStream {
fn impl_encode(data: &Data, type_name: &Ident, crate_path: &syn::Path) -> TokenStream {
let self_ = quote!(self);
let dest = &quote!(__codec_dest_edqy);
let encoding = match *data {
Expand All @@ -197,7 +197,7 @@ fn impl_encode(data: &Data, type_name: &Ident, crate_ident: &TokenStream) -> Tok
dest,
&fields.named,
|_, name| quote!(&#self_.#name),
crate_ident,
crate_path,
),
Fields::Unnamed(ref fields) => encode_fields(
dest,
Expand All @@ -206,7 +206,7 @@ fn impl_encode(data: &Data, type_name: &Ident, crate_ident: &TokenStream) -> Tok
let i = syn::Index::from(i);
quote!(&#self_.#i)
},
crate_ident,
crate_path,
),
Fields::Unit => quote!(),
}
Expand Down Expand Up @@ -242,7 +242,7 @@ fn impl_encode(data: &Data, type_name: &Ident, crate_ident: &TokenStream) -> Tok
dest,
&fields.named,
|a, b| field_name(a, b),
crate_ident,
crate_path,
);

quote_spanned! { f.span() =>
Expand All @@ -268,7 +268,7 @@ fn impl_encode(data: &Data, type_name: &Ident, crate_ident: &TokenStream) -> Tok
dest,
&fields.unnamed,
|a, b| field_name(a, b),
crate_ident,
crate_path,
);

quote_spanned! { f.span() =>
Expand Down Expand Up @@ -301,7 +301,7 @@ fn impl_encode(data: &Data, type_name: &Ident, crate_ident: &TokenStream) -> Tok
).to_compile_error(),
};
quote! {
fn encode_to<__CodecOutputEdqy: #crate_ident::Output + ?::core::marker::Sized>(
fn encode_to<__CodecOutputEdqy: #crate_path::Output + ?::core::marker::Sized>(
&#self_,
#dest: &mut __CodecOutputEdqy
) {
Expand All @@ -310,11 +310,11 @@ fn impl_encode(data: &Data, type_name: &Ident, crate_ident: &TokenStream) -> Tok
}
}

pub fn quote(data: &Data, type_name: &Ident, crate_ident: &TokenStream) -> TokenStream {
if let Some(implementation) = try_impl_encode_single_field_optimisation(data, crate_ident) {
pub fn quote(data: &Data, type_name: &Ident, crate_path: &syn::Path) -> TokenStream {
if let Some(implementation) = try_impl_encode_single_field_optimisation(data, crate_path) {
implementation
} else {
impl_encode(data, type_name, crate_ident)
impl_encode(data, type_name, crate_path)
}
}

Expand Down
Loading

0 comments on commit b860411

Please sign in to comment.