Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 15, 2019 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/c8b4f05973c2535baf4db0cb8ddd0c20 to your computer and use it in GitHub Desktop.
Save rust-play/c8b4f05973c2535baf4db0cb8ddd0c20 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
//! test of using an arena for managing info
use id_arena::{Arena, Id};
use std::collections::HashMap;
type IdString = Id<String>;
type StringArena = Arena<String>;
struct PackageVersion {
id: IdString,
}
impl PackageVersion {
pub fn name<'a>(&self, arena:&'a StringArena) -> &'a str {
let item = arena.get(self.id).unwrap();
let pieces: Vec<&str> = item.split("-").collect();
pieces[0]
}
pub fn new(id: IdString) -> Self {
Self {
id
}
}
}
//type Map = HashMap<String, Option<IdString>>;
type Map<'a> = HashMap<&'a str, IdString>;
fn add<'a>(arena: &mut StringArena, map: &'a mut Map, package:String) {
let id = arena.alloc(package);
let refval = arena.get(id).unwrap();
map.insert(refval, id);
}
fn main() {
let mut arena = StringArena::new();
let mut map :Map = HashMap::new();
add(&mut arena, &mut map, "foo-0.1.0".into());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment