diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2014-03-19 23:16:56 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2014-03-21 10:52:48 +1100 |
| commit | 7785fe191651fb42a6300a399d61414bf49dffb3 (patch) | |
| tree | 1d58655556eac689f63b20f5944dd7e806d50de4 /src/libsyntax | |
| parent | 7334c11b4b196e39da2418a239e2ff916896fa19 (diff) | |
| download | rust-7785fe191651fb42a6300a399d61414bf49dffb3.tar.gz rust-7785fe191651fb42a6300a399d61414bf49dffb3.zip | |
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.
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ext/deriving/generic.rs | 25 | ||||
| -rw-r--r-- | src/libsyntax/opt_vec.rs | 47 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 10 |
3 files changed, 22 insertions, 60 deletions
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>(t: Vec<T> ) -> OptVec<T> { } impl<T> OptVec<T> { - 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<T> { - 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<T> OptVec<T> { } } - 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<T>(v: OptVec<T>) -> Vec<T> { } } -impl<T:Clone> OptVec<T> { - pub fn prepend(&self, t: T) -> OptVec<T> { - let mut v0 = vec!(t); - match *self { - Empty => {} - Vec(ref v1) => { v0.push_all(v1.as_slice()); } - } - return Vec(v0); - } -} - impl<A:Eq> Eq for OptVec<A> { fn eq(&self, other: &OptVec<A>) -> bool { // Note: cannot use #[deriving(Eq)] here because @@ -208,10 +168,7 @@ impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> { impl<A> FromIterator<A> for OptVec<A> { fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> OptVec<A> { - let mut r = Empty; - for x in *iterator { - r.push(x); - } - r + let v: Vec<A> = 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<T> { 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<T>( @@ -681,7 +681,7 @@ impl<'a> Parser<'a> { f: |&mut Parser| -> T) -> Vec<T> { let mut first: bool = true; - let mut v: Vec<T> = 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 )? |
