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

Delete EnumSet and remove its final use in rustc #26705

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
//! This module defines a container which uses an efficient bit mask
//! representation to hold C-like enum variants.

#![deprecated(reason = "EnumSet has been deprecated",
since = "1.2.0")]
#![unstable(feature = "enumset",
reason = "matches collection reform specification, \
waiting for dust to settle")]

reason = "deprecated")]
#![allow(deprecated)]
use core::prelude::*;
use core::marker;
use core::fmt;
Expand Down
1 change: 1 addition & 0 deletions src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub use bit_set::BitSet;
pub use btree_map::BTreeMap;
pub use btree_set::BTreeSet;
pub use linked_list::LinkedList;
#[allow(deprecated)]
pub use enum_set::EnumSet;
pub use vec_deque::VecDeque;
pub use string::String;
Expand Down
1 change: 0 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#![feature(duration)]
#![feature(duration_span)]
#![feature(dynamic_lib)]
#![feature(enumset)]
#![feature(fs_canonicalize)]
#![feature(hash_default)]
#![feature(hashmap_hasher)]
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
ty::TyTrait(ref data) => {
match self.tcx().lang_items.to_builtin_kind(obligation.predicate.def_id()) {
Some(bound @ ty::BoundSend) | Some(bound @ ty::BoundSync) => {
if data.bounds.builtin_bounds.contains(&bound) {
if data.bounds.builtin_bounds.contains(bound) {
debug!("assemble_candidates_from_object_ty: matched builtin bound, \
pushing candidate");
candidates.vec.push(BuiltinObjectCandidate);
Expand Down Expand Up @@ -1632,7 +1632,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
match bound {
ty::BoundSized => Err(Unimplemented),
ty::BoundCopy => {
if data.bounds.builtin_bounds.contains(&bound) {
if data.bounds.builtin_bounds.contains(bound) {
ok_if(Vec::new())
} else {
// Recursively check all supertraits to find out if any further
Expand Down
94 changes: 66 additions & 28 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ use std::cell::{Cell, RefCell, Ref};
use std::cmp;
use std::fmt;
use std::hash::{Hash, SipHasher, Hasher};
use std::mem;
use std::ops;
use std::rc::Rc;
use std::vec::IntoIter;
use collections::enum_set::{self, EnumSet, CLike};
use std::collections::{HashMap, HashSet};
use syntax::abi;
use syntax::ast::{CrateNum, DefId, ItemImpl, ItemTrait, LOCAL_CRATE};
Expand Down Expand Up @@ -2059,18 +2057,44 @@ pub struct ExistentialBounds<'tcx> {
pub projection_bounds: Vec<PolyProjectionPredicate<'tcx>>,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct BuiltinBounds(EnumSet<BuiltinBound>);
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct BuiltinBounds {
bits: u8
}

impl BuiltinBounds {
pub fn empty() -> BuiltinBounds {
BuiltinBounds(EnumSet::new())
pub fn empty() -> BuiltinBounds {
BuiltinBounds { bits: 0 }
}

pub fn insert(&mut self, bound: BuiltinBound) {
self.bits = match bound {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.bits |= match ... ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also just cast the BuiltinBound to an integer. So

pub fn insert(&mut self, bound) {
    self.bits |= (1 << (bound as u8));
}

pub fn contains(& self, bound) {
    ((self.bits >> (bound as u8)) & 1) == 1
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good point some of these are probably just my functional programmer side coming through ;)

BuiltinBound::Send => self.bits | 0b0000_0001,
BuiltinBound::Sized => self.bits | 0b0000_0010,
BuiltinBound::Copy => self.bits | 0b0000_0100,
BuiltinBound::Sync => self.bits | 0b0000_1000,
}
}

pub fn contains(&self, bound: BuiltinBound) -> bool {
let bit = match bound {
BuiltinBound::Send => self.bits,
BuiltinBound::Sized => self.bits >> 1,
BuiltinBound::Copy => self.bits >> 2,
BuiltinBound::Sync => self.bits >> 3
};

(bit & 0b0000_0001) == 1
}

pub fn iter(&self) -> enum_set::Iter<BuiltinBound> {
pub fn iter(&self) -> BuiltinBoundsIter {
self.into_iter()
}

fn is_empty(&self) -> bool {
self.bits == 0
}

pub fn to_predicates<'tcx>(&self,
tcx: &ty::ctxt<'tcx>,
self_ty: Ty<'tcx>) -> Vec<Predicate<'tcx>> {
Expand All @@ -2081,44 +2105,58 @@ impl BuiltinBounds {
}
).collect()
}

pub fn is_superset(&self, other: &BuiltinBounds) -> bool {
(self.bits & other.bits) == other.bits
}
}

impl ops::Deref for BuiltinBounds {
type Target = EnumSet<BuiltinBound>;
fn deref(&self) -> &Self::Target { &self.0 }
pub struct BuiltinBoundsIter {
bounds: BuiltinBounds,
index: u8
}

impl ops::DerefMut for BuiltinBounds {
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 }
impl Iterator for BuiltinBoundsIter {
type Item = BuiltinBound;

fn next(&mut self) -> Option<BuiltinBound> {
while self.index < 4 {
let result = match self.index {
0 if self.bounds.contains(BuiltinBound::Send) => Some(BuiltinBound::Send),
1 if self.bounds.contains(BuiltinBound::Sized) => Some(BuiltinBound::Sized),
2 if self.bounds.contains(BuiltinBound::Copy) => Some(BuiltinBound::Copy),
3 if self.bounds.contains(BuiltinBound::Sync) => Some(BuiltinBound::Sync),
_ => None
};

self.index += 1;

if result.is_some() {
return result;
}
}

return None;
}
}

impl<'a> IntoIterator for &'a BuiltinBounds {
type Item = BuiltinBound;
type IntoIter = enum_set::Iter<BuiltinBound>;
fn into_iter(self) -> Self::IntoIter {
(**self).into_iter()
type IntoIter = BuiltinBoundsIter;

fn into_iter(self) -> BuiltinBoundsIter {
BuiltinBoundsIter { bounds: self.clone(), index: 0 }
}
}

#[derive(Clone, RustcEncodable, PartialEq, Eq, RustcDecodable, Hash,
Debug, Copy)]
#[repr(usize)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, RustcDecodable, RustcEncodable, Hash, Debug)]
pub enum BuiltinBound {
Send,
Sized,
Copy,
Sync,
}

impl CLike for BuiltinBound {
fn to_usize(&self) -> usize {
*self as usize
}
fn from_usize(v: usize) -> BuiltinBound {
unsafe { mem::transmute(v) }
}
}

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct TyVid {
pub index: u32
Expand Down Expand Up @@ -5707,7 +5745,7 @@ impl<'tcx> ctxt<'tcx> {

pub fn try_add_builtin_trait(&self,
trait_def_id: ast::DefId,
builtin_bounds: &mut EnumSet<BuiltinBound>)
builtin_bounds: &mut BuiltinBounds)
-> bool
{
//! Checks whether `trait_ref` refers to one of the builtin
Expand Down
3 changes: 3 additions & 0 deletions src/libserialize/collection_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::collections::hash_state::HashState;

use {Decodable, Encodable, Decoder, Encoder};
use std::collections::{LinkedList, VecDeque, BTreeMap, BTreeSet, HashMap, HashSet, VecMap};
#[allow(deprecated)]
use collections::enum_set::{EnumSet, CLike};

impl<
Expand Down Expand Up @@ -130,6 +131,7 @@ impl<
}
}

#[allow(deprecated)]
impl<
T: Encodable + CLike
> Encodable for EnumSet<T> {
Expand All @@ -142,6 +144,7 @@ impl<
}
}

#[allow(deprecated)]
impl<
T: Decodable + CLike
> Decodable for EnumSet<T> {
Expand Down