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
This commit is contained in:
parent
3aedcf06b7
commit
223c0d2a85
1 changed files with 35 additions and 1 deletions
|
@ -674,10 +674,10 @@ pub enum Bound<T> {
|
||||||
Unbounded,
|
Unbounded,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unstable(feature = "bound_as_ref", issue = "80996")]
|
|
||||||
impl<T> Bound<T> {
|
impl<T> Bound<T> {
|
||||||
/// Converts from `&Bound<T>` to `Bound<&T>`.
|
/// Converts from `&Bound<T>` to `Bound<&T>`.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[unstable(feature = "bound_as_ref", issue = "80996")]
|
||||||
pub fn as_ref(&self) -> Bound<&T> {
|
pub fn as_ref(&self) -> Bound<&T> {
|
||||||
match *self {
|
match *self {
|
||||||
Included(ref x) => Included(x),
|
Included(ref x) => Included(x),
|
||||||
|
@ -688,6 +688,7 @@ impl<T> Bound<T> {
|
||||||
|
|
||||||
/// Converts from `&mut Bound<T>` to `Bound<&T>`.
|
/// Converts from `&mut Bound<T>` to `Bound<&T>`.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[unstable(feature = "bound_as_ref", issue = "80996")]
|
||||||
pub fn as_mut(&mut self) -> Bound<&mut T> {
|
pub fn as_mut(&mut self) -> Bound<&mut T> {
|
||||||
match *self {
|
match *self {
|
||||||
Included(ref mut x) => Included(x),
|
Included(ref mut x) => Included(x),
|
||||||
|
@ -695,6 +696,39 @@ impl<T> Bound<T> {
|
||||||
Unbounded => Unbounded,
|
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> {
|
impl<T: Clone> Bound<&T> {
|
||||||
|
|
Loading…
Add table
Reference in a new issue