@@ -9,21 +9,19 @@ use visitor::*;
99/// Freezes the layout of a struct to the current hash of its fields, ensuring that future
1010/// changes require updating the hash.
1111///
12- /// If you ever get in a loop where rust analyzer insists that the hash is wrong each time you
13- /// update, but `cargo check` is passing, you should use [`freeze_struct_ignore_ra`] instead.
12+ /// ```
13+ /// use subtensor_macros::freeze_struct;
14+ ///
15+ /// #[freeze_struct("13f75e4ea46b4e80")]
16+ /// #[derive(Copy, Clone, PartialEq, Eq)]
17+ /// pub struct MyStruct {
18+ /// pub a: u32,
19+ /// pub b: u64,
20+ /// }
21+ /// ```
1422#[ proc_macro_attribute]
1523pub fn freeze_struct ( attr : TokenStream , tokens : TokenStream ) -> TokenStream {
16- match freeze_struct_impl ( attr, tokens, false ) {
17- Ok ( item_struct) => item_struct. to_token_stream ( ) . into ( ) ,
18- Err ( err) => err. to_compile_error ( ) . into ( ) ,
19- }
20- }
21-
22- /// More permissive version of [`freeze_struct`] that ignores the hash check when running in
23- /// rust-analyzer since in some rare situations rust-analyzer will generate an incorrect hash code
24- #[ proc_macro_attribute]
25- pub fn freeze_struct_ignore_ra ( attr : TokenStream , tokens : TokenStream ) -> TokenStream {
26- match freeze_struct_impl ( attr, tokens, true ) {
24+ match freeze_struct_impl ( attr, tokens) {
2725 Ok ( item_struct) => item_struct. to_token_stream ( ) . into ( ) ,
2826 Err ( err) => err. to_compile_error ( ) . into ( ) ,
2927 }
@@ -32,7 +30,6 @@ pub fn freeze_struct_ignore_ra(attr: TokenStream, tokens: TokenStream) -> TokenS
3230fn freeze_struct_impl (
3331 attr : impl Into < TokenStream2 > ,
3432 tokens : impl Into < TokenStream2 > ,
35- ignore_ra : bool ,
3633) -> Result < ItemStruct > {
3734 let attr = attr. into ( ) ;
3835 let tokens = tokens. into ( ) ;
@@ -49,16 +46,13 @@ fn freeze_struct_impl(
4946 expected hashcode: `#[freeze_struct(\" {calculated_hash_hex}\" )]`") ,
5047 ) ) ;
5148 }
52- let hash_lit = parse2 :: < LitStr > ( attr) ?;
53- let provided_hash_hex = hash_lit. value ( ) . to_lowercase ( ) ;
49+
50+ let parsed_attr = parse2 :: < LitStr > ( attr) ?;
51+ let provided_hash_hex = parsed_attr. value ( ) . to_lowercase ( ) ;
5452
5553 let mut visitor = CleanDocComments :: new ( ) ;
5654 visit_item_struct_mut ( & mut visitor, & mut item_clone) ;
5755
58- if ignore_ra && is_rust_analyzer ( ) {
59- return Ok ( item) ;
60- }
61-
6256 if provided_hash_hex != calculated_hash_hex {
6357 return Err ( Error :: new_spanned ( item,
6458 format ! (
@@ -72,8 +66,3 @@ fn freeze_struct_impl(
7266 }
7367 Ok ( item)
7468}
75-
76- /// Returns true if the current build is being run by rust-analyzer.
77- fn is_rust_analyzer ( ) -> bool {
78- std:: env!( "RUSTC_WRAPPER" ) . contains ( "rust-analyzer" )
79- }
0 commit comments