-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Improves handling of statement macros. #24155
Changes from 3 commits
1934386
b16cfac
fae29e4
22eb319
77627ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,42 @@ impl<T> SmallVector<T> { | |
} | ||
} | ||
|
||
pub fn pop(&mut self) -> Option<T> { | ||
match self.repr { | ||
Zero => None, | ||
One(..) => { | ||
let one = mem::replace(&mut self.repr, Zero); | ||
match one { | ||
One(v1) => Some(v1), | ||
_ => unreachable!() | ||
} | ||
} | ||
Many(..) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can simplify this a bit more since we don't actually need to swap anyore: Many(ref mut vs) => vs.pop() Edit: though that may treat the repr as borrowed in the other branches... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right you are. Updated. |
||
let mut many = mem::replace(&mut self.repr, Zero); | ||
let item = | ||
match many { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't necessarily need to worry too much about making sure to convert a length 1 vec to a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure whether it was a concern about allocation time or space (I guessed space, from the name). I also went for the safe thing, since I wasn't sure if the rest of the impl was prepared for zero- or one-length vectors, rather than Zero and One enum values. It would definitely simplify things, if that is an acceptable way to do it. |
||
Many(ref mut vs) if vs.len() == 1 => { | ||
// self.repr is already Zero | ||
vs.pop() | ||
}, | ||
Many(ref mut vs) if vs.len() == 2 => { | ||
let item = vs.pop(); | ||
mem::replace(&mut self.repr, One(vs.pop().unwrap())); | ||
item | ||
}, | ||
Many(ref mut vs) if vs.len() > 2 => { | ||
let item = vs.pop(); | ||
let rest = mem::replace(vs, vec!()); | ||
mem::replace(&mut self.repr, Many(rest)); | ||
item | ||
}, | ||
_ => unreachable!() | ||
}; | ||
item | ||
} | ||
} | ||
} | ||
|
||
pub fn push(&mut self, v: T) { | ||
match self.repr { | ||
Zero => self.repr = One(v), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
macro_rules! foo { | ||
() => { | ||
struct Bar; | ||
struct Baz; | ||
} | ||
} | ||
|
||
macro_rules! grault { | ||
() => { | ||
foo!(); | ||
struct Xyzzy; | ||
} | ||
} | ||
|
||
fn static_assert_exists<T>() { } | ||
|
||
fn main() { | ||
grault!(); | ||
static_assert_exists::<Bar>(); | ||
static_assert_exists::<Baz>(); | ||
static_assert_exists::<Xyzzy>(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
macro_rules! foo { | ||
() => { | ||
struct Bar; | ||
struct Baz; | ||
} | ||
} | ||
|
||
macro_rules! grault { | ||
() => {{ | ||
foo!(); | ||
struct Xyzzy; | ||
0 | ||
}} | ||
} | ||
|
||
fn main() { | ||
let x = grault!(); | ||
assert_eq!(x, 0); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be an
if let Some(stmt) = fully_expanded.pop() { .. }
instead of a match.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, that would be cleaner.