Skip to content

Commit 3fa81c6

Browse files
tommilligancalebcartwright
authored andcommitted
[review] use extend trait, enum for skip context
1 parent 7cc1261 commit 3fa81c6

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

src/skip.rs

+39-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::collections::HashSet;
1111
/// - attributes slice
1212
/// - manually feeding values into the underlying contexts
1313
///
14-
/// Query this context to know if you need skip a block.
14+
/// Query this context to know if you need to skip a block.
1515
#[derive(Default, Clone)]
1616
pub(crate) struct SkipContext {
1717
pub(crate) macros: SkipNameContext,
@@ -20,8 +20,8 @@ pub(crate) struct SkipContext {
2020

2121
impl SkipContext {
2222
pub(crate) fn update_with_attrs(&mut self, attrs: &[ast::Attribute]) {
23-
self.macros.append(get_skip_names("macros", attrs));
24-
self.attributes.append(get_skip_names("attributes", attrs));
23+
self.macros.extend(get_skip_names("macros", attrs));
24+
self.attributes.extend(get_skip_names("attributes", attrs));
2525
}
2626

2727
pub(crate) fn update(&mut self, other: SkipContext) {
@@ -34,28 +34,52 @@ impl SkipContext {
3434
/// Track which names to skip.
3535
///
3636
/// Query this context with a string to know whether to skip it.
37-
#[derive(Default, Clone)]
38-
pub(crate) struct SkipNameContext {
39-
all: bool,
40-
values: HashSet<String>,
37+
#[derive(Clone)]
38+
pub(crate) enum SkipNameContext {
39+
All,
40+
Values(HashSet<String>),
4141
}
4242

43-
impl SkipNameContext {
44-
pub(crate) fn append(&mut self, values: Vec<String>) {
45-
self.values.extend(values);
43+
impl Default for SkipNameContext {
44+
fn default() -> Self {
45+
Self::Values(Default::default())
46+
}
47+
}
48+
49+
impl Extend<String> for SkipNameContext {
50+
fn extend<T: IntoIterator<Item = String>>(&mut self, iter: T) {
51+
match self {
52+
Self::All => {}
53+
Self::Values(values) => values.extend(iter),
54+
}
4655
}
56+
}
4757

58+
impl SkipNameContext {
4859
pub(crate) fn update(&mut self, other: Self) {
49-
self.all = self.all || other.all;
50-
self.values.extend(other.values);
60+
match (self, other) {
61+
// If we're already skipping everything, nothing more can be added
62+
(Self::All, _) => {}
63+
// If we want to skip all, set it
64+
(this, Self::All) => {
65+
*this = Self::All;
66+
}
67+
// If we have some new values to skip, add them
68+
(Self::Values(existing_values), Self::Values(new_values)) => {
69+
existing_values.extend(new_values)
70+
}
71+
}
5172
}
5273

5374
pub(crate) fn skip(&self, name: &str) -> bool {
54-
self.all || self.values.contains(name)
75+
match self {
76+
Self::All => true,
77+
Self::Values(values) => values.contains(name),
78+
}
5579
}
5680

57-
pub(crate) fn set_all(&mut self, all: bool) {
58-
self.all = all;
81+
pub(crate) fn skip_all(&mut self) {
82+
*self = Self::All;
5983
}
6084
}
6185

src/visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,10 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
775775
for macro_selector in config.skip_macro_invocations().0 {
776776
match macro_selector {
777777
MacroSelector::Name(name) => macro_names.push(name.to_string()),
778-
MacroSelector::All => skip_context.macros.set_all(true),
778+
MacroSelector::All => skip_context.macros.skip_all(),
779779
}
780780
}
781-
skip_context.macros.append(macro_names);
781+
skip_context.macros.extend(macro_names);
782782
FmtVisitor {
783783
parent_context: None,
784784
parse_sess: parse_session,

0 commit comments

Comments
 (0)