Skip to content

Commit 7f21087

Browse files
committed
Simplify FromIterator<TokenTree> for TokenStream
1 parent 12bac84 commit 7f21087

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

src/wrapper.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -153,40 +153,38 @@ impl From<fallback::TokenStream> for TokenStream {
153153
}
154154
}
155155

156+
// Assumes nightly_works().
157+
fn into_compiler_token(token: TokenTree) -> proc_macro::TokenTree {
158+
match token {
159+
TokenTree::Group(tt) => tt.inner.unwrap_nightly().into(),
160+
TokenTree::Punct(tt) => {
161+
let spacing = match tt.spacing() {
162+
Spacing::Joint => proc_macro::Spacing::Joint,
163+
Spacing::Alone => proc_macro::Spacing::Alone,
164+
};
165+
let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
166+
op.set_span(tt.span().inner.unwrap_nightly());
167+
op.into()
168+
}
169+
TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
170+
TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
171+
}
172+
}
173+
156174
impl From<TokenTree> for TokenStream {
157175
fn from(token: TokenTree) -> TokenStream {
158-
if !nightly_works() {
159-
return TokenStream::Fallback(token.into());
176+
if nightly_works() {
177+
TokenStream::Compiler(into_compiler_token(token).into())
178+
} else {
179+
TokenStream::Fallback(token.into())
160180
}
161-
let tt: proc_macro::TokenTree = match token {
162-
TokenTree::Group(tt) => tt.inner.unwrap_nightly().into(),
163-
TokenTree::Punct(tt) => {
164-
let spacing = match tt.spacing() {
165-
Spacing::Joint => proc_macro::Spacing::Joint,
166-
Spacing::Alone => proc_macro::Spacing::Alone,
167-
};
168-
let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
169-
op.set_span(tt.span().inner.unwrap_nightly());
170-
op.into()
171-
}
172-
TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
173-
TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
174-
};
175-
TokenStream::Compiler(tt.into())
176181
}
177182
}
178183

179184
impl iter::FromIterator<TokenTree> for TokenStream {
180185
fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
181186
if nightly_works() {
182-
let trees = trees
183-
.into_iter()
184-
.map(TokenStream::from)
185-
.flat_map(|t| match t {
186-
TokenStream::Compiler(s) => s,
187-
TokenStream::Fallback(_) => mismatch(),
188-
});
189-
TokenStream::Compiler(trees.collect())
187+
TokenStream::Compiler(trees.into_iter().map(into_compiler_token).collect())
190188
} else {
191189
TokenStream::Fallback(trees.into_iter().collect())
192190
}
@@ -220,11 +218,7 @@ impl Extend<TokenTree> for TokenStream {
220218
fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
221219
match self {
222220
TokenStream::Compiler(tts) => {
223-
tts.extend(
224-
streams
225-
.into_iter()
226-
.map(|t| TokenStream::from(t).unwrap_nightly()),
227-
);
221+
tts.extend(streams.into_iter().map(into_compiler_token));
228222
}
229223
TokenStream::Fallback(tts) => tts.extend(streams),
230224
}

0 commit comments

Comments
 (0)