Skip to content

Commit 654854f

Browse files
authored
Rollup merge of rust-lang#61376 - czipperz:bound-cloned, r=sfackler
Add Bound::cloned() Suggested by rust-lang#61356
2 parents 8b36867 + c1bc8f1 commit 654854f

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#![feature(arbitrary_self_types)]
7171
#![feature(asm)]
7272
#![feature(associated_type_defaults)]
73+
#![feature(bound_cloned)]
7374
#![feature(cfg_target_has_atomic)]
7475
#![feature(concat_idents)]
7576
#![feature(const_fn)]

src/libcore/ops/range.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,29 @@ pub enum Bound<T> {
696696
Unbounded,
697697
}
698698

699+
impl<T: Clone> Bound<&T> {
700+
/// Map a `Bound<&T>` to a `Bound<T>` by cloning the contents of the bound.
701+
///
702+
/// # Examples
703+
///
704+
/// ```
705+
/// #![feature(bound_cloned)]
706+
/// use std::ops::Bound::*;
707+
/// use std::ops::RangeBounds;
708+
///
709+
/// assert_eq!((1..12).start_bound(), Included(&1));
710+
/// assert_eq!((1..12).start_bound().cloned(), Included(1));
711+
/// ```
712+
#[unstable(feature = "bound_cloned", issue = "61356")]
713+
pub fn cloned(self) -> Bound<T> {
714+
match self {
715+
Bound::Unbounded => Bound::Unbounded,
716+
Bound::Included(x) => Bound::Included(x.clone()),
717+
Bound::Excluded(x) => Bound::Excluded(x.clone()),
718+
}
719+
}
720+
}
721+
699722
#[stable(feature = "collections_range", since = "1.28.0")]
700723
/// `RangeBounds` is implemented by Rust's built-in range types, produced
701724
/// by range syntax like `..`, `a..`, `..b`, `..=c`, `d..e`, or `f..=g`.

src/libcore/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(bound_cloned)]
12
#![feature(box_syntax)]
23
#![feature(cell_update)]
34
#![feature(core_private_bignum)]

src/libcore/tests/ops.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::ops::{Range, RangeFull, RangeFrom, RangeTo, RangeInclusive};
1+
use core::ops::{Bound, Range, RangeFull, RangeFrom, RangeTo, RangeInclusive};
22

33
// Test the Range structs without the syntactic sugar.
44

@@ -82,3 +82,18 @@ fn test_range_is_empty() {
8282
assert!( (NAN ..= EPSILON).is_empty());
8383
assert!( (NAN ..= NAN).is_empty());
8484
}
85+
86+
#[test]
87+
fn test_bound_cloned_unbounded() {
88+
assert_eq!(Bound::<&u32>::Unbounded.cloned(), Bound::Unbounded);
89+
}
90+
91+
#[test]
92+
fn test_bound_cloned_included() {
93+
assert_eq!(Bound::Included(&3).cloned(), Bound::Included(3));
94+
}
95+
96+
#[test]
97+
fn test_bound_cloned_excluded() {
98+
assert_eq!(Bound::Excluded(&3).cloned(), Bound::Excluded(3));
99+
}

0 commit comments

Comments
 (0)