Skip to content

Commit 8be135b

Browse files
committed
Write code directly instead of using FromIterator
The FromIterator was only used once and made the code much harder to understand. The types don't make sense until you realize there's a custom FromIterator impl.
1 parent 22dc5c4 commit 8be135b

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/librustdoc/clean/types.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::cell::RefCell;
22
use std::default::Default;
33
use std::hash::{Hash, Hasher};
4-
use std::iter::FromIterator;
54
use std::lazy::SyncOnceCell as OnceCell;
65
use std::path::PathBuf;
76
use std::rc::Rc;
@@ -945,16 +944,14 @@ fn add_doc_fragment(out: &mut String, frag: &DocFragment) {
945944
}
946945
}
947946

948-
impl<'a> FromIterator<&'a DocFragment> for String {
949-
fn from_iter<T>(iter: T) -> Self
950-
where
951-
T: IntoIterator<Item = &'a DocFragment>,
952-
{
953-
iter.into_iter().fold(String::new(), |mut acc, frag| {
954-
add_doc_fragment(&mut acc, frag);
955-
acc
956-
})
947+
/// Collapse a collection of [`DocFragment`]s into one string,
948+
/// handling indentation and newlines as needed.
949+
crate fn collapse_doc_fragments(doc_strings: &[DocFragment]) -> String {
950+
let mut acc = String::new();
951+
for frag in doc_strings {
952+
add_doc_fragment(&mut acc, frag);
957953
}
954+
acc
958955
}
959956

960957
/// A link that has not yet been rendered.
@@ -1083,7 +1080,11 @@ impl Attributes {
10831080
/// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
10841081
/// with newlines.
10851082
crate fn doc_value(&self) -> Option<String> {
1086-
if self.doc_strings.is_empty() { None } else { Some(self.doc_strings.iter().collect()) }
1083+
if self.doc_strings.is_empty() {
1084+
None
1085+
} else {
1086+
Some(collapse_doc_fragments(&self.doc_strings))
1087+
}
10871088
}
10881089

10891090
crate fn get_doc_aliases(&self) -> Box<[String]> {

src/librustdoc/passes/unindent_comments/tests.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use super::*;
2+
3+
use crate::clean::types::collapse_doc_fragments;
4+
25
use rustc_span::create_default_session_globals_then;
36
use rustc_span::source_map::DUMMY_SP;
47
use rustc_span::symbol::Symbol;
@@ -19,7 +22,7 @@ fn run_test(input: &str, expected: &str) {
1922
create_default_session_globals_then(|| {
2023
let mut s = create_doc_fragment(input);
2124
unindent_fragments(&mut s);
22-
assert_eq!(&s.iter().collect::<String>(), expected);
25+
assert_eq!(collapse_doc_fragments(&s), expected);
2326
});
2427
}
2528

0 commit comments

Comments
 (0)