Skip to content

Commit 3a3e4b7

Browse files
committedJan 15, 2022
Rustdoc: remove ListAttributesIter and use impl Iterator instead
1 parent ec4bcaa commit 3a3e4b7

File tree

2 files changed

+19
-46
lines changed

2 files changed

+19
-46
lines changed
 

Diff for: ‎src/librustdoc/clean/types.rs

+17-46
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::lazy::SyncOnceCell as OnceCell;
55
use std::path::PathBuf;
66
use std::rc::Rc;
77
use std::sync::Arc;
8-
use std::{slice, vec};
8+
use std::vec;
99

1010
use arrayvec::ArrayVec;
1111

@@ -733,43 +733,12 @@ crate struct Module {
733733
crate span: Span,
734734
}
735735

736-
crate struct ListAttributesIter<'a> {
737-
attrs: slice::Iter<'a, ast::Attribute>,
738-
current_list: vec::IntoIter<ast::NestedMetaItem>,
739-
name: Symbol,
740-
}
741-
742-
impl<'a> Iterator for ListAttributesIter<'a> {
743-
type Item = ast::NestedMetaItem;
744-
745-
fn next(&mut self) -> Option<Self::Item> {
746-
if let Some(nested) = self.current_list.next() {
747-
return Some(nested);
748-
}
749-
750-
for attr in &mut self.attrs {
751-
if let Some(list) = attr.meta_item_list() {
752-
if attr.has_name(self.name) {
753-
self.current_list = list.into_iter();
754-
if let Some(nested) = self.current_list.next() {
755-
return Some(nested);
756-
}
757-
}
758-
}
759-
}
760-
761-
None
762-
}
763-
764-
fn size_hint(&self) -> (usize, Option<usize>) {
765-
let lower = self.current_list.len();
766-
(lower, None)
767-
}
768-
}
769-
770736
crate trait AttributesExt {
771-
/// Finds an attribute as List and returns the list of attributes nested inside.
772-
fn lists(&self, name: Symbol) -> ListAttributesIter<'_>;
737+
type AttributeIterator<'a>: Iterator<Item = ast::NestedMetaItem>
738+
where
739+
Self: 'a;
740+
741+
fn lists<'a>(&'a self, name: Symbol) -> Self::AttributeIterator<'a>;
773742

774743
fn span(&self) -> Option<rustc_span::Span>;
775744

@@ -781,8 +750,13 @@ crate trait AttributesExt {
781750
}
782751

783752
impl AttributesExt for [ast::Attribute] {
784-
fn lists(&self, name: Symbol) -> ListAttributesIter<'_> {
785-
ListAttributesIter { attrs: self.iter(), current_list: Vec::new().into_iter(), name }
753+
type AttributeIterator<'a> = impl Iterator<Item = ast::NestedMetaItem> + 'a;
754+
755+
fn lists<'a>(&'a self, name: Symbol) -> Self::AttributeIterator<'a> {
756+
self.iter()
757+
.filter(move |attr| attr.has_name(name))
758+
.filter_map(ast::Attribute::meta_item_list)
759+
.flatten()
786760
}
787761

788762
/// Return the span of the first doc-comment, if it exists.
@@ -901,12 +875,9 @@ crate trait NestedAttributesExt {
901875
fn get_word_attr(self, word: Symbol) -> Option<ast::NestedMetaItem>;
902876
}
903877

904-
impl<I> NestedAttributesExt for I
905-
where
906-
I: IntoIterator<Item = ast::NestedMetaItem>,
907-
{
908-
fn get_word_attr(self, word: Symbol) -> Option<ast::NestedMetaItem> {
909-
self.into_iter().find(|attr| attr.is_word() && attr.has_name(word))
878+
impl<I: Iterator<Item = ast::NestedMetaItem>> NestedAttributesExt for I {
879+
fn get_word_attr(mut self, word: Symbol) -> Option<ast::NestedMetaItem> {
880+
self.find(|attr| attr.is_word() && attr.has_name(word))
910881
}
911882
}
912883

@@ -1014,7 +985,7 @@ crate struct Attributes {
1014985
}
1015986

1016987
impl Attributes {
1017-
crate fn lists(&self, name: Symbol) -> ListAttributesIter<'_> {
988+
crate fn lists(&self, name: Symbol) -> impl Iterator<Item = ast::NestedMetaItem> + '_ {
1018989
self.other_attrs.lists(name)
1019990
}
1020991

Diff for: ‎src/librustdoc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#![feature(once_cell)]
1717
#![feature(type_ascription)]
1818
#![feature(iter_intersperse)]
19+
#![feature(type_alias_impl_trait)]
20+
#![feature(generic_associated_types)]
1921
#![recursion_limit = "256"]
2022
#![warn(rustc::internal)]
2123
#![allow(clippy::collapsible_if, clippy::collapsible_else_if)]

0 commit comments

Comments
 (0)
Please sign in to comment.