Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- #[derive(Debug)]
- struct SoftwareMi {
- content: String,
- // Make sure nobody can see these as 'static
- map: HashMap<&'static str, &'static str>,
- }
- impl SoftwareMi {
- // if exposing the entire HM, don't show 'static
- fn get_map(&self) -> &HashMap<&str, &str> {
- &self.map
- }
- }
- fn parse(content: String) -> SoftwareMi {
- let mut map = HashMap::new();
- for lines in content.lines() {
- let mut kv = lines.split("=").map(str::trim);
- let (key, val) = (kv.next().unwrap(), kv.next().unwrap());
- map.insert(unsafe { std::mem::transmute(key) }, unsafe {
- std::mem::transmute(val)
- });
- }
- SoftwareMi { content, map }
- }
- fn main() {
- let content = "foo=1 \
- baz=2 \
- ";
- let swmi = parse(content.into());
- println!("{:?}", swmi);
- // safe to move because the slices point to heap memory behind the String,
- // which has a stable address
- let new = swmi;
- println!("{:?}", new);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement