Skip to content

Commit 45f1906

Browse files
committed
Merge pull request #85 from servo/no_static_assert
Remove usage of #[static_assert]
2 parents 5697161 + d16898a commit 45f1906

File tree

6 files changed

+35
-19
lines changed

6 files changed

+35
-19
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
sudo: false
22
language: rust
3+
rust: nightly
34
script:
45
- cargo test
56
- cargo clean

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

plugin/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#![crate_name="string_cache_plugin"]
1111
#![crate_type="dylib"]
1212

13-
#![feature(plugin_registrar, quote, box_syntax, static_assert)]
13+
#![feature(plugin_registrar, quote, box_syntax)]
1414
#![feature(rustc_private, slice_patterns)]
1515
#![deny(warnings)]
1616
#![allow(unused_imports)] // for quotes

shared/lib.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! the macros crate and the run-time library, in order to guarantee
1212
//! consistency.
1313
14-
#![feature(core, static_assert)]
14+
#![feature(core)]
1515
#![deny(warnings)]
1616

1717
use std::{mem, raw, intrinsics};
@@ -42,11 +42,9 @@ pub enum UnpackedAtom {
4242

4343
const STATIC_SHIFT_BITS: usize = 32;
4444

45+
#[cfg(target_endian = "little")] // Not implemented yet for big-endian
4546
#[inline(always)]
4647
unsafe fn inline_atom_slice(x: &u64) -> raw::Slice<u8> {
47-
#[static_assert]
48-
const _IS_LITTLE_ENDIAN: bool = cfg!(target_endian = "little");
49-
5048
let x: *const u64 = x;
5149
raw::Slice {
5250
data: (x as *const u8).offset(1),
@@ -82,8 +80,7 @@ impl UnpackedAtom {
8280

8381
#[inline(always)]
8482
pub unsafe fn from_packed(data: u64) -> UnpackedAtom {
85-
#[static_assert]
86-
const _DYNAMIC_IS_UNTAGGED: bool = DYNAMIC_TAG == 0;
83+
debug_assert!(DYNAMIC_TAG == 0); // Dynamic is untagged
8784

8885
match (data & 0xf) as u8 {
8986
DYNAMIC_TAG => Dynamic(data as *mut ()),

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#![crate_name = "string_cache"]
1111
#![crate_type = "rlib"]
1212

13-
#![feature(plugin, unsafe_no_drop_flag, static_assert)]
13+
#![feature(plugin, unsafe_no_drop_flag)]
1414
#![feature(core, collections, alloc, hash)]
1515
#![deny(warnings)]
1616
#![cfg_attr(test, feature(test))]

0 commit comments

Comments
 (0)