title: Static references, multiple crates, and data from compile time to runtime
body: <p>I have some <code>multi-crate</code> Rust library project. This includes one module for <code>proc-macro</code> definitions.</p>
  <p>For sake of simplicity, let's supose that the project contains 3 diferent crates, <code>crate_a, crate_b, crate_c</code>.</p>
  <ul>
  <li><code>crate_a</code> it's mainly used to expose the public API's, organizing reexports...</li>
  <li><code>crate_b</code> is where the <code>proc-macro</code> lives.</li>
  <li><code>crate_c</code> contains a static reference of contents.</li>
  </ul>
  <p><code>crate_a</code> depends on both others crates. <code>crate_b</code> depends on <code>crate_c</code>.</p>
  <p>The purpose of <code>crate_c</code> it's to hold data that one macro of the <code>crate_b</code> generates at compile time, and them wire them into a client <code>main()</code> function.</p>
  <p>Code on <code>lib.rs</code> of <code>crate_c</code>:</p>
  <pre><code>use std::sync::Mutex;
  use lazy_static::lazy_static;
  
  lazy_static! {
      pub static ref REGISTER_ENTITIES: Mutex&lt;Vec&lt;RegisterEntity&lt;'static&gt;&gt;&gt; = Mutex::new(Vec::new());
  pub static ref QUERIES_TO_EXECUTE: Mutex&lt;Vec&lt;String&gt;&gt; = Mutex::new(Vec::new());
  
  }
  </code></pre>
  <p>A example of the macro on <code>crate_b</code>:</p>
  <pre><code>#[proc_macro_attribute]
  pub fn macro(_meta: CompilerTokenStream, input: CompilerTokenStream) -&gt; CompilerTokenStream {
      // Parses the function that this attribute is attached to
      let func_res = syn::parse::&lt;FunctionParser&gt;(input);
      
      if func_res.is_err() {
          return quote! { fn main() {} }.into()
      }
      
      let func = func_res.ok().unwrap();
      let sign = func.clone().sig;
      let body = func.clone().block.stmts;
  
      let rt = tokio::runtime::Runtime::new().unwrap();
      rt.block_on(async {
          Handler::run().await;  // Execute code that stores data on the `REGISTER_ENTITIES` static ref
      });
  
      // Saves data on the `QUERIES_TO_EXECUTE` static ref
      let mut queries_tokens: Vec&lt;TokenStream&gt; = Vec::new();
      wire_queries_to_execute(&amp;mut queries_tokens);
      
      // The final code wired in main()
      quote! {
          #[tokio::main]
          async #sign {
              {
                  #(#queries_tokens)*
              }
              #(#body)*
          }
      }.into()
  
  }
  </code></pre>
  <p>where <code>wire_queries_to_execute</code> reduced for the example:</p>
  <pre><code>pub fn wire_queries_to_execute(manager_tokens: &amp;mut Vec&lt;TokenStream&gt;) {
      let mut queries = String::new();
  
      for query in (*QUERIES_TO_EXECUTE).lock().unwrap().iter() {
          queries.push_str(&amp;(query.to_owned() + &quot;-&gt;&quot;));
      }
      
      let tokens = quote! {
          *QUERIES_TO_EXECUTE.lock().unwrap() = #queries
              .split(&quot;-&gt;&quot;)
              .map(str::to_string)
              .collect::&lt;Vec&lt;String&gt;&gt;();
  
          DatabaseSyncOperations::from_query_register().await;
      };
      
      manager_tokens.push(tokens)    
  }
  </code></pre>
  <p>and then just the code used by some consumer of the library:</p>
  <pre><code>// use statements...
  
  fn main() {
      {  
          // here run, for example, queries to the database wired by the macro
      }
  
      // User's code
  
  }
  </code></pre>
  <p>I can write some data to the static ref at compile time (when the macro runs) and then be retrieved. Is this always true?</p>
  <p>Edit: For clarification, let's put an example:</p>
  <p>At compile time macro saves some data into the <code>static ref</code>. Then, when the code of the user runs, it's able to fetch that data of the <code>static ref</code>. I wanted to know if there's some possibility of overwrite the data stored  in the <code>static ref</code> by another program (or service, or whatever element) in the time between the compilation and the start the program.</p>
  <ul>
  <li>Suppose that you, as user of my library, compile your binary, wait to hours, and then executes your binary.</li>
  </ul>
  <p>What happens to that data? Change the value of two hours for the amount of time that you want, for the example.</p>
  <p>How I can ensure that this data will be there, waiting for the runtime execution of the binary? Is possible that another program, thread or service of the OS, decides to allocate some data where resides the data of the <code>static ref</code>? Or it will be there until a reboot or a shutdown?</p>
  <p>Even more, what it's the real connection between the data stored at runtime, and the one retrieved at runtime?
  Or just, because it's a <code>static ref</code> it's stored inside the binary, and no such case as present above can happens?</p>