Skip to content

Commit 0fa8265

Browse files
authored
Rollup merge of rust-lang#145968 - connortsui20:bound-copied, r=joboet
Add `Bound::copied` Tracking Issue: rust-lang#145966 Some questions: - [x] Should I update the documentation for `cloned` to actual used a `Clone` type instead of an integer? - [x] I removed the `must_use` since this is a cheap copy, does that make sense?
2 parents d17b3fb + 114c0c2 commit 0fa8265

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

library/core/src/ops/range.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,31 @@ impl<T> Bound<T> {
736736
}
737737
}
738738

739+
impl<T: Copy> Bound<&T> {
740+
/// Map a `Bound<&T>` to a `Bound<T>` by copying the contents of the bound.
741+
///
742+
/// # Examples
743+
///
744+
/// ```
745+
/// #![feature(bound_copied)]
746+
///
747+
/// use std::ops::Bound::*;
748+
/// use std::ops::RangeBounds;
749+
///
750+
/// assert_eq!((1..12).start_bound(), Included(&1));
751+
/// assert_eq!((1..12).start_bound().copied(), Included(1));
752+
/// ```
753+
#[unstable(feature = "bound_copied", issue = "145966")]
754+
#[must_use]
755+
pub fn copied(self) -> Bound<T> {
756+
match self {
757+
Bound::Unbounded => Bound::Unbounded,
758+
Bound::Included(x) => Bound::Included(*x),
759+
Bound::Excluded(x) => Bound::Excluded(*x),
760+
}
761+
}
762+
}
763+
739764
impl<T: Clone> Bound<&T> {
740765
/// Map a `Bound<&T>` to a `Bound<T>` by cloning the contents of the bound.
741766
///
@@ -745,8 +770,11 @@ impl<T: Clone> Bound<&T> {
745770
/// use std::ops::Bound::*;
746771
/// use std::ops::RangeBounds;
747772
///
748-
/// assert_eq!((1..12).start_bound(), Included(&1));
749-
/// assert_eq!((1..12).start_bound().cloned(), Included(1));
773+
/// let a1 = String::from("a");
774+
/// let (a2, a3, a4) = (a1.clone(), a1.clone(), a1.clone());
775+
///
776+
/// assert_eq!(Included(&a1), (a2..).start_bound());
777+
/// assert_eq!(Included(a3), (a4..).start_bound().cloned());
750778
/// ```
751779
#[must_use = "`self` will be dropped if the result is not used"]
752780
#[stable(feature = "bound_cloned", since = "1.55.0")]

0 commit comments

Comments
 (0)