Skip to content

Commit 7f383c3

Browse files
camelidjyn514
authored andcommitted
Write code directly instead of using FromIterator
The FromIterator impl made the code much harder to understand. The types don't make sense until you realize there's a custom FromIterator impl.
1 parent d3f3004 commit 7f383c3

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;
@@ -958,16 +957,14 @@ fn add_doc_fragment(out: &mut String, frag: &DocFragment) {
958957
}
959958
}
960959

961-
impl<'a> FromIterator<&'a DocFragment> for String {
962-
fn from_iter<T>(iter: T) -> Self
963-
where
964-
T: IntoIterator<Item = &'a DocFragment>,
965-
{
966-
iter.into_iter().fold(String::new(), |mut acc, frag| {
967-
add_doc_fragment(&mut acc, frag);
968-
acc
969-
})
960+
/// Collapse a collection of [`DocFragment`]s into one string,
961+
/// handling indentation and newlines as needed.
962+
crate fn collapse_doc_fragments(doc_strings: &[DocFragment]) -> String {
963+
let mut acc = String::new();
964+
for frag in doc_strings {
965+
add_doc_fragment(&mut acc, frag);
970966
}
967+
acc
971968
}
972969

973970
/// A link that has not yet been rendered.
@@ -1113,7 +1110,11 @@ impl Attributes {
11131110
/// Finds all `doc` attributes as NameValues and returns their corresponding values, joined
11141111
/// with newlines.
11151112
crate fn collapsed_doc_value(&self) -> Option<String> {
1116-
if self.doc_strings.is_empty() { None } else { Some(self.doc_strings.iter().collect()) }
1113+
if self.doc_strings.is_empty() {
1114+
None
1115+
} else {
1116+
Some(collapse_doc_fragments(&self.doc_strings))
1117+
}
11171118
}
11181119

11191120
crate fn get_doc_aliases(&self) -> Box<[Symbol]> {

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::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)