Skip to content

Commit d16898a

Browse files
committed
Update examples/summarize-events
1 parent e66ba20 commit d16898a

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

examples/summarize-events/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ rustc-serialize = "0"
1010

1111
[dependencies.string_cache]
1212
path = "../.."
13+
14+
[dependencies.string_cache_shared]
15+
path = "../../shared"

examples/summarize-events/src/main.rs

+26-11
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10-
#![feature(core)]
11-
1210
extern crate csv;
1311
extern crate string_cache;
12+
extern crate string_cache_shared;
1413
extern crate rustc_serialize;
1514

1615
use string_cache::Atom;
17-
use string_cache::atom::repr;
1816

1917
use std::{env, cmp};
20-
use std::num::FromPrimitive;
2118
use std::collections::hash_map::{HashMap, Entry};
2219
use std::path::Path;
2320

@@ -28,14 +25,32 @@ struct Event {
2825
string: Option<String>,
2926
}
3027

31-
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)]
32-
#[repr(u8)]
28+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
3329
enum Kind {
3430
Dynamic,
3531
Inline,
3632
Static,
3733
}
3834

35+
impl Kind {
36+
fn from_tag(tag: u8) -> Kind {
37+
match tag {
38+
string_cache_shared::DYNAMIC_TAG => Kind::Dynamic,
39+
string_cache_shared::INLINE_TAG => Kind::Inline,
40+
string_cache_shared::STATIC_TAG => Kind::Static,
41+
_ => panic!()
42+
}
43+
}
44+
45+
fn to_tag(self) -> u8 {
46+
match self {
47+
Kind::Dynamic => string_cache_shared::DYNAMIC_TAG,
48+
Kind::Inline => string_cache_shared::INLINE_TAG,
49+
Kind::Static => string_cache_shared::STATIC_TAG,
50+
}
51+
}
52+
}
53+
3954
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
4055
struct Summary {
4156
kind: Kind,
@@ -62,10 +77,10 @@ fn main() {
6277
match &ev.event[..] {
6378
"intern" => {
6479
let tag = (ev.id & 0xf) as u8;
65-
assert!(tag <= repr::STATIC_TAG);
80+
assert!(tag <= string_cache_shared::STATIC_TAG);
6681

6782
let string = match tag {
68-
repr::DYNAMIC_TAG => dynamic[&ev.id].clone(),
83+
string_cache_shared::DYNAMIC_TAG => dynamic[&ev.id].clone(),
6984

7085
// FIXME: We really shouldn't be allowed to do this. It's a memory-safety
7186
// hazard; the field is only public for the atom!() macro.
@@ -76,7 +91,7 @@ fn main() {
7691
Entry::Occupied(entry) => entry.into_mut().times += 1,
7792
Entry::Vacant(entry) => {
7893
entry.insert(Summary {
79-
kind: FromPrimitive::from_u8(tag).unwrap(),
94+
kind: Kind::from_tag(tag),
8095
times: 1,
8196
});
8297
}
@@ -118,14 +133,14 @@ fn main() {
118133
let mut by_kind = [0, 0, 0];
119134
for &(_, Summary { kind, times }) in &summary {
120135
total += times;
121-
by_kind[kind as usize] += times;
136+
by_kind[kind.to_tag() as usize] += times;
122137
}
123138

124139
println!("\n");
125140
println!("kind times pct");
126141
println!("------- ------- ----");
127142
for (k, &n) in by_kind.iter().enumerate() {
128-
let k: Kind = FromPrimitive::from_usize(k).unwrap();
143+
let k: Kind = Kind::from_tag(k as u8);
129144
print!("{:7?} {:7} {:4.1}",
130145
k, n, 100.0 * (n as f64) / (total as f64));
131146

0 commit comments

Comments
 (0)