-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[derive] use known crate name aliases #61
Changes from 7 commits
4a67533
531dd5d
459e29b
63f8f49
159f626
c3cadc6
38ef7df
9ab7c51
b292cc9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,32 +12,36 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#[cfg(not(feature = "std"))] | ||
use alloc::{ | ||
format, | ||
string::ToString, | ||
}; | ||
|
||
use proc_macro2::{ | ||
Span, | ||
TokenStream as TokenStream2, | ||
}; | ||
use quote::quote; | ||
use syn::Ident; | ||
|
||
pub fn wrap( | ||
ident: &Ident, | ||
trait_name: &'static str, | ||
impl_quote: TokenStream2, | ||
) -> TokenStream2 { | ||
let mut renamed = format!("_IMPL_{}_FOR_", trait_name); | ||
renamed.push_str(ident.to_string().trim_start_matches("r#")); | ||
let dummy_const = Ident::new(&renamed, Span::call_site()); | ||
pub fn wrap(impl_quote: TokenStream2) -> TokenStream2 { | ||
let include_scale_info = include_crate("scale-info", "_scale_info"); | ||
let include_parity_scale_codec = include_crate("parity-scale-codec", "_scale"); | ||
|
||
quote! { | ||
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)] | ||
const #dummy_const: () = { | ||
const _: () = { | ||
#include_scale_info | ||
#include_parity_scale_codec | ||
|
||
#impl_quote; | ||
}; | ||
} | ||
} | ||
|
||
/// Include a crate under a known alias, to be robust against renamed dependencies. | ||
fn include_crate(name: &str, alias: &str) -> proc_macro2::TokenStream { | ||
match proc_macro_crate::crate_name(name) { | ||
Ok(crate_name) => { | ||
let crate_name_ident = Ident::new(&crate_name, Span::call_site()); | ||
let crate_alias_ident = Ident::new(&alias, Span::call_site()); | ||
quote!( extern crate #crate_name_ident as #crate_alias_ident; ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not directly accessing the crate with its ident, instead of renaming it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree: #61 (comment) |
||
} | ||
Err(e) => syn::Error::new(Span::call_site(), &e).to_compile_error(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd personally prefer a solution where we'd still keep having absolute paths everything. let parity_scale_codec_alias = include_crate("parity-scale-codec");
quote! { :: #parity_scale_codec_alias ::rest::of::my::path } This solution would also not need yet another alias, e.g. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in general a bad style, to require some other crate than the macro providing crate in the
Cargo.toml
.Why aren't you re-exporting the required stuff from
scale-info
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not entirely sold on this.
While I agree that your proposal solves certain problems the
scale-info
crate works with multipleparity-scale-codec
versions. So re-exporting one of the supported versions would case a lock-in and potentially a duplication of dependencies in case thescale-info
user does not also use the same version.The reality is that
scale-info
andparity-scale-codec
are somehow tightly coupled by design (one does encoding, the other does encoding of the encoding) and could actually even live under the same crate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed that is a much better idea