Skip to content

Commit

Permalink
Stabilize Option::zip
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Jun 13, 2020
1 parent f4fbb93 commit 8b20928
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/libcore/lib.rs
Expand Up @@ -145,7 +145,6 @@
#![feature(associated_type_bounds)]
#![feature(const_type_id)]
#![feature(const_caller_location)]
#![feature(option_zip)]
#![feature(no_niche)] // rust-lang/rust#68303

#[prelude_import]
Expand Down
8 changes: 5 additions & 3 deletions src/libcore/option.rs
Expand Up @@ -926,17 +926,19 @@ impl<T> Option<T> {
/// # Examples
///
/// ```
/// #![feature(option_zip)]
/// let x = Some(1);
/// let y = Some("hi");
/// let z = None::<u8>;
///
/// assert_eq!(x.zip(y), Some((1, "hi")));
/// assert_eq!(x.zip(z), None);
/// ```
#[unstable(feature = "option_zip", issue = "70086")]
#[stable(feature = "option_zip_option", since = "1.46.0")]
pub fn zip<U>(self, other: Option<U>) -> Option<(T, U)> {
self.zip_with(other, |a, b| (a, b))
match (self, other) {
(Some(a), Some(b)) => Some((a, b)),
_ => None,
}
}

/// Zips `self` and another `Option` with function `f`.
Expand Down
1 change: 0 additions & 1 deletion src/librustc_trait_selection/lib.rs
Expand Up @@ -16,7 +16,6 @@
#![feature(in_band_lifetimes)]
#![feature(crate_visibility_modifier)]
#![feature(or_patterns)]
#![feature(option_zip)]
#![recursion_limit = "512"] // For rustdoc

#[macro_use]
Expand Down
15 changes: 5 additions & 10 deletions src/tools/clippy/clippy_lints/src/checked_conversions.rs
Expand Up @@ -88,7 +88,7 @@ fn double_check<'a>(cx: &LateContext<'_, '_>, left: &'a Expr<'_>, right: &'a Exp
let upper = check_upper_bound(l);
let lower = check_lower_bound(r);

transpose(upper, lower).and_then(|(l, r)| l.combine(r, cx))
upper.zip(lower).and_then(|(l, r)| l.combine(r, cx))
};

upper_lower(left, right).or_else(|| upper_lower(right, left))
Expand Down Expand Up @@ -131,7 +131,10 @@ impl<'a> Conversion<'a> {

/// Checks if the to-type is the same (if there is a type constraint)
fn has_compatible_to_type(&self, other: &Self) -> bool {
transpose(self.to_type.as_ref(), other.to_type.as_ref()).map_or(true, |(l, r)| l == r)
match (self.to_type, other.to_type) {
(Some(l), Some(r)) => l == r,
_ => true,
}
}

/// Try to construct a new conversion if the conversion type is valid
Expand Down Expand Up @@ -322,14 +325,6 @@ fn int_ty_to_sym<'tcx>(path: &QPath<'_>) -> Option<&'tcx str> {
}
}

/// (Option<T>, Option<U>) -> Option<(T, U)>
fn transpose<T, U>(lhs: Option<T>, rhs: Option<U>) -> Option<(T, U)> {
match (lhs, rhs) {
(Some(l), Some(r)) => Some((l, r)),
_ => None,
}
}

/// Will return the expressions as if they were expr1 <= expr2
fn normalize_le_ge<'a>(op: &BinOp, left: &'a Expr<'a>, right: &'a Expr<'a>) -> Option<(&'a Expr<'a>, &'a Expr<'a>)> {
match op.node {
Expand Down

0 comments on commit 8b20928

Please sign in to comment.