Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarton committed Mar 29, 2020
1 parent f36ce52 commit c34728d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
14 changes: 3 additions & 11 deletions src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use proc_macro2;
use ast;
use attr;
use matcher;
use paths;
use syn;
use utils;

Expand All @@ -31,7 +32,7 @@ pub fn derive_eq(input: &ast::Input) -> proc_macro2::TokenStream {
/// Derive `PartialEq` for `input`.
pub fn derive_partial_eq(input: &ast::Input) -> Result<proc_macro2::TokenStream, String> {
let discriminant_cmp = if let ast::Body::Enum(_) = input.body {
let discriminant_path = discriminant_path();
let discriminant_path = paths::discriminant_path();

quote!((#discriminant_path(&*self) == #discriminant_path(&*other)))
} else {
Expand Down Expand Up @@ -340,13 +341,4 @@ fn ordering_path() -> syn::Path {
} else {
parse_quote!(::std::cmp::Ordering)
}
}

/// Return the path of the `discriminant` function, that is `::std::mem::discriminant`.
fn discriminant_path() -> syn::Path {
if cfg!(feature = "use_core") {
parse_quote!(::core::mem::discriminant)
} else {
parse_quote!(::std::mem::discriminant)
}
}
}
4 changes: 3 additions & 1 deletion src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use proc_macro2;
use ast;
use attr;
use matcher;
use paths;
use syn;
use utils;

Expand All @@ -11,8 +12,9 @@ pub fn derive(input: &ast::Input) -> proc_macro2::TokenStream {
let hash_trait_path = hash_trait_path();

let discriminant = if let ast::Body::Enum(_) = input.body {
let discriminant = paths::discriminant_path();
Some(quote!(
#hash_trait_path::hash(&std::mem::discriminant(self), __state);
#hash_trait_path::hash(&#discriminant(self), __state);
))
} else {
None
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod debug;
mod default;
mod hash;
mod matcher;
mod paths;
mod utils;

use proc_macro::TokenStream;
Expand Down
10 changes: 10 additions & 0 deletions src/paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Contains some standard paths.

/// Return the path of the `discriminant` function, that is `::std::mem::discriminant`.
pub fn discriminant_path() -> syn::Path {
if cfg!(feature = "use_core") {
parse_quote!(::core::mem::discriminant)
} else {
parse_quote!(::std::mem::discriminant)
}
}

0 comments on commit c34728d

Please sign in to comment.