Skip to content

Commit

Permalink
Simple library test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm committed May 6, 2021
1 parent ca92b5a commit 266a726
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 39 deletions.
20 changes: 7 additions & 13 deletions library/core/src/ops/try_trait.rs
Expand Up @@ -41,8 +41,7 @@ use crate::ops::ControlFlow;
/// output type that we want:
/// ```
/// # #![feature(try_trait_v2)]
/// # #![feature(try_trait_transition)]
/// # use std::ops::TryV2 as Try;
/// # use std::ops::Try;
/// fn simple_try_fold_1<A, T, R: Try<Output = A>>(
/// iter: impl Iterator<Item = T>,
/// mut accum: A,
Expand All @@ -56,9 +55,8 @@ use crate::ops::ControlFlow;
/// into the return type using [`Try::from_output`]:
/// ```
/// # #![feature(try_trait_v2)]
/// # #![feature(try_trait_transition)]
/// # #![feature(control_flow_enum)]
/// # use std::ops::{ControlFlow, TryV2 as Try};
/// # use std::ops::{ControlFlow, Try};
/// fn simple_try_fold_2<A, T, R: Try<Output = A>>(
/// iter: impl Iterator<Item = T>,
/// mut accum: A,
Expand All @@ -81,9 +79,8 @@ use crate::ops::ControlFlow;
/// recreated from their corresponding residual, so we'll just call it:
/// ```
/// # #![feature(try_trait_v2)]
/// # #![feature(try_trait_transition)]
/// # #![feature(control_flow_enum)]
/// # use std::ops::{ControlFlow, TryV2 as Try};
/// # use std::ops::{ControlFlow, Try};
/// pub fn simple_try_fold_3<A, T, R: Try<Output = A>>(
/// iter: impl Iterator<Item = T>,
/// mut accum: A,
Expand All @@ -103,10 +100,9 @@ use crate::ops::ControlFlow;
/// But this "call `branch`, then `match` on it, and `return` if it was a
/// `Break`" is exactly what happens inside the `?` operator. So rather than
/// do all this manually, we can just use `?` instead:
/// ```compile_fail (enable again once ? converts to the new trait)
/// ```
/// # #![feature(try_trait_v2)]
/// # #![feature(try_trait_transition)]
/// # use std::ops::TryV2 as Try;
/// # use std::ops::Try;
/// fn simple_try_fold<A, T, R: Try<Output = A>>(
/// iter: impl Iterator<Item = T>,
/// mut accum: A,
Expand Down Expand Up @@ -160,8 +156,7 @@ pub trait Try: FromResidual {
/// ```
/// #![feature(try_trait_v2)]
/// #![feature(control_flow_enum)]
/// #![feature(try_trait_transition)]
/// use std::ops::TryV2 as Try;
/// use std::ops::Try;
///
/// assert_eq!(<Result<_, String> as Try>::from_output(3), Ok(3));
/// assert_eq!(<Option<_> as Try>::from_output(4), Some(4));
Expand Down Expand Up @@ -193,8 +188,7 @@ pub trait Try: FromResidual {
/// ```
/// #![feature(try_trait_v2)]
/// #![feature(control_flow_enum)]
/// #![feature(try_trait_transition)]
/// use std::ops::{ControlFlow, TryV2 as Try};
/// use std::ops::{ControlFlow, Try};
///
/// assert_eq!(Ok::<_, String>(3).branch(), ControlFlow::Continue(3));
/// assert_eq!(Err::<String, _>(3).branch(), ControlFlow::Break(Err(3)));
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Expand Up @@ -45,6 +45,7 @@
#![feature(test)]
#![feature(trusted_len)]
#![feature(try_trait)]
#![feature(try_trait_v2)]
#![feature(slice_internals)]
#![feature(slice_partition_dedup)]
#![feature(int_error_matching)]
Expand Down
12 changes: 0 additions & 12 deletions library/core/tests/option.rs
Expand Up @@ -301,18 +301,6 @@ fn test_try() {
Some(val)
}
assert_eq!(try_option_none(), None);

fn try_option_ok() -> Result<u8, NoneError> {
let val = Some(1)?;
Ok(val)
}
assert_eq!(try_option_ok(), Ok(1));

fn try_option_err() -> Result<u8, NoneError> {
let val = None?;
Ok(val)
}
assert_eq!(try_option_err(), Err(NoneError));
}

#[test]
Expand Down
30 changes: 16 additions & 14 deletions library/core/tests/result.rs
Expand Up @@ -249,26 +249,14 @@ pub fn test_into_err() {

#[test]
fn test_try() {
fn try_result_some() -> Option<u8> {
let val = Ok(1)?;
Some(val)
}
assert_eq!(try_result_some(), Some(1));

fn try_result_none() -> Option<u8> {
let val = Err(NoneError)?;
Some(val)
}
assert_eq!(try_result_none(), None);

fn try_result_ok() -> Result<u8, u8> {
fn try_result_ok() -> Result<u8, u32> {
let result: Result<u8, u8> = Ok(1);
let val = result?;
Ok(val)
}
assert_eq!(try_result_ok(), Ok(1));

fn try_result_err() -> Result<u8, u8> {
fn try_result_err() -> Result<u8, u32> {
let result: Result<u8, u8> = Err(1);
let val = result?;
Ok(val)
Expand Down Expand Up @@ -401,3 +389,17 @@ fn result_opt_conversions() {

assert_eq!(res, Err(BadNumErr))
}

#[test]
#[cfg(not(bootstrap))] // Needs the V2 trait
fn result_try_trait_v2_branch() {
use core::num::NonZeroU32;
use core::ops::{ControlFlow::*, Try};
assert_eq!(Ok::<i32, i32>(4).branch(), Continue(4));
assert_eq!(Err::<i32, i32>(4).branch(), Break(Err(4)));
let one = NonZeroU32::new(1).unwrap();
assert_eq!(Ok::<(), NonZeroU32>(()).branch(), Continue(()));
assert_eq!(Err::<(), NonZeroU32>(one).branch(), Break(Err(one)));
assert_eq!(Ok::<NonZeroU32, ()>(one).branch(), Continue(one));
assert_eq!(Err::<NonZeroU32, ()>(()).branch(), Break(Err(())));
}

0 comments on commit 266a726

Please sign in to comment.