daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::collections::HashMap;
  2.  
  3. #[derive(Debug)]
  4. struct SoftwareMi {
  5.     content: String,
  6.     // Make sure nobody can see these as 'static
  7.     map: HashMap<&'static str, &'static str>,
  8. }
  9.  
  10. impl SoftwareMi {
  11.     // if exposing the entire HM, don't show 'static
  12.     fn get_map(&self) -> &HashMap<&str, &str> {
  13.         &self.map
  14.     }
  15. }
  16.  
  17. fn parse(content: String) -> SoftwareMi {
  18.     let mut map = HashMap::new();
  19.     for lines in content.lines() {
  20.         let mut kv = lines.split("=").map(str::trim);
  21.         let (key, val) = (kv.next().unwrap(), kv.next().unwrap());
  22.  
  23.         map.insert(unsafe { std::mem::transmute(key) }, unsafe {
  24.             std::mem::transmute(val)
  25.         });
  26.     }
  27.     SoftwareMi { content, map }
  28. }
  29.  
  30. fn main() {
  31.     let content = "foo=1 \
  32.                    baz=2 \
  33.                    ";
  34.     let swmi = parse(content.into());
  35.     println!("{:?}", swmi);
  36.     // safe to move because the slices point to heap memory behind the String,
  37.     // which has a stable address
  38.     let new = swmi;
  39.     println!("{:?}", new);
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top