Skip to content

Commit 1594076

Browse files
authoredJun 5, 2021
Rollup merge of #83646 - glittershark:bound-map, r=m-ou-se
Add a map method to Bound Add a map method to std::ops::range::Bound, patterned off of the method of the same name on Option. Have left off creating a tracking issue initially, but as soon as I get the go-ahead from a reviewer I'll make that right away 😄
2 parents 9104c89 + 223c0d2 commit 1594076

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed
 

‎library/core/src/ops/range.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -674,10 +674,10 @@ pub enum Bound<T> {
674674
Unbounded,
675675
}
676676

677-
#[unstable(feature = "bound_as_ref", issue = "80996")]
678677
impl<T> Bound<T> {
679678
/// Converts from `&Bound<T>` to `Bound<&T>`.
680679
#[inline]
680+
#[unstable(feature = "bound_as_ref", issue = "80996")]
681681
pub fn as_ref(&self) -> Bound<&T> {
682682
match *self {
683683
Included(ref x) => Included(x),
@@ -688,13 +688,47 @@ impl<T> Bound<T> {
688688

689689
/// Converts from `&mut Bound<T>` to `Bound<&T>`.
690690
#[inline]
691+
#[unstable(feature = "bound_as_ref", issue = "80996")]
691692
pub fn as_mut(&mut self) -> Bound<&mut T> {
692693
match *self {
693694
Included(ref mut x) => Included(x),
694695
Excluded(ref mut x) => Excluded(x),
695696
Unbounded => Unbounded,
696697
}
697698
}
699+
700+
/// Maps a `Bound<T>` to a `Bound<U>` by applying a function to the contained value (including
701+
/// both `Included` and `Excluded`), returning a `Bound` of the same kind.
702+
///
703+
/// # Examples
704+
///
705+
/// ```
706+
/// #![feature(bound_map)]
707+
/// use std::ops::Bound::*;
708+
///
709+
/// let bound_string = Included("Hello, World!");
710+
///
711+
/// assert_eq!(bound_string.map(|s| s.len()), Included(13));
712+
/// ```
713+
///
714+
/// ```
715+
/// #![feature(bound_map)]
716+
/// use std::ops::Bound;
717+
/// use Bound::*;
718+
///
719+
/// let unbounded_string: Bound<String> = Unbounded;
720+
///
721+
/// assert_eq!(unbounded_string.map(|s| s.len()), Unbounded);
722+
/// ```
723+
#[inline]
724+
#[unstable(feature = "bound_map", issue = "86026")]
725+
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Bound<U> {
726+
match self {
727+
Unbounded => Unbounded,
728+
Included(x) => Included(f(x)),
729+
Excluded(x) => Excluded(f(x)),
730+
}
731+
}
698732
}
699733

700734
impl<T: Clone> Bound<&T> {

0 commit comments

Comments
 (0)
Please sign in to comment.