Created
January 15, 2019 15:36
-
-
Save rust-play/c8b4f05973c2535baf4db0cb8ddd0c20 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! 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