Skip to content

Commit aff5c55

Browse files
authored
Rollup merge of rust-lang#72938 - lzutao:stabilize_option_zip, r=dtolnay
Stabilize Option::zip This PR stabilizes the following API: ```rust impl<T> Option<T> { pub fn zip<U>(self, other: Option<U>) -> Option<(T, U)>; } ``` This API has real world usage as seen in <https://grep.app/search?q=-%3E%20Option%3C%5C%28T%2C%5Cs%3FU%5C%29%3E&regexp=true&filter[lang][0]=Rust>. The `zip_with` method is left unstably as this API is kinda niche and it hasn't received much usage in Rust repositories on GitHub. cc rust-lang#70086
2 parents 9ec412b + 8b20928 commit aff5c55

File tree

4 files changed

+10
-15
lines changed

4 files changed

+10
-15
lines changed

src/libcore/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@
145145
#![feature(associated_type_bounds)]
146146
#![feature(const_type_id)]
147147
#![feature(const_caller_location)]
148-
#![feature(option_zip)]
149148
#![feature(no_niche)] // rust-lang/rust#68303
150149

151150
#[prelude_import]

src/libcore/option.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -926,17 +926,19 @@ impl<T> Option<T> {
926926
/// # Examples
927927
///
928928
/// ```
929-
/// #![feature(option_zip)]
930929
/// let x = Some(1);
931930
/// let y = Some("hi");
932931
/// let z = None::<u8>;
933932
///
934933
/// assert_eq!(x.zip(y), Some((1, "hi")));
935934
/// assert_eq!(x.zip(z), None);
936935
/// ```
937-
#[unstable(feature = "option_zip", issue = "70086")]
936+
#[stable(feature = "option_zip_option", since = "1.46.0")]
938937
pub fn zip<U>(self, other: Option<U>) -> Option<(T, U)> {
939-
self.zip_with(other, |a, b| (a, b))
938+
match (self, other) {
939+
(Some(a), Some(b)) => Some((a, b)),
940+
_ => None,
941+
}
940942
}
941943

942944
/// Zips `self` and another `Option` with function `f`.

src/librustc_trait_selection/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#![feature(in_band_lifetimes)]
1717
#![feature(crate_visibility_modifier)]
1818
#![feature(or_patterns)]
19-
#![feature(option_zip)]
2019
#![recursion_limit = "512"] // For rustdoc
2120

2221
#[macro_use]

src/tools/clippy/clippy_lints/src/checked_conversions.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn double_check<'a>(cx: &LateContext<'_, '_>, left: &'a Expr<'_>, right: &'a Exp
8888
let upper = check_upper_bound(l);
8989
let lower = check_lower_bound(r);
9090

91-
transpose(upper, lower).and_then(|(l, r)| l.combine(r, cx))
91+
upper.zip(lower).and_then(|(l, r)| l.combine(r, cx))
9292
};
9393

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

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

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

325-
/// (Option<T>, Option<U>) -> Option<(T, U)>
326-
fn transpose<T, U>(lhs: Option<T>, rhs: Option<U>) -> Option<(T, U)> {
327-
match (lhs, rhs) {
328-
(Some(l), Some(r)) => Some((l, r)),
329-
_ => None,
330-
}
331-
}
332-
333328
/// Will return the expressions as if they were expr1 <= expr2
334329
fn normalize_le_ge<'a>(op: &BinOp, left: &'a Expr<'a>, right: &'a Expr<'a>) -> Option<(&'a Expr<'a>, &'a Expr<'a>)> {
335330
match op.node {

0 commit comments

Comments
 (0)