Skip to content

Commit

Permalink
Rollup merge of rust-lang#61376 - czipperz:bound-cloned, r=sfackler
Browse files Browse the repository at this point in the history
Add Bound::cloned()

Suggested by rust-lang#61356
  • Loading branch information
Centril authored Jun 6, 2019
2 parents 8b36867 + c1bc8f1 commit 6c5c143
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
23 changes: 23 additions & 0 deletions src/libcore/ops/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,29 @@ pub enum Bound<T> {
Unbounded,
}

impl<T: Clone> Bound<&T> {
/// Map a `Bound<&T>` to a `Bound<T>` by cloning the contents of the bound.
///
/// # Examples
///
/// ```
/// #![feature(bound_cloned)]
/// 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")]
pub fn cloned(self) -> Bound<T> {
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`.
Expand Down
1 change: 1 addition & 0 deletions src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(bound_cloned)]
#![feature(box_syntax)]
#![feature(cell_update)]
#![feature(core_private_bignum)]
Expand Down
17 changes: 16 additions & 1 deletion src/libcore/tests/ops.rs
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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));
}

0 comments on commit 6c5c143

Please sign in to comment.