From 5cb2fa487bf6405a18c13041bc18544884f603a7 Mon Sep 17 00:00:00 2001 From: Dolpheyn Date: Mon, 4 May 2020 22:14:25 +0800 Subject: [PATCH 01/12] Document From trait for Option implementations --- src/libcore/option.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 63a5277100fa8..a87f3c1804cb0 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1357,12 +1357,38 @@ impl<'a, T> IntoIterator for &'a mut Option { #[stable(since = "1.12.0", feature = "option_from")] impl From for Option { + /// Copies val to a new Option::Some + /// + /// # Examples + /// + /// ``` + /// let o: Option = Option::from(67); + /// assert_eq!(Some(67), o); + /// ``` fn from(val: T) -> Option { Some(val) } } #[stable(feature = "option_ref_from_ref_option", since = "1.30.0")] + /// Converts from &Option to Option<&T> + /// + /// # Examples + /// Converts an `Option<`[`String`]`>` into an `Option<`[`usize`]`>`, preserving the original. + /// The [`map`] method takes the `self` argument by value, consuming the original, + /// so this technique uses `as_ref` to first take an `Option` to a reference + /// to the value inside the original. + /// + /// [`map`]: enum.Option.html#method.map + /// [`String`]: ../../std/string/struct.String.html + /// [`usize`]: ../../std/primitive.usize.html + /// + /// ``` + /// let s: Option = Some(String::from("Hello, Rustaceans!")); + /// let o: Option = Option::from(&s).map(|ss: &String| ss.len()); + /// println!("Can still print s: {}", s); + /// assert_eq!(o, Some(18)); + /// ``` impl<'a, T> From<&'a Option> for Option<&'a T> { fn from(o: &'a Option) -> Option<&'a T> { o.as_ref() @@ -1371,6 +1397,19 @@ impl<'a, T> From<&'a Option> for Option<&'a T> { #[stable(feature = "option_ref_from_ref_option", since = "1.30.0")] impl<'a, T> From<&'a mut Option> for Option<&'a mut T> { + /// Converts from &mut Option to Option<&mut T> + /// + /// # Examples + /// + /// ``` + /// let mut s = Some(String::from("Hello")); + /// let o: Option<&mut String> = Option::from(&mut s); + /// match o { + /// Some(t) => *t = String::from("Hello, Rustaceans!"), + /// None => (), + /// } + /// assert_eq!(s, Some(String::from("Hello, Rustaceans!"))); + /// ``` fn from(o: &'a mut Option) -> Option<&'a mut T> { o.as_mut() } From 73f07a47b892216c47a741e229924b90a86585e1 Mon Sep 17 00:00:00 2001 From: Dolpheyn Date: Tue, 5 May 2020 08:25:20 +0800 Subject: [PATCH 02/12] Fix comment position --- src/libcore/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index a87f3c1804cb0..480fe4fd4bef7 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1371,6 +1371,7 @@ impl From for Option { } #[stable(feature = "option_ref_from_ref_option", since = "1.30.0")] +impl<'a, T> From<&'a Option> for Option<&'a T> { /// Converts from &Option to Option<&T> /// /// # Examples @@ -1389,7 +1390,6 @@ impl From for Option { /// println!("Can still print s: {}", s); /// assert_eq!(o, Some(18)); /// ``` -impl<'a, T> From<&'a Option> for Option<&'a T> { fn from(o: &'a Option) -> Option<&'a T> { o.as_ref() } From 2badd41d043c9ff549855ad832e7bcd2ad44970d Mon Sep 17 00:00:00 2001 From: Dolpheyn Date: Tue, 5 May 2020 09:50:59 +0800 Subject: [PATCH 03/12] Fix example --- src/libcore/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 480fe4fd4bef7..4647ad010067d 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1387,7 +1387,7 @@ impl<'a, T> From<&'a Option> for Option<&'a T> { /// ``` /// let s: Option = Some(String::from("Hello, Rustaceans!")); /// let o: Option = Option::from(&s).map(|ss: &String| ss.len()); - /// println!("Can still print s: {}", s); + /// println!("Can still print s: {:?}", s); /// assert_eq!(o, Some(18)); /// ``` fn from(o: &'a Option) -> Option<&'a T> { From d80ac6416d1a6b38756e18c5b5ea421bbd11dfba Mon Sep 17 00:00:00 2001 From: Faris Sufyan <47665123+Dolpheyn@users.noreply.github.com> Date: Sun, 10 May 2020 18:06:30 +0800 Subject: [PATCH 04/12] Fix link to `map` documentation in example Co-authored-by: Timo --- src/libcore/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 4647ad010067d..4df9eaa48156d 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1380,7 +1380,7 @@ impl<'a, T> From<&'a Option> for Option<&'a T> { /// so this technique uses `as_ref` to first take an `Option` to a reference /// to the value inside the original. /// - /// [`map`]: enum.Option.html#method.map + /// [`map`]: ../../std/option/enum.Option.html#method.map /// [`String`]: ../../std/string/struct.String.html /// [`usize`]: ../../std/primitive.usize.html /// From 46e9cbea3a65aff70c838ebbd157d715ee84718b Mon Sep 17 00:00:00 2001 From: Faris Sufyan <47665123+Dolpheyn@users.noreply.github.com> Date: Wed, 13 May 2020 21:04:52 +0800 Subject: [PATCH 05/12] Update src/libcore/option.rs Co-authored-by: Steve Klabnik --- src/libcore/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 4df9eaa48156d..542115763c24e 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1357,7 +1357,7 @@ impl<'a, T> IntoIterator for &'a mut Option { #[stable(since = "1.12.0", feature = "option_from")] impl From for Option { - /// Copies val to a new Option::Some + /// Copies `val` into a new `Some`. /// /// # Examples /// From 4588c26e53a39f0379a0f25ace4228bc207f4a73 Mon Sep 17 00:00:00 2001 From: Faris Sufyan <47665123+Dolpheyn@users.noreply.github.com> Date: Wed, 13 May 2020 21:05:53 +0800 Subject: [PATCH 06/12] Update src/libcore/option.rs Co-authored-by: Steve Klabnik --- src/libcore/option.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 542115763c24e..515040dd4ccab 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1363,6 +1363,7 @@ impl From for Option { /// /// ``` /// let o: Option = Option::from(67); + /// /// assert_eq!(Some(67), o); /// ``` fn from(val: T) -> Option { From a9e7d572380c271848f1137f39ef09257144bd37 Mon Sep 17 00:00:00 2001 From: Faris Sufyan <47665123+Dolpheyn@users.noreply.github.com> Date: Wed, 13 May 2020 21:12:35 +0800 Subject: [PATCH 07/12] Update src/libcore/option.rs Co-authored-by: Steve Klabnik --- src/libcore/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 515040dd4ccab..3e64177a4676c 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1373,7 +1373,7 @@ impl From for Option { #[stable(feature = "option_ref_from_ref_option", since = "1.30.0")] impl<'a, T> From<&'a Option> for Option<&'a T> { - /// Converts from &Option to Option<&T> + /// Converts from `&Option` to `Option<&T>`. /// /// # Examples /// Converts an `Option<`[`String`]`>` into an `Option<`[`usize`]`>`, preserving the original. From 644bb24e335bd15c7c90a2c31afa63d9ba5f5dbc Mon Sep 17 00:00:00 2001 From: Faris Sufyan <47665123+Dolpheyn@users.noreply.github.com> Date: Wed, 13 May 2020 21:12:50 +0800 Subject: [PATCH 08/12] Update src/libcore/option.rs Co-authored-by: Steve Klabnik --- src/libcore/option.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 3e64177a4676c..848560bd795d1 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1405,6 +1405,7 @@ impl<'a, T> From<&'a mut Option> for Option<&'a mut T> { /// ``` /// let mut s = Some(String::from("Hello")); /// let o: Option<&mut String> = Option::from(&mut s); + /// /// match o { /// Some(t) => *t = String::from("Hello, Rustaceans!"), /// None => (), From adc2bf0cfe3f4c6611ebcd80b570ed8ebd76b548 Mon Sep 17 00:00:00 2001 From: Faris Sufyan <47665123+Dolpheyn@users.noreply.github.com> Date: Wed, 13 May 2020 21:13:08 +0800 Subject: [PATCH 09/12] Update src/libcore/option.rs Co-authored-by: Steve Klabnik --- src/libcore/option.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 848560bd795d1..340637a55f7e2 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1376,6 +1376,7 @@ impl<'a, T> From<&'a Option> for Option<&'a T> { /// Converts from `&Option` to `Option<&T>`. /// /// # Examples + /// /// Converts an `Option<`[`String`]`>` into an `Option<`[`usize`]`>`, preserving the original. /// The [`map`] method takes the `self` argument by value, consuming the original, /// so this technique uses `as_ref` to first take an `Option` to a reference From f445a8286e4d889ac33935768f2ac0823b2c01ba Mon Sep 17 00:00:00 2001 From: Faris Sufyan <47665123+Dolpheyn@users.noreply.github.com> Date: Wed, 13 May 2020 21:13:17 +0800 Subject: [PATCH 10/12] Update src/libcore/option.rs Co-authored-by: Steve Klabnik --- src/libcore/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 340637a55f7e2..a6a8d4bd7e170 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1399,7 +1399,7 @@ impl<'a, T> From<&'a Option> for Option<&'a T> { #[stable(feature = "option_ref_from_ref_option", since = "1.30.0")] impl<'a, T> From<&'a mut Option> for Option<&'a mut T> { - /// Converts from &mut Option to Option<&mut T> + /// Converts from `&mut Option` to `Option<&mut T>` /// /// # Examples /// From ef1688db8e33b635287462854b7203f86c7acbf5 Mon Sep 17 00:00:00 2001 From: Faris Sufyan <47665123+Dolpheyn@users.noreply.github.com> Date: Wed, 13 May 2020 21:13:27 +0800 Subject: [PATCH 11/12] Update src/libcore/option.rs Co-authored-by: Steve Klabnik --- src/libcore/option.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index a6a8d4bd7e170..ebaa489486251 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1411,6 +1411,7 @@ impl<'a, T> From<&'a mut Option> for Option<&'a mut T> { /// Some(t) => *t = String::from("Hello, Rustaceans!"), /// None => (), /// } + /// /// assert_eq!(s, Some(String::from("Hello, Rustaceans!"))); /// ``` fn from(o: &'a mut Option) -> Option<&'a mut T> { From 6c3856f3ec5bbcd1c748d01a463f04a8fef0bd6c Mon Sep 17 00:00:00 2001 From: Faris Sufyan <47665123+Dolpheyn@users.noreply.github.com> Date: Wed, 13 May 2020 21:13:35 +0800 Subject: [PATCH 12/12] Update src/libcore/option.rs Co-authored-by: Steve Klabnik --- src/libcore/option.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index ebaa489486251..e8483875c97e5 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -1389,7 +1389,9 @@ impl<'a, T> From<&'a Option> for Option<&'a T> { /// ``` /// let s: Option = Some(String::from("Hello, Rustaceans!")); /// let o: Option = Option::from(&s).map(|ss: &String| ss.len()); + /// /// println!("Can still print s: {:?}", s); + /// /// assert_eq!(o, Some(18)); /// ``` fn from(o: &'a Option) -> Option<&'a T> {