mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
moved simple test to coretests, introduced more fleshed out doctests for break_ok/continue_ok
This commit is contained in:
@@ -197,8 +197,60 @@ pub fn break_value(self) -> Option<B> {
|
||||
///
|
||||
/// use std::ops::ControlFlow;
|
||||
///
|
||||
/// assert_eq!(ControlFlow::<&str, i32>::Break("Stop right there!").break_ok(), Ok("Stop right there!"));
|
||||
/// assert_eq!(ControlFlow::<&str, i32>::Continue(3).break_ok(), Err(3));
|
||||
/// struct TreeNode<T> {
|
||||
/// value: T,
|
||||
/// left: Option<Box<TreeNode<T>>>,
|
||||
/// right: Option<Box<TreeNode<T>>>,
|
||||
/// }
|
||||
///
|
||||
/// impl<T> TreeNode<T> {
|
||||
/// fn find<'a>(&'a self, mut predicate: impl FnMut(&T) -> bool) -> Result<&'a T, ()> {
|
||||
/// let mut f = |t: &'a T| -> ControlFlow<&'a T> {
|
||||
/// if predicate(t) {
|
||||
/// ControlFlow::Break(t)
|
||||
/// } else {
|
||||
/// ControlFlow::Continue(())
|
||||
/// }
|
||||
/// };
|
||||
///
|
||||
/// self.traverse_inorder(&mut f).break_ok()
|
||||
/// }
|
||||
///
|
||||
/// fn traverse_inorder<'a, B>(
|
||||
/// &'a self,
|
||||
/// f: &mut impl FnMut(&'a T) -> ControlFlow<B>,
|
||||
/// ) -> ControlFlow<B> {
|
||||
/// if let Some(left) = &self.left {
|
||||
/// left.traverse_inorder(f)?;
|
||||
/// }
|
||||
/// f(&self.value)?;
|
||||
/// if let Some(right) = &self.right {
|
||||
/// right.traverse_inorder(f)?;
|
||||
/// }
|
||||
/// ControlFlow::Continue(())
|
||||
/// }
|
||||
///
|
||||
/// fn leaf(value: T) -> Option<Box<TreeNode<T>>> {
|
||||
/// Some(Box::new(Self {
|
||||
/// value,
|
||||
/// left: None,
|
||||
/// right: None,
|
||||
/// }))
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// let node = TreeNode {
|
||||
/// value: 0,
|
||||
/// left: TreeNode::leaf(1),
|
||||
/// right: Some(Box::new(TreeNode {
|
||||
/// value: -1,
|
||||
/// left: TreeNode::leaf(5),
|
||||
/// right: TreeNode::leaf(2),
|
||||
/// })),
|
||||
/// };
|
||||
///
|
||||
/// let res = node.find(|val: &i32| *val > 3);
|
||||
/// assert_eq!(res, Ok(&5));
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "control_flow_ok", issue = "140266")]
|
||||
@@ -250,8 +302,59 @@ pub fn continue_value(self) -> Option<C> {
|
||||
///
|
||||
/// use std::ops::ControlFlow;
|
||||
///
|
||||
/// assert_eq!(ControlFlow::<&str, i32>::Break("Stop right there!").continue_ok(), Err("Stop right there!"));
|
||||
/// assert_eq!(ControlFlow::<&str, i32>::Continue(3).continue_ok(), Ok(3));
|
||||
/// struct TreeNode<T> {
|
||||
/// value: T,
|
||||
/// left: Option<Box<TreeNode<T>>>,
|
||||
/// right: Option<Box<TreeNode<T>>>,
|
||||
/// }
|
||||
///
|
||||
/// impl<T> TreeNode<T> {
|
||||
/// fn validate<B>(&self, f: &mut impl FnMut(&T) -> ControlFlow<B>) -> Result<(), B> {
|
||||
/// self.traverse_inorder(f).continue_ok()
|
||||
/// }
|
||||
///
|
||||
/// fn traverse_inorder<B>(&self, f: &mut impl FnMut(&T) -> ControlFlow<B>) -> ControlFlow<B> {
|
||||
/// if let Some(left) = &self.left {
|
||||
/// left.traverse_inorder(f)?;
|
||||
/// }
|
||||
/// f(&self.value)?;
|
||||
/// if let Some(right) = &self.right {
|
||||
/// right.traverse_inorder(f)?;
|
||||
/// }
|
||||
/// ControlFlow::Continue(())
|
||||
/// }
|
||||
///
|
||||
/// fn leaf(value: T) -> Option<Box<TreeNode<T>>> {
|
||||
/// Some(Box::new(Self {
|
||||
/// value,
|
||||
/// left: None,
|
||||
/// right: None,
|
||||
/// }))
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// let node = TreeNode {
|
||||
/// value: 0,
|
||||
/// left: TreeNode::leaf(1),
|
||||
/// right: Some(Box::new(TreeNode {
|
||||
/// value: -1,
|
||||
/// left: TreeNode::leaf(5),
|
||||
/// right: TreeNode::leaf(2),
|
||||
/// })),
|
||||
/// };
|
||||
///
|
||||
/// let res = node.validate(&mut |val| {
|
||||
/// if *val < 0 {
|
||||
/// return ControlFlow::Break("negative value detected");
|
||||
/// }
|
||||
///
|
||||
/// if *val > 4 {
|
||||
/// return ControlFlow::Break("too big value detected");
|
||||
/// }
|
||||
///
|
||||
/// ControlFlow::Continue(())
|
||||
/// });
|
||||
/// assert_eq!(res, Err("too big value detected"));
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "control_flow_ok", issue = "140266")]
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#![feature(const_ref_cell)]
|
||||
#![feature(const_result_trait_fn)]
|
||||
#![feature(const_trait_impl)]
|
||||
#![feature(control_flow_ok)]
|
||||
#![feature(core_float_math)]
|
||||
#![feature(core_intrinsics)]
|
||||
#![feature(core_intrinsics_fallbacks)]
|
||||
|
||||
@@ -16,3 +16,15 @@ fn control_flow_discriminants_match_result() {
|
||||
discriminant_value(&Result::<i32, i32>::Ok(3)),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn control_flow_break_ok() {
|
||||
assert_eq!(ControlFlow::<char, i32>::Break('b').break_ok(), Ok('b'));
|
||||
assert_eq!(ControlFlow::<char, i32>::Continue(3).break_ok(), Err(3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn control_flow_continue_ok() {
|
||||
assert_eq!(ControlFlow::<char, i32>::Break('b').continue_ok(), Err('b'));
|
||||
assert_eq!(ControlFlow::<char, i32>::Continue(3).continue_ok(), Ok(3));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user