Clean up SmallVector use a bit

This commit is contained in:
Steven Fackler 2013-11-25 20:02:15 -08:00 committed by Steven Fackler
parent c144752a2d
commit c403c1f18e
3 changed files with 39 additions and 109 deletions

View File

@ -33,7 +33,6 @@ use syntax::codemap;
use syntax::fold::*;
use syntax::fold;
use syntax::parse::token;
use syntax::util::small_vector::SmallVector;
use syntax;
use std::at_vec;
@ -348,20 +347,14 @@ fn simplify_ast(ii: &ast::inlined_item) -> ast::inlined_item {
match *ii {
//hack: we're not dropping items
ast::ii_item(i) => ast::ii_item(get_only_one(fld.fold_item(i))),
ast::ii_item(i) => ast::ii_item(fld.fold_item(i)
.expect_one("expected one item")),
ast::ii_method(d, is_provided, m) =>
ast::ii_method(d, is_provided, fld.fold_method(m)),
ast::ii_foreign(i) => ast::ii_foreign(fld.fold_foreign_item(i))
}
}
fn get_only_one<T>(mut v: SmallVector<T>) -> T {
if v.len() != 1 {
fail!("Attempting to extract unique member but there isn't one");
}
v.pop()
}
fn decode_ast(par_doc: ebml::Doc) -> ast::inlined_item {
let chi_doc = par_doc.get(c::tag_tree as uint);
let mut d = reader::Decoder(chi_doc);
@ -387,7 +380,8 @@ fn renumber_ast(xcx: @ExtendedDecodeContext, ii: ast::inlined_item)
xcx: xcx,
};
match ii {
ast::ii_item(i) => ast::ii_item(get_only_one(fld.fold_item(i))),
ast::ii_item(i) => ast::ii_item(fld.fold_item(i)
.expect_one("expected one item")),
ast::ii_method(d, is_provided, m) =>
ast::ii_method(xcx.tr_def_id(d), is_provided, fld.fold_method(m)),
ast::ii_foreign(i) => ast::ii_foreign(fld.fold_foreign_item(i)),

View File

@ -678,7 +678,9 @@ pub fn expand_block_elts(exts: SyntaxEnv, b: &Block, fld: &MacroExpander)
let rename_fld = renames_to_fold(pending_renames);
let new_view_items = b.view_items.map(|x| fld.fold_view_item(x));
let new_stmts = b.stmts.iter()
.flat_map(|x| fld.fold_stmt(mustbeone(rename_fld.fold_stmt(*x))).move_iter())
.map(|x| rename_fld.fold_stmt(*x)
.expect_one("rename_fold didn't return one value"))
.flat_map(|x| fld.fold_stmt(x).move_iter())
.collect();
let new_expr = b.expr.map(|x| fld.fold_expr(rename_fld.fold_expr(x)));
Block{
@ -691,14 +693,6 @@ pub fn expand_block_elts(exts: SyntaxEnv, b: &Block, fld: &MacroExpander)
}
}
// rename_fold should never return anything other than one thing
fn mustbeone<T>(mut val : SmallVector<T>) -> T {
if val.len() != 1 {
fail!("rename_fold didn't return one value");
}
val.pop()
}
// get the (innermost) BlockInfo from an exts stack
fn get_block_info(exts : SyntaxEnv) -> BlockInfo {
match exts.find_in_topmost_frame(&intern(special_block_name)) {
@ -734,11 +728,8 @@ pub fn renames_to_fold(renames: @mut ~[(ast::Ident,ast::Name)]) -> @ast_fold {
// perform a bunch of renames
fn apply_pending_renames(folder : @ast_fold, stmt : ast::Stmt) -> @ast::Stmt {
let mut stmts = folder.fold_stmt(&stmt);
if stmts.len() != 1 {
fail!("renaming of stmt did not produce one stmt");
}
stmts.pop()
folder.fold_stmt(&stmt)
.expect_one("renaming of stmt did not produce one stmt")
}
@ -1185,11 +1176,8 @@ fn mark_expr(expr : @ast::Expr, m : Mrk) -> @ast::Expr {
// apply a given mark to the given stmt. Used following the expansion of a macro.
fn mark_stmt(expr : &ast::Stmt, m : Mrk) -> @ast::Stmt {
let mut stmts = new_mark_folder(m).fold_stmt(expr);
if stmts.len() != 1 {
fail!("marking a stmt didn't return a stmt");
}
stmts.pop()
new_mark_folder(m).fold_stmt(expr)
.expect_one("marking a stmt didn't return a stmt")
}
// apply a given mark to the given item. Used following the expansion of a macro.

View File

@ -54,42 +54,17 @@ impl<T> SmallVector<T> {
match *self {
Zero => *self = One(v),
One(*) => {
let mut tmp = Many(~[]);
util::swap(self, &mut tmp);
match *self {
Many(ref mut vs) => {
match tmp {
One(v1) => {
vs.push(v1);
vs.push(v);
}
_ => unreachable!()
}
}
let one = util::replace(self, Zero);
match one {
One(v1) => util::replace(self, Many(~[v1, v])),
_ => unreachable!()
}
};
}
Many(ref mut vs) => vs.push(v)
}
}
pub fn pop(&mut self) -> T {
match *self {
Zero => fail!("attempted to pop from an empty SmallVector"),
One(*) => {
let mut tmp = Zero;
util::swap(self, &mut tmp);
match tmp {
One(v) => v,
_ => unreachable!()
}
}
// Should this reduce to a One if possible?
Many(ref mut vs) => vs.pop()
}
}
pub fn get<'a>(&'a self, idx: uint) -> &'a T {
fn get<'a>(&'a self, idx: uint) -> &'a T {
match *self {
One(ref v) if idx == 0 => v,
Many(ref vs) => &vs[idx],
@ -97,10 +72,11 @@ impl<T> SmallVector<T> {
}
}
pub fn iter<'a>(&'a self) -> SmallVectorIterator<'a, T> {
SmallVectorIterator {
vec: self,
idx: 0
pub fn expect_one(self, err: &'static str) -> T {
match self {
One(v) => v,
Many([v]) => v,
_ => fail!(err)
}
}
@ -113,27 +89,6 @@ impl<T> SmallVector<T> {
}
}
pub struct SmallVectorIterator<'vec, T> {
priv vec: &'vec SmallVector<T>,
priv idx: uint
}
impl<'vec, T> Iterator<&'vec T> for SmallVectorIterator<'vec, T> {
fn next(&mut self) -> Option<&'vec T> {
if self.idx == self.vec.len() {
return None;
}
self.idx += 1;
Some(self.vec.get(self.idx - 1))
}
fn size_hint(&self) -> (uint, Option<uint>) {
let rem = self.vec.len() - self.idx;
(rem, Some(rem))
}
}
pub enum SmallVectorMoveIterator<T> {
priv ZeroIterator,
priv OneIterator(T),
@ -192,18 +147,6 @@ mod test {
assert_eq!(&3, v.get(2));
}
#[test]
fn test_pop() {
let mut v = SmallVector::one(1);
assert_eq!(1, v.pop());
assert_eq!(0, v.len());
let mut v= SmallVector::many(~[1, 2]);
assert_eq!(2, v.pop());
assert_eq!(1, v.pop());
assert_eq!(0, v.len());
}
#[test]
fn test_from_iterator() {
let v: SmallVector<int> = (~[1, 2, 3]).move_iter().collect();
@ -213,19 +156,6 @@ mod test {
assert_eq!(&3, v.get(2));
}
#[test]
fn test_iter() {
let v = SmallVector::zero();
let v: ~[&int] = v.iter().collect();
assert_eq!(~[], v);
let v = SmallVector::one(1);
assert_eq!(~[&1], v.iter().collect());
let v = SmallVector::many(~[1, 2, 3]);
assert_eq!(~[&1, &2, &3], v.iter().collect());
}
#[test]
fn test_move_iter() {
let v = SmallVector::zero();
@ -238,4 +168,22 @@ mod test {
let v = SmallVector::many(~[1, 2, 3]);
assert_eq!(~[1, 2, 3], v.move_iter().collect());
}
#[test]
#[should_fail]
fn test_expect_one_zero() {
let _: int = SmallVector::zero().expect_one("");
}
#[test]
#[should_fail]
fn test_expect_one_many() {
SmallVector::many(~[1, 2]).expect_one("");
}
#[test]
fn test_expect_one_one() {
assert_eq!(1, SmallVector::one(1).expect_one(""));
assert_eq!(1, SmallVector::many(~[1]).expect_one(""));
}
}