Created
January 15, 2019 23:30
-
-
Save rust-play/2fbe6dfb7895bd5e1151723eaa2b4f0c 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
// test2.rs | |
use typed_arena::Arena; | |
use std::collections::HashMap; | |
pub type StringArena = Arena<String>; | |
pub struct PackageVersion<'a> { | |
item: &'a str, | |
} | |
impl<'a> PackageVersion<'a> { | |
pub fn name(&self) -> &str { | |
let pieces: Vec<&'a str> = self.item.split("-").collect(); | |
pieces[0] | |
} | |
pub fn version(&self) -> &str { | |
let pieces: Vec<&'a str> = self.item.split("-").collect(); | |
pieces[1] | |
} | |
pub fn fullname(&self) -> &str { | |
self.item | |
} | |
pub fn new<'b: 'a>(item: &'b str) -> Self { | |
Self { | |
item | |
} | |
} | |
} | |
//type Map = HashMap<String, Option<IdString>>; | |
pub type Map<'a> = HashMap<&'a str, PackageVersion<'a>>; | |
pub fn test2() { | |
let arena = StringArena::new(); | |
let mut map = Map::new(); | |
let v1 = arena.alloc("foo-0.1.0".into()); | |
let val1 = PackageVersion::new(v1) ; | |
let v2 = arena.alloc("foo-0.2.0".into()); | |
let val2 = PackageVersion::new(v1) ; | |
map.insert(v1, val1); | |
map.insert(v2, val2); | |
let v = map.get("foo-0.2.0").unwrap(); | |
println!("fullname:{} base:{} version:{}", v.fullname(), v.name(), v.version()); | |
} | |
// main.rs | |
mod test2; | |
use self::test2::test2; | |
fn main() { | |
test2(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment