title: Create a global font cache
body: <p>I'm using <a href="https://github.com/RazrFalcon/rustybuzz" rel="nofollow noreferrer">rustybuzz</a> and <a href="https://github.com/rustwasm/wasm-bindgen" rel="nofollow noreferrer">wasm-bindgen</a> to typeset text on an HTML5 canvas. I would like to create a global font cache, indexed by <code>String</code>s, so that I can load and parse font files once and then reuse them multiple times (cue comments about how global variables are bad... if anyone has a better way to do this, please let me know). Specifically, I want some variation of a <code>HashMap&lt;String, rustybuzz::Face&gt;</code> that I can access anywhere. I then want to expose a <code>register_font</code> function to the JavaScript side so I can load in <code>ArrayBuffers</code>, something like:</p>
  <pre class="lang-rust prettyprint-override"><code>#[wasm_bindgen]
  pub fn register_font(name: &amp;str, font_data: &amp;[u8]) {
    MY_FONT_CACHE_SOMEHOW.insert(
      name.to_string(),
      rustybuzz::Face::from_slice(&amp;font_data, 0).unwrap()
    );
  }
  </code></pre>
  <p>And, for completion, I'd like an internal <code>get_font</code> function to retrieve:</p>
  <pre class="lang-rust prettyprint-override"><code>fn get_font(name: &amp;String) -&gt; rustybuzz::Face {
    MY_FONT_CACHE_SOMEHOW.get(name).unwrap()
  }
  </code></pre>
  <p>I know I've got issues with variable lifetimes, I just don't know how to solve them. The <code>rustybuzz::Face</code> struct just references its internal data, it doesn't own it. <code>wasm-bindgen</code> doesn't support marking the incoming <code>ArrayBuffer</code>/<code>[u8]</code> on the <code>register_font</code> function as <code>'static</code>, which seems to be one of the main problems. But I'm new to this whole rust thing. Anyone know how to do this (or a better way to do this)?</p>