Skip to content

Commit 2c8ab28

Browse files
committed
Move BuiltinBounds underlying implementation to the data structure crate
1 parent 2b0f13f commit 2c8ab28

File tree

3 files changed

+111
-26
lines changed

3 files changed

+111
-26
lines changed

src/librustc/middle/ty.rs

+13-26
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,15 @@ use util::nodemap::{NodeMap, NodeSet, DefIdMap, DefIdSet};
7070
use util::nodemap::FnvHashMap;
7171
use util::num::ToPrimitive;
7272

73+
use rustc_data_structures::bitset::{U8BitSet, BitSet, BitSetIter};
74+
7375
use arena::TypedArena;
7476
use std::borrow::{Borrow, Cow};
7577
use std::cell::{Cell, RefCell, Ref};
7678
use std::cmp;
7779
use std::fmt;
7880
use std::hash::{Hash, SipHasher, Hasher};
81+
use std::mem;
7982
use std::ops;
8083
use std::rc::Rc;
8184
use std::vec::IntoIter;
@@ -2059,28 +2062,28 @@ pub struct ExistentialBounds<'tcx> {
20592062

20602063
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
20612064
pub struct BuiltinBounds {
2062-
bits: u8
2065+
bit_set: U8BitSet
20632066
}
20642067

20652068
impl BuiltinBounds {
20662069
pub fn empty() -> BuiltinBounds {
2067-
BuiltinBounds { bits: 0 }
2070+
BuiltinBounds { bit_set: BitSet::empty() }
20682071
}
20692072

20702073
pub fn insert(&mut self, bound: BuiltinBound) {
2071-
self.bits |= 1 << (bound as u8);
2074+
self.bit_set.insert(bound as u8)
20722075
}
20732076

20742077
pub fn contains(&self, bound: BuiltinBound) -> bool {
2075-
((self.bits >> (bound as u8)) & 1) == 1
2078+
self.bit_set.contains(bound as u8)
20762079
}
20772080

20782081
pub fn iter(&self) -> BuiltinBoundsIter {
20792082
self.into_iter()
20802083
}
20812084

20822085
fn is_empty(&self) -> bool {
2083-
self.bits == 0
2086+
self.bit_set.is_empty()
20842087
}
20852088

20862089
pub fn to_predicates<'tcx>(&self,
@@ -2095,36 +2098,19 @@ impl BuiltinBounds {
20952098
}
20962099

20972100
pub fn is_superset(&self, other: &BuiltinBounds) -> bool {
2098-
(self.bits & other.bits) == other.bits
2101+
self.bit_set.is_superset(other.bit_set)
20992102
}
21002103
}
21012104

21022105
pub struct BuiltinBoundsIter {
2103-
bounds: BuiltinBounds,
2104-
index: u8
2106+
bit_set: BitSetIter<u8>
21052107
}
21062108

21072109
impl Iterator for BuiltinBoundsIter {
21082110
type Item = BuiltinBound;
21092111

21102112
fn next(&mut self) -> Option<BuiltinBound> {
2111-
while self.index < 4 {
2112-
let result = match self.index {
2113-
0 if self.bounds.contains(BuiltinBound::Send) => Some(BuiltinBound::Send),
2114-
1 if self.bounds.contains(BuiltinBound::Sized) => Some(BuiltinBound::Sized),
2115-
2 if self.bounds.contains(BuiltinBound::Copy) => Some(BuiltinBound::Copy),
2116-
3 if self.bounds.contains(BuiltinBound::Sync) => Some(BuiltinBound::Sync),
2117-
_ => None
2118-
};
2119-
2120-
self.index += 1;
2121-
2122-
if result.is_some() {
2123-
return result;
2124-
}
2125-
}
2126-
2127-
return None;
2113+
self.bit_set.next().map(|b| unsafe { mem::transmute(b) })
21282114
}
21292115
}
21302116

@@ -2133,10 +2119,11 @@ impl<'a> IntoIterator for &'a BuiltinBounds {
21332119
type IntoIter = BuiltinBoundsIter;
21342120

21352121
fn into_iter(self) -> BuiltinBoundsIter {
2136-
BuiltinBoundsIter { bounds: self.clone(), index: 0 }
2122+
BuiltinBoundsIter { bit_set: self.bit_set.into_iter() }
21372123
}
21382124
}
21392125

2126+
#[repr(u8)]
21402127
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, RustcDecodable, RustcEncodable, Hash, Debug)]
21412128
pub enum BuiltinBound {
21422129
Send,
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::ops::{Add, Sub, Shl, Shr, BitAnd, BitOr};
12+
use std::num::{Zero, One};
13+
use std::cmp::PartialEq;
14+
15+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
16+
pub struct BitSet<N> {
17+
bits: N
18+
}
19+
20+
pub trait UnsignedInt : Zero
21+
+ One
22+
+ Add<Self, Output=Self>
23+
+ Sub<Self, Output=Self>
24+
+ Shl<Self, Output=Self>
25+
+ Shr<Self, Output=Self>
26+
+ BitAnd<Self, Output=Self>
27+
+ BitOr<Self, Output=Self>
28+
+ PartialEq<Self>
29+
+ PartialOrd<Self>
30+
+ Copy {}
31+
32+
impl UnsignedInt for u8 {}
33+
34+
pub type U8BitSet = BitSet<u8>;
35+
36+
impl<N: UnsignedInt> BitSet<N> {
37+
pub fn empty() -> BitSet<N> {
38+
BitSet { bits: Zero::zero() }
39+
}
40+
41+
pub fn insert(&mut self, value: N) {
42+
self.bits = self.bits | (N::one() << value);
43+
}
44+
45+
pub fn contains(&self, value: N) -> bool {
46+
((self.bits >> value) & N::one()) == N::one()
47+
}
48+
49+
pub fn iter(&self) -> BitSetIter<N> {
50+
self.into_iter()
51+
}
52+
53+
pub fn is_empty(&self) -> bool {
54+
self.bits == N::zero()
55+
}
56+
57+
pub fn is_superset(&self, other: BitSet<N>) -> bool {
58+
(self.bits & other.bits) == other.bits
59+
}
60+
}
61+
62+
pub struct BitSetIter<N> {
63+
bits: N,
64+
index: N
65+
}
66+
67+
impl<N: UnsignedInt> Iterator for BitSetIter<N> {
68+
type Item = N;
69+
70+
fn next(&mut self) -> Option<N> {
71+
if self.bits == N::zero() {
72+
return None;
73+
}
74+
75+
while (self.bits & N::one()) == N::zero() {
76+
self.index = self.index + N::one();
77+
self.bits = self.bits >> N::one();
78+
}
79+
80+
let result = self.index;
81+
82+
self.index = self.index + N::one();
83+
self.bits = self.bits >> N::one();
84+
Some(result)
85+
}
86+
}
87+
88+
89+
impl<'a, N: UnsignedInt> IntoIterator for &'a BitSet<N> {
90+
type Item = N;
91+
type IntoIter = BitSetIter<N>;
92+
93+
fn into_iter(self) -> BitSetIter<N> {
94+
BitSetIter { bits: self.bits, index: N::zero() }
95+
}
96+
}

src/librustc_data_structures/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
html_root_url = "http://doc.rust-lang.org/nightly/")]
2929

3030
#![feature(rustc_private, staged_api)]
31+
#![feature(zero_one)]
3132
#![cfg_attr(test, feature(test))]
3233

3334
#[macro_use] extern crate log;
@@ -36,4 +37,5 @@ extern crate serialize as rustc_serialize; // used by deriving
3637
pub mod snapshot_vec;
3738
pub mod graph;
3839
pub mod bitvec;
40+
pub mod bitset;
3941
pub mod unify;

0 commit comments

Comments
 (0)