Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of #[static_assert] #85

Merged
merged 3 commits into from
Jun 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sudo: false
language: rust
rust: nightly
script:
- cargo test
- cargo clean
Expand Down
3 changes: 3 additions & 0 deletions examples/summarize-events/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ rustc-serialize = "0"

[dependencies.string_cache]
path = "../.."

[dependencies.string_cache_shared]
path = "../../shared"
37 changes: 26 additions & 11 deletions examples/summarize-events/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(core)]

extern crate csv;
extern crate string_cache;
extern crate string_cache_shared;
extern crate rustc_serialize;

use string_cache::Atom;
use string_cache::atom::repr;

use std::{env, cmp};
use std::num::FromPrimitive;
use std::collections::hash_map::{HashMap, Entry};
use std::path::Path;

Expand All @@ -28,14 +25,32 @@ struct Event {
string: Option<String>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)]
#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
enum Kind {
Dynamic,
Inline,
Static,
}

impl Kind {
fn from_tag(tag: u8) -> Kind {
match tag {
string_cache_shared::DYNAMIC_TAG => Kind::Dynamic,
string_cache_shared::INLINE_TAG => Kind::Inline,
string_cache_shared::STATIC_TAG => Kind::Static,
_ => panic!()
}
}

fn to_tag(self) -> u8 {
match self {
Kind::Dynamic => string_cache_shared::DYNAMIC_TAG,
Kind::Inline => string_cache_shared::INLINE_TAG,
Kind::Static => string_cache_shared::STATIC_TAG,
}
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Summary {
kind: Kind,
Expand All @@ -62,10 +77,10 @@ fn main() {
match &ev.event[..] {
"intern" => {
let tag = (ev.id & 0xf) as u8;
assert!(tag <= repr::STATIC_TAG);
assert!(tag <= string_cache_shared::STATIC_TAG);

let string = match tag {
repr::DYNAMIC_TAG => dynamic[&ev.id].clone(),
string_cache_shared::DYNAMIC_TAG => dynamic[&ev.id].clone(),

// FIXME: We really shouldn't be allowed to do this. It's a memory-safety
// hazard; the field is only public for the atom!() macro.
Expand All @@ -76,7 +91,7 @@ fn main() {
Entry::Occupied(entry) => entry.into_mut().times += 1,
Entry::Vacant(entry) => {
entry.insert(Summary {
kind: FromPrimitive::from_u8(tag).unwrap(),
kind: Kind::from_tag(tag),
times: 1,
});
}
Expand Down Expand Up @@ -118,14 +133,14 @@ fn main() {
let mut by_kind = [0, 0, 0];
for &(_, Summary { kind, times }) in &summary {
total += times;
by_kind[kind as usize] += times;
by_kind[kind.to_tag() as usize] += times;
}

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

Expand Down
2 changes: 1 addition & 1 deletion plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#![crate_name="string_cache_plugin"]
#![crate_type="dylib"]

#![feature(plugin_registrar, quote, box_syntax, static_assert)]
#![feature(plugin_registrar, quote, box_syntax)]
#![feature(rustc_private, slice_patterns)]
#![deny(warnings)]
#![allow(unused_imports)] // for quotes
Expand Down
9 changes: 3 additions & 6 deletions shared/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! the macros crate and the run-time library, in order to guarantee
//! consistency.

#![feature(core, static_assert)]
#![feature(core)]
#![deny(warnings)]

use std::{mem, raw, intrinsics};
Expand Down Expand Up @@ -42,11 +42,9 @@ pub enum UnpackedAtom {

const STATIC_SHIFT_BITS: usize = 32;

#[cfg(target_endian = "little")] // Not implemented yet for big-endian
#[inline(always)]
unsafe fn inline_atom_slice(x: &u64) -> raw::Slice<u8> {
#[static_assert]
const _IS_LITTLE_ENDIAN: bool = cfg!(target_endian = "little");

let x: *const u64 = x;
raw::Slice {
data: (x as *const u8).offset(1),
Expand Down Expand Up @@ -82,8 +80,7 @@ impl UnpackedAtom {

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

match (data & 0xf) as u8 {
DYNAMIC_TAG => Dynamic(data as *mut ()),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#![crate_name = "string_cache"]
#![crate_type = "rlib"]

#![feature(plugin, unsafe_no_drop_flag, static_assert)]
#![feature(plugin, unsafe_no_drop_flag)]
#![feature(core, collections, alloc, hash)]
#![deny(warnings)]
#![cfg_attr(test, feature(test))]
Expand Down