From daa62d9081bfeeb0f438f88eb69082c227a5c221 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 9 Sep 2020 17:40:15 +0200 Subject: [PATCH 1/6] Add as_str() and AsRef to string::Drain. --- library/alloc/src/string.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index e1724bf3c9a90..d27dd9c93402c 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2462,6 +2462,32 @@ impl Drop for Drain<'_> { } } +impl<'a> Drain<'a> { + /// Returns the remaining (sub)string of this iterator as a slice. + /// + /// # Examples + /// + /// ``` + /// #![feature(string_drain_as_str)] + /// let mut s = String::from("abc"); + /// let mut drain = s.drain(..); + /// assert_eq!(drain.as_str(), "abc"); + /// let _ = drain.next().unwrap(); + /// assert_eq!(drain.as_str(), "bc"); + /// ``` + #[unstable(feature = "string_drain_as_str", issue = "none")] + pub fn as_str(&self) -> &str { + self.iter.as_str() + } +} + +#[unstable(feature = "string_drain_as_str", issue = "none")] +impl<'a> AsRef for Drain<'a> { + fn as_ref(&self) -> &str { + self.as_str() + } +} + #[stable(feature = "drain", since = "1.6.0")] impl Iterator for Drain<'_> { type Item = char; From 673284058b53992cfd66c04b9db8e551a0dc9996 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 9 Sep 2020 17:40:44 +0200 Subject: [PATCH 2/6] Show remaining data in string::Drain's Debug impl. --- library/alloc/src/string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index d27dd9c93402c..14ffd06e9bdfd 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2439,7 +2439,7 @@ pub struct Drain<'a> { #[stable(feature = "collection_debug", since = "1.17.0")] impl fmt::Debug for Drain<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.pad("Drain { .. }") + f.debug_tuple("Drain").field(&self.as_str()).finish() } } From f5bb523e94b431c35b495b6ad6ae94495b653a5f Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 9 Sep 2020 17:59:43 +0200 Subject: [PATCH 3/6] Add AsRef<[u8]> for String's Drain. --- library/alloc/src/string.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 14ffd06e9bdfd..a164de8b4d387 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2488,6 +2488,13 @@ impl<'a> AsRef for Drain<'a> { } } +#[unstable(feature = "string_drain_as_str", issue = "none")] +impl<'a> AsRef<[u8]> for Drain<'a> { + fn as_ref(&self) -> &[u8] { + self.as_str().as_bytes() + } +} + #[stable(feature = "drain", since = "1.6.0")] impl Iterator for Drain<'_> { type Item = char; From f2a32909e0649eb589406ddac63597ba34273c95 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 9 Sep 2020 19:10:06 +0200 Subject: [PATCH 4/6] Mark AsRef impls for String's Drain as stable. Trait implementations effectively can't be #[unstable]. --- library/alloc/src/string.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index a164de8b4d387..047ae942cd9a2 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2481,14 +2481,14 @@ impl<'a> Drain<'a> { } } -#[unstable(feature = "string_drain_as_str", issue = "none")] +#[stable(feature = "string_drain_as_ref", since = "1.48.0")] impl<'a> AsRef for Drain<'a> { fn as_ref(&self) -> &str { self.as_str() } } -#[unstable(feature = "string_drain_as_str", issue = "none")] +#[stable(feature = "string_drain_as_ref", since = "1.48.0")] impl<'a> AsRef<[u8]> for Drain<'a> { fn as_ref(&self) -> &[u8] { self.as_str().as_bytes() From 829019d4043a6e9dd1305113f43b30fc8415893d Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 9 Sep 2020 19:38:51 +0200 Subject: [PATCH 5/6] Disable AsRef implementations for String's Drain. Since trait implementations cannot be unstable, we should only add them when the as_str feature gets stabilized. Until then, only `.as_str()` is available (behind a feature gate). --- library/alloc/src/string.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 047ae942cd9a2..b9506281ed629 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2475,25 +2475,26 @@ impl<'a> Drain<'a> { /// let _ = drain.next().unwrap(); /// assert_eq!(drain.as_str(), "bc"); /// ``` - #[unstable(feature = "string_drain_as_str", issue = "none")] + #[unstable(feature = "string_drain_as_str", issue = "none")] // Note: uncomment AsRef impls below when stabilizing. pub fn as_str(&self) -> &str { self.iter.as_str() } } -#[stable(feature = "string_drain_as_ref", since = "1.48.0")] -impl<'a> AsRef for Drain<'a> { - fn as_ref(&self) -> &str { - self.as_str() - } -} - -#[stable(feature = "string_drain_as_ref", since = "1.48.0")] -impl<'a> AsRef<[u8]> for Drain<'a> { - fn as_ref(&self) -> &[u8] { - self.as_str().as_bytes() - } -} +// Uncomment when stabilizing `string_drain_as_str`. +// #[unstable(feature = "string_drain_as_str", issue = "none")] +// impl<'a> AsRef for Drain<'a> { +// fn as_ref(&self) -> &str { +// self.as_str() +// } +// } +// +// #[unstable(feature = "string_drain_as_str", issue = "none")] +// impl<'a> AsRef<[u8]> for Drain<'a> { +// fn as_ref(&self) -> &[u8] { +// self.as_str().as_bytes() +// } +// } #[stable(feature = "drain", since = "1.6.0")] impl Iterator for Drain<'_> { From 15eb638dc9c9f663ab9e596a838c33821ae7f772 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sat, 19 Sep 2020 08:23:23 +0200 Subject: [PATCH 6/6] Add tracking issue number for string_drain_as_str. --- library/alloc/src/string.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index b9506281ed629..ed6443da830d0 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -2475,21 +2475,21 @@ impl<'a> Drain<'a> { /// let _ = drain.next().unwrap(); /// assert_eq!(drain.as_str(), "bc"); /// ``` - #[unstable(feature = "string_drain_as_str", issue = "none")] // Note: uncomment AsRef impls below when stabilizing. + #[unstable(feature = "string_drain_as_str", issue = "76905")] // Note: uncomment AsRef impls below when stabilizing. pub fn as_str(&self) -> &str { self.iter.as_str() } } // Uncomment when stabilizing `string_drain_as_str`. -// #[unstable(feature = "string_drain_as_str", issue = "none")] +// #[unstable(feature = "string_drain_as_str", issue = "76905")] // impl<'a> AsRef for Drain<'a> { // fn as_ref(&self) -> &str { // self.as_str() // } // } // -// #[unstable(feature = "string_drain_as_str", issue = "none")] +// #[unstable(feature = "string_drain_as_str", issue = "76905")] // impl<'a> AsRef<[u8]> for Drain<'a> { // fn as_ref(&self) -> &[u8] { // self.as_str().as_bytes()