Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a map method to Bound #83646

Merged
merged 1 commit into from
Jun 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion library/core/src/ops/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -688,13 +688,47 @@ 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),
Excluded(ref mut x) => Excluded(x),
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> {
Expand Down