diff options
| author | bors <bors@rust-lang.org> | 2021-03-19 23:53:02 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-03-19 23:53:02 +0000 |
| commit | 6bfbf0c33a86707cedd02ca985285191282a80b3 (patch) | |
| tree | 3379b7c53a90fdbb23ef8c041ae4a8eb0b1955fe /library/core/src | |
| parent | f5f33ec0e0455eefa72fc5567eb1280a4d5ee206 (diff) | |
| parent | 51a29cbb23d2146322350fbfde53fe9523e554fb (diff) | |
| download | rust-6bfbf0c33a86707cedd02ca985285191282a80b3.tar.gz rust-6bfbf0c33a86707cedd02ca985285191282a80b3.zip | |
Auto merge of #83308 - Dylan-DPC:rollup-p2j6sy8, r=Dylan-DPC
Rollup of 8 pull requests Successful merges: - #79986 (Only build help popup when it's really needed) - #82570 (Add `as_str` method for split whitespace str iterators) - #83244 (Fix overflowing length in Vec<ZST> to VecDeque) - #83254 (Include output stream in `panic!()` documentation) - #83269 (Revert the second deprecation of collections::Bound) - #83277 (Mark early otherwise optimization unsound) - #83285 (Update LLVM to bring in SIMD updates for WebAssembly) - #83297 (Do not ICE on ty::Error as an error must already have been reported) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'library/core/src')
| -rw-r--r-- | library/core/src/iter/adapters/filter.rs | 3 | ||||
| -rw-r--r-- | library/core/src/iter/adapters/map.rs | 3 | ||||
| -rw-r--r-- | library/core/src/macros/panic.md | 15 | ||||
| -rw-r--r-- | library/core/src/slice/iter.rs | 6 | ||||
| -rw-r--r-- | library/core/src/str/iter.rs | 53 |
5 files changed, 71 insertions, 9 deletions
diff --git a/library/core/src/iter/adapters/filter.rs b/library/core/src/iter/adapters/filter.rs index f8d684fcdda..0337892b9e8 100644 --- a/library/core/src/iter/adapters/filter.rs +++ b/library/core/src/iter/adapters/filter.rs @@ -13,7 +13,8 @@ use crate::ops::Try; #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone)] pub struct Filter<I, P> { - iter: I, + // Used for `SplitWhitespace` and `SplitAsciiWhitespace` `as_str` methods + pub(crate) iter: I, predicate: P, } impl<I, P> Filter<I, P> { diff --git a/library/core/src/iter/adapters/map.rs b/library/core/src/iter/adapters/map.rs index 2d997cfe509..2a4b7efd5e6 100644 --- a/library/core/src/iter/adapters/map.rs +++ b/library/core/src/iter/adapters/map.rs @@ -57,7 +57,8 @@ use crate::ops::Try; #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone)] pub struct Map<I, F> { - iter: I, + // Used for `SplitWhitespace` and `SplitAsciiWhitespace` `as_str` methods + pub(crate) iter: I, f: F, } diff --git a/library/core/src/macros/panic.md b/library/core/src/macros/panic.md index 6e502426df9..5127a16bbfd 100644 --- a/library/core/src/macros/panic.md +++ b/library/core/src/macros/panic.md @@ -9,11 +9,15 @@ tests. `panic!` is closely tied with the `unwrap` method of both [`Option`][ounwrap] and [`Result`][runwrap] enums. Both implementations call `panic!` when they are set to [`None`] or [`Err`] variants. -This macro is used to inject panic into a Rust thread, causing the thread to -panic entirely. This macro panics with a string and uses the [`format!`] syntax -for building the message. - -Each thread's panic can be reaped as the [`Box`]`<`[`Any`]`>` type, +When using `panic!()` you can specify a string payload, that is built using +the [`format!`] syntax. That payload is used when injecting the panic into +the calling Rust thread, causing the thread to panic entirely. + +The behavior of the default `std` hook, i.e. the code that runs directly +after the panic is invoked, is to print the message payload to +`stderr` along with the file/line/column information of the `panic!()` +call. You can override the panic hook using [`std::panic::set_hook()`]. +Inside the hook a panic can be accessed as a `&dyn Any + Send`, which contains either a `&str` or `String` for regular `panic!()` invocations. To panic with a value of another other type, [`panic_any`] can be used. @@ -26,6 +30,7 @@ See also the macro [`compile_error!`], for raising errors during compilation. [ounwrap]: Option::unwrap [runwrap]: Result::unwrap +[`std::panic::set_hook()`]: ../std/panic/fn.set_hook.html [`panic_any`]: ../std/panic/fn.panic_any.html [`Box`]: ../std/boxed/struct.Box.html [`Any`]: crate::any::Any diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index 796301c76b6..c82b76df6ff 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -335,9 +335,11 @@ pub struct Split<'a, T: 'a, P> where P: FnMut(&T) -> bool, { - v: &'a [T], + // Used for `SplitWhitespace` and `SplitAsciiWhitespace` `as_str` methods + pub(crate) v: &'a [T], pred: P, - finished: bool, + // Used for `SplitAsciiWhitespace` `as_str` method + pub(crate) finished: bool, } impl<'a, T: 'a, P: FnMut(&T) -> bool> Split<'a, T, P> { diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 0c9307a6d2f..4eac017f915 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -1200,6 +1200,30 @@ impl<'a> DoubleEndedIterator for SplitWhitespace<'a> { #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for SplitWhitespace<'_> {} +impl<'a> SplitWhitespace<'a> { + /// Returns remainder of the splitted string + /// + /// # Examples + /// + /// ``` + /// #![feature(str_split_whitespace_as_str)] + /// + /// let mut split = "Mary had a little lamb".split_whitespace(); + /// assert_eq!(split.as_str(), "Mary had a little lamb"); + /// + /// split.next(); + /// assert_eq!(split.as_str(), "had a little lamb"); + /// + /// split.by_ref().for_each(drop); + /// assert_eq!(split.as_str(), ""); + /// ``` + #[inline] + #[unstable(feature = "str_split_whitespace_as_str", issue = "77998")] + pub fn as_str(&self) -> &'a str { + self.inner.iter.as_str() + } +} + #[stable(feature = "split_ascii_whitespace", since = "1.34.0")] impl<'a> Iterator for SplitAsciiWhitespace<'a> { type Item = &'a str; @@ -1231,6 +1255,35 @@ impl<'a> DoubleEndedIterator for SplitAsciiWhitespace<'a> { #[stable(feature = "split_ascii_whitespace", since = "1.34.0")] impl FusedIterator for SplitAsciiWhitespace<'_> {} +impl<'a> SplitAsciiWhitespace<'a> { + /// Returns remainder of the splitted string + /// + /// # Examples + /// + /// ``` + /// #![feature(str_split_whitespace_as_str)] + /// + /// let mut split = "Mary had a little lamb".split_ascii_whitespace(); + /// assert_eq!(split.as_str(), "Mary had a little lamb"); + /// + /// split.next(); + /// assert_eq!(split.as_str(), "had a little lamb"); + /// + /// split.by_ref().for_each(drop); + /// assert_eq!(split.as_str(), ""); + /// ``` + #[inline] + #[unstable(feature = "str_split_whitespace_as_str", issue = "77998")] + pub fn as_str(&self) -> &'a str { + if self.inner.iter.iter.finished { + return ""; + } + + // SAFETY: Slice is created from str. + unsafe { crate::str::from_utf8_unchecked(&self.inner.iter.iter.v) } + } +} + #[stable(feature = "split_inclusive", since = "1.51.0")] impl<'a, P: Pattern<'a>> Iterator for SplitInclusive<'a, P> { type Item = &'a str; |
