From 333e1ca319636d23983de1f8ea2c102aae731c54 Mon Sep 17 00:00:00 2001 From: Chris Gregory Date: Thu, 30 May 2019 16:46:53 -0500 Subject: [PATCH 1/9] Add Bound::cloned() --- src/libcore/ops/range.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/libcore/ops/range.rs b/src/libcore/ops/range.rs index a707f0cc0627a..39bf80281c576 100644 --- a/src/libcore/ops/range.rs +++ b/src/libcore/ops/range.rs @@ -696,6 +696,28 @@ pub enum Bound { Unbounded, } +impl Bound<&T> { + /// Map a `Bound<&T>` to a `Bound` by cloning the contents of the bound. + /// + /// # Examples + /// + /// ``` + /// use std::ops::Bound::*; + /// use std::ops::RangeBounds; + /// + /// assert_eq!((1..12).start_bound(), Included(&1)); + /// assert_eq!((1..12).start_bound().cloned(), Included(1)); + /// ``` + #[unstable(feature = "bound_cloned", issue = 61356)] + fn cloned(&self) -> Bound { + match self { + Bound::Unbounded => Bound::Unbounded, + Bound::Included(x) => Bound::Included(x.clone()), + Bound::Excluded(x) => Bound::Excluded(x.clone()), + } + } +} + #[stable(feature = "collections_range", since = "1.28.0")] /// `RangeBounds` is implemented by Rust's built-in range types, produced /// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`. From 867c4a5fdac44d683035d8eaaf69f43adffcc5ca Mon Sep 17 00:00:00 2001 From: Chris Gregory Date: Thu, 30 May 2019 21:52:52 -0500 Subject: [PATCH 2/9] Fix compilation errors --- src/libcore/ops/range.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/ops/range.rs b/src/libcore/ops/range.rs index 39bf80281c576..a04c1bc033929 100644 --- a/src/libcore/ops/range.rs +++ b/src/libcore/ops/range.rs @@ -708,9 +708,9 @@ impl Bound<&T> { /// assert_eq!((1..12).start_bound(), Included(&1)); /// assert_eq!((1..12).start_bound().cloned(), Included(1)); /// ``` - #[unstable(feature = "bound_cloned", issue = 61356)] + #[unstable(feature = "bound_cloned", issue = "61356")] fn cloned(&self) -> Bound { - match self { + match *self { Bound::Unbounded => Bound::Unbounded, Bound::Included(x) => Bound::Included(x.clone()), Bound::Excluded(x) => Bound::Excluded(x.clone()), From 0772f9a92082c3f5a734e1fae198f817bbf2f61f Mon Sep 17 00:00:00 2001 From: Chris Gregory Date: Fri, 31 May 2019 11:36:28 -0500 Subject: [PATCH 3/9] Make Bound::cloned public --- src/libcore/ops/range.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/ops/range.rs b/src/libcore/ops/range.rs index a04c1bc033929..e9c5f1d4a8bfa 100644 --- a/src/libcore/ops/range.rs +++ b/src/libcore/ops/range.rs @@ -709,7 +709,7 @@ impl Bound<&T> { /// assert_eq!((1..12).start_bound().cloned(), Included(1)); /// ``` #[unstable(feature = "bound_cloned", issue = "61356")] - fn cloned(&self) -> Bound { + pub fn cloned(&self) -> Bound { match *self { Bound::Unbounded => Bound::Unbounded, Bound::Included(x) => Bound::Included(x.clone()), From df845784ee74cee760caf38064d39e14a23ef791 Mon Sep 17 00:00:00 2001 From: Chris Gregory Date: Fri, 31 May 2019 11:36:37 -0500 Subject: [PATCH 4/9] Add Bound tests --- src/libcore/tests/ops.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/libcore/tests/ops.rs b/src/libcore/tests/ops.rs index 78cf07119e729..3f6d63fc8a151 100644 --- a/src/libcore/tests/ops.rs +++ b/src/libcore/tests/ops.rs @@ -82,3 +82,18 @@ fn test_range_is_empty() { assert!( (NAN ..= EPSILON).is_empty()); assert!( (NAN ..= NAN).is_empty()); } + +#[test] +fn test_bound_cloned_unbounded() { + assert_eq!(Bound::<&u32>::Unbounded.cloned(), Bound::Unbounded); +} + +#[test] +fn test_bound_cloned_included() { + assert_eq!(Bound::Included(&3).cloned(), Bound::Included(3)); +} + +#[test] +fn test_bound_cloned_excluded() { + assert_eq!(Bound::Excluded(&3).cloned(), Bound::Excluded(3)); +} From a21a0e0268a2121339148b97ed31bffedf860021 Mon Sep 17 00:00:00 2001 From: Chris Gregory Date: Fri, 31 May 2019 16:56:26 -0500 Subject: [PATCH 5/9] Import Bound in tests --- src/libcore/tests/ops.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/tests/ops.rs b/src/libcore/tests/ops.rs index 3f6d63fc8a151..48755ae4c1641 100644 --- a/src/libcore/tests/ops.rs +++ b/src/libcore/tests/ops.rs @@ -1,4 +1,4 @@ -use core::ops::{Range, RangeFull, RangeFrom, RangeTo, RangeInclusive}; +use core::ops::{Bound, Range, RangeFull, RangeFrom, RangeTo, RangeInclusive}; // Test the Range structs without the syntactic sugar. From c3824919fd29c936f07806733c463e73172c60a2 Mon Sep 17 00:00:00 2001 From: Chris Gregory Date: Fri, 31 May 2019 21:17:32 -0500 Subject: [PATCH 6/9] Enable feature bound_cloned --- src/libcore/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 4a70329b64bc9..64d70b5de21dd 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -70,6 +70,7 @@ #![feature(arbitrary_self_types)] #![feature(asm)] #![feature(associated_type_defaults)] +#![feature(bound_cloned)] #![feature(cfg_target_has_atomic)] #![feature(concat_idents)] #![feature(const_fn)] From c478efbe6a2e324910f2f82db7d03a6476215ca2 Mon Sep 17 00:00:00 2001 From: Chris Gregory Date: Sat, 1 Jun 2019 22:39:13 -0700 Subject: [PATCH 7/9] Enable feature bound_cloned for tests --- src/libcore/ops/range.rs | 1 + src/libcore/tests/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libcore/ops/range.rs b/src/libcore/ops/range.rs index e9c5f1d4a8bfa..ddcf3addf398f 100644 --- a/src/libcore/ops/range.rs +++ b/src/libcore/ops/range.rs @@ -702,6 +702,7 @@ impl Bound<&T> { /// # Examples /// /// ``` + /// #![feature(bound_cloned)] /// use std::ops::Bound::*; /// use std::ops::RangeBounds; /// diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index c617596aba801..7465ef4a44e24 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -1,3 +1,4 @@ +#![feature(bound_cloned)] #![feature(box_syntax)] #![feature(cell_update)] #![feature(core_private_bignum)] From 4a88614c4f46e1786a5bf2ce650631cc250a6edb Mon Sep 17 00:00:00 2001 From: Chris Gregory Date: Wed, 5 Jun 2019 21:11:02 -0700 Subject: [PATCH 8/9] Take self by value (Self is Copy here) --- src/libcore/ops/range.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/ops/range.rs b/src/libcore/ops/range.rs index ddcf3addf398f..763020c478fba 100644 --- a/src/libcore/ops/range.rs +++ b/src/libcore/ops/range.rs @@ -710,7 +710,7 @@ impl Bound<&T> { /// assert_eq!((1..12).start_bound().cloned(), Included(1)); /// ``` #[unstable(feature = "bound_cloned", issue = "61356")] - pub fn cloned(&self) -> Bound { + pub fn cloned(self) -> Bound { match *self { Bound::Unbounded => Bound::Unbounded, Bound::Included(x) => Bound::Included(x.clone()), From c1bc8f11cb36cade87422b91a0bdec1fe8b5af41 Mon Sep 17 00:00:00 2001 From: Chris Gregory Date: Wed, 5 Jun 2019 21:23:45 -0700 Subject: [PATCH 9/9] Remove dereference Co-Authored-By: Steven Fackler --- src/libcore/ops/range.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/ops/range.rs b/src/libcore/ops/range.rs index 763020c478fba..1b4c4218cc15b 100644 --- a/src/libcore/ops/range.rs +++ b/src/libcore/ops/range.rs @@ -711,7 +711,7 @@ impl Bound<&T> { /// ``` #[unstable(feature = "bound_cloned", issue = "61356")] pub fn cloned(self) -> Bound { - match *self { + match self { Bound::Unbounded => Bound::Unbounded, Bound::Included(x) => Bound::Included(x.clone()), Bound::Excluded(x) => Bound::Excluded(x.clone()),