From 7785fe191651fb42a6300a399d61414bf49dffb3 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Wed, 19 Mar 2014 23:16:56 +1100 Subject: syntax: make OptVec immutable. This is the first step to replacing OptVec with a new representation: remove all mutability. Any mutations have to go via `Vec` and then make to `OptVec`. Many of the uses of OptVec are unnecessary now that Vec has no-alloc emptiness (and have been converted to Vec): the only ones that really need it are the AST and sty's (and so on) where there are a *lot* of instances of them, and they're (mostly) immutable. --- src/libsyntax/ext/deriving/generic.rs | 25 +++++++++++-------- src/libsyntax/opt_vec.rs | 47 ++--------------------------------- src/libsyntax/parse/parser.rs | 10 ++++---- 3 files changed, 22 insertions(+), 60 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/deriving/generic.rs b/src/libsyntax/ext/deriving/generic.rs index 546c3eac41c..7aa98a60781 100644 --- a/src/libsyntax/ext/deriving/generic.rs +++ b/src/libsyntax/ext/deriving/generic.rs @@ -360,27 +360,32 @@ impl<'a> TraitDef<'a> { methods: Vec<@ast::Method> ) -> @ast::Item { let trait_path = self.path.to_path(cx, self.span, type_ident, generics); - let mut trait_generics = self.generics.to_generics(cx, self.span, - type_ident, generics); + let Generics { mut lifetimes, ty_params } = + self.generics.to_generics(cx, self.span, type_ident, generics); + let mut ty_params = opt_vec::take_vec(ty_params); + // Copy the lifetimes - for l in generics.lifetimes.iter() { - trait_generics.lifetimes.push(*l) - }; + lifetimes.extend(&mut generics.lifetimes.iter().map(|l| *l)); + // Create the type parameters. - for ty_param in generics.ty_params.iter() { + ty_params.extend(&mut generics.ty_params.iter().map(|ty_param| { // I don't think this can be moved out of the loop, since // a TyParamBound requires an ast id - let mut bounds = opt_vec::from( + let mut bounds = // extra restrictions on the generics parameters to the type being derived upon self.additional_bounds.map(|p| { cx.typarambound(p.to_path(cx, self.span, type_ident, generics)) - })); + }); // require the current trait bounds.push(cx.typarambound(trait_path.clone())); - trait_generics.ty_params.push(cx.typaram(ty_param.ident, bounds, None)); - } + cx.typaram(ty_param.ident, opt_vec::from(bounds), None) + })); + let trait_generics = Generics { + lifetimes: lifetimes, + ty_params: opt_vec::from(ty_params) + }; // Create the reference to the trait. let trait_ref = cx.trait_ref(trait_path); diff --git a/src/libsyntax/opt_vec.rs b/src/libsyntax/opt_vec.rs index aeb521468d2..425a5a9a5fa 100644 --- a/src/libsyntax/opt_vec.rs +++ b/src/libsyntax/opt_vec.rs @@ -37,25 +37,6 @@ pub fn from(t: Vec ) -> OptVec { } impl OptVec { - pub fn push(&mut self, t: T) { - match *self { - Vec(ref mut v) => { - v.push(t); - return; - } - Empty => { - *self = Vec(vec!(t)); - } - } - } - - pub fn pop(&mut self) -> Option { - match *self { - Vec(ref mut v) => v.pop(), - Empty => None - } - } - pub fn last<'a>(&'a self) -> Option<&'a T> { match *self { Vec(ref v) => v.last(), @@ -102,16 +83,6 @@ impl OptVec { } } - pub fn swap_remove(&mut self, index: uint) { - match *self { - Empty => { fail!("index out of bounds"); } - Vec(ref mut v) => { - assert!(index < v.len()); - v.swap_remove(index); - } - } - } - #[inline] pub fn iter<'r>(&'r self) -> Items<'r, T> { match *self { @@ -142,17 +113,6 @@ pub fn take_vec(v: OptVec) -> Vec { } } -impl OptVec { - pub fn prepend(&self, t: T) -> OptVec { - let mut v0 = vec!(t); - match *self { - Empty => {} - Vec(ref v1) => { v0.push_all(v1.as_slice()); } - } - return Vec(v0); - } -} - impl Eq for OptVec { fn eq(&self, other: &OptVec) -> bool { // Note: cannot use #[deriving(Eq)] here because @@ -208,10 +168,7 @@ impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> { impl FromIterator for OptVec { fn from_iterator>(iterator: &mut T) -> OptVec { - let mut r = Empty; - for x in *iterator { - r.push(x); - } - r + let v: Vec = iterator.collect(); + from(v) } } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 5398844d52a..3e2b88c1cf1 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -632,7 +632,7 @@ impl<'a> Parser<'a> { f: |&mut Parser| -> T) -> OptVec { let mut first = true; - let mut v = opt_vec::Empty; + let mut v = Vec::new(); while self.token != token::GT && self.token != token::BINOP(token::SHR) { match sep { @@ -644,7 +644,7 @@ impl<'a> Parser<'a> { } v.push(f(self)); } - return v; + return opt_vec::from(v); } pub fn parse_seq_to_gt( @@ -681,7 +681,7 @@ impl<'a> Parser<'a> { f: |&mut Parser| -> T) -> Vec { let mut first: bool = true; - let mut v: Vec = Vec::new(); + let mut v = vec!(); while self.token != *ket { match sep.sep { Some(ref t) => { @@ -3437,7 +3437,7 @@ impl<'a> Parser<'a> { return None; } - let mut result = opt_vec::Empty; + let mut result = vec!(); loop { match self.token { token::LIFETIME(lifetime) => { @@ -3462,7 +3462,7 @@ impl<'a> Parser<'a> { } } - return Some(result); + return Some(opt_vec::from(result)); } // matches typaram = IDENT optbounds ( EQ ty )? -- cgit 1.4.1-3-g733a5