SHARE
TWEET

Untitled

a guest Jun 17th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::borrow::Cow;
  2. use std::collections::HashMap;
  3.  
  4. // note these cow<'a, str> can be serialized and
  5. // deserialized with serde with the same semantics as
  6. // String
  7. pub type AssignmentId<'a> = Cow<'a, str>;
  8. pub type PlayerId<'a> = Cow<'a, str>;
  9. pub type Data<'a> = Cow<'a, str>;
  10.  
  11. pub fn n<'a>(s: &'a str) -> Cow<'a, str> {
  12.     Cow::Borrowed(s)
  13. }
  14.  
  15.  
  16. #[derive(Debug)]
  17. struct MarioWorldDescriptor<'a> {
  18.     assignments: HashMap<AssignmentId<'a>, Data<'a>>,
  19.     player_data: HashMap<PlayerId<'a>, Data<'a>>,
  20. }
  21.  
  22. #[derive(Debug)]
  23. struct IndexedMarioWorld<'a, 'b: 'a> {
  24.     descriptor: MarioWorldDescriptor<'b>,
  25.     index1: HashMap<PlayerId<'a>, Data<'a>>,
  26.     index2: HashMap<AssignmentId<'a>, Data<'a>>
  27. }
  28.  
  29.  
  30. fn index_mario_world<'a, 'b: 'a>(descriptor: MarioWorldDescriptor<'b>) -> IndexedMarioWorld<'a, 'b> {
  31.  
  32.     let mut index1 = HashMap::new();
  33.     for (key, value) in descriptor.assignments.iter() {
  34.         assert!(index1.insert(key.clone(), value.clone()).is_none());
  35.     }
  36.     let mut index2 = HashMap::new();
  37.     for (key, value) in descriptor.player_data.iter() {
  38.         assert!(index2.insert(key.clone(), value.clone()).is_none());
  39.     }
  40.  
  41.     IndexedMarioWorld {
  42.         descriptor,
  43.         index1,
  44.         index2
  45.     }
  46. }
  47.  
  48. fn test_mario() {
  49.     let assignments = {
  50.         let mut a = HashMap::new();
  51.         a.insert(n("role-a"), n("tank"));
  52.         a.insert(n("role-b"), n("healer"));
  53.         a.insert(n("role-c"), n("dps"));
  54.         a
  55.     };
  56.  
  57.     let player_data = {
  58.         let mut p = HashMap::new();
  59.         p.insert(n("easytacos"), n("role-a"));
  60.         p.insert(n("earlypear"), n("role-b"));
  61.         p.insert(n("ritzyzucchini"), n("role-c"));
  62.         p.insert(n("crazyjalapeno"), n("role-c"));
  63.         p.insert(n("inertpotatosalad"), n("role-c"));
  64.  
  65.         p
  66.     };
  67.  
  68.     let descriptor = MarioWorldDescriptor {
  69.         assignments,
  70.         player_data
  71.     };
  72.     println!("{:#?}", descriptor);
  73.  
  74.     let indexed = index_mario_world(descriptor);
  75.  
  76.     println!("{:#?}", indexed);
  77. }
  78.  
  79.  
  80. fn main() {
  81.    
  82.  test_mario();    
  83. }
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