diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2021-12-19 00:38:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-19 00:38:44 +0100 |
| commit | d486e68ab29a8c5ba2e776a0ff74a760fd3edf19 (patch) | |
| tree | 50f76b229371100d6341f512e266fe8ffcd960be /src | |
| parent | 74c3ce9207cfb1fd14642edf1e010a86156e6145 (diff) | |
| parent | 7f383c3aaf38c94fb57b0a926b057f563b8faa47 (diff) | |
| download | rust-d486e68ab29a8c5ba2e776a0ff74a760fd3edf19.tar.gz rust-d486e68ab29a8c5ba2e776a0ff74a760fd3edf19.zip | |
Rollup merge of #92082 - jyn514:remove-from-iterator, r=jyn514
rustdoc: Write doc-comments 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. This is the first commit from https://github.com/rust-lang/rust/pull/91305; since ``@camelid`` wrote it originally I don't feel bad unilaterally approving it. r? ``@ghost`` ``@bors`` r+ Note that this will conflict with https://github.com/rust-lang/rust/pull/92078.
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustdoc/clean/types.rs | 23 | ||||
| -rw-r--r-- | src/librustdoc/passes/unindent_comments/tests.rs | 5 |
2 files changed, 16 insertions, 12 deletions
diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 6ce6d889150..2bd90f67cf4 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1,7 +1,6 @@ use std::cell::RefCell; use std::default::Default; use std::hash::{Hash, Hasher}; -use std::iter::FromIterator; use std::lazy::SyncOnceCell as OnceCell; use std::path::PathBuf; use std::rc::Rc; @@ -958,16 +957,14 @@ fn add_doc_fragment(out: &mut String, frag: &DocFragment) { } } -impl<'a> FromIterator<&'a DocFragment> for String { - fn from_iter<T>(iter: T) -> Self - where - T: IntoIterator<Item = &'a DocFragment>, - { - iter.into_iter().fold(String::new(), |mut acc, frag| { - add_doc_fragment(&mut acc, frag); - acc - }) +/// Collapse a collection of [`DocFragment`]s into one string, +/// handling indentation and newlines as needed. +crate fn collapse_doc_fragments(doc_strings: &[DocFragment]) -> String { + let mut acc = String::new(); + for frag in doc_strings { + add_doc_fragment(&mut acc, frag); } + acc } /// A link that has not yet been rendered. @@ -1113,7 +1110,11 @@ impl Attributes { /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined /// with newlines. crate fn collapsed_doc_value(&self) -> Option<String> { - if self.doc_strings.is_empty() { None } else { Some(self.doc_strings.iter().collect()) } + if self.doc_strings.is_empty() { + None + } else { + Some(collapse_doc_fragments(&self.doc_strings)) + } } crate fn get_doc_aliases(&self) -> Box<[Symbol]> { diff --git a/src/librustdoc/passes/unindent_comments/tests.rs b/src/librustdoc/passes/unindent_comments/tests.rs index daec04e11cd..3d3d2e50321 100644 --- a/src/librustdoc/passes/unindent_comments/tests.rs +++ b/src/librustdoc/passes/unindent_comments/tests.rs @@ -1,4 +1,7 @@ use super::*; + +use crate::clean::collapse_doc_fragments; + use rustc_span::create_default_session_globals_then; use rustc_span::source_map::DUMMY_SP; use rustc_span::symbol::Symbol; @@ -19,7 +22,7 @@ fn run_test(input: &str, expected: &str) { create_default_session_globals_then(|| { let mut s = create_doc_fragment(input); unindent_fragments(&mut s); - assert_eq!(&s.iter().collect::<String>(), expected); + assert_eq!(collapse_doc_fragments(&s), expected); }); } |
