diff options
| author | Griffin Smith <root@gws.fyi> | 2021-03-29 10:35:21 -0400 |
|---|---|---|
| committer | Griffin Smith <root@gws.fyi> | 2021-06-05 17:22:30 +0200 |
| commit | 223c0d2a852be3dc74e0bfee875d63fcf161c757 (patch) | |
| tree | ccf90eab958b4261c6f11f996f3009b3e72afd3d | |
| parent | 3aedcf06b73fc36feeebca3d579e1d2a6c40acc5 (diff) | |
| download | rust-223c0d2a852be3dc74e0bfee875d63fcf161c757.tar.gz rust-223c0d2a852be3dc74e0bfee875d63fcf161c757.zip | |
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
| -rw-r--r-- | library/core/src/ops/range.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/library/core/src/ops/range.rs b/library/core/src/ops/range.rs index dbeb3912130..684e6bb4a0f 100644 --- a/library/core/src/ops/range.rs +++ b/library/core/src/ops/range.rs @@ -674,10 +674,10 @@ pub enum Bound<T> { Unbounded, } -#[unstable(feature = "bound_as_ref", issue = "80996")] impl<T> Bound<T> { /// Converts from `&Bound<T>` to `Bound<&T>`. #[inline] + #[unstable(feature = "bound_as_ref", issue = "80996")] pub fn as_ref(&self) -> Bound<&T> { match *self { Included(ref x) => Included(x), @@ -688,6 +688,7 @@ impl<T> Bound<T> { /// Converts from `&mut Bound<T>` to `Bound<&T>`. #[inline] + #[unstable(feature = "bound_as_ref", issue = "80996")] pub fn as_mut(&mut self) -> Bound<&mut T> { match *self { Included(ref mut x) => Included(x), @@ -695,6 +696,39 @@ impl<T> Bound<T> { Unbounded => Unbounded, } } + + /// Maps a `Bound<T>` to a `Bound<U>` by applying a function to the contained value (including + /// both `Included` and `Excluded`), returning a `Bound` of the same kind. + /// + /// # Examples + /// + /// ``` + /// #![feature(bound_map)] + /// use std::ops::Bound::*; + /// + /// let bound_string = Included("Hello, World!"); + /// + /// assert_eq!(bound_string.map(|s| s.len()), Included(13)); + /// ``` + /// + /// ``` + /// #![feature(bound_map)] + /// use std::ops::Bound; + /// use Bound::*; + /// + /// let unbounded_string: Bound<String> = Unbounded; + /// + /// assert_eq!(unbounded_string.map(|s| s.len()), Unbounded); + /// ``` + #[inline] + #[unstable(feature = "bound_map", issue = "86026")] + pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Bound<U> { + match self { + Unbounded => Unbounded, + Included(x) => Included(f(x)), + Excluded(x) => Excluded(f(x)), + } + } } impl<T: Clone> Bound<&T> { |
