-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Impl Try for Option #42526
Impl Try for Option #42526
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use core::option::*; | ||
|
||
fn op1() -> Result<isize, &'static str> { Ok(666) } | ||
fn op2() -> Result<isize, &'static str> { Err("sadface") } | ||
|
||
|
@@ -202,3 +204,30 @@ pub fn test_unwrap_or_default() { | |
assert_eq!(op1().unwrap_or_default(), 666); | ||
assert_eq!(op2().unwrap_or_default(), 0); | ||
} | ||
|
||
#[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> { | ||
let val = Ok(1)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also nothing new... maybe change it into a compile-fail test by using Some/None and triggering a compilation error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't see any tests for Result's Try implemenation, but if they exist I can remove these tests. |
||
Ok(val) | ||
} | ||
assert_eq!(try_result_ok(), Ok(1)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that the major concern was the error messages, I agree with @oli-obk that it would be good to add some
Here is a guide to adding ui tests: https://github.com/rust-lang/rust/blob/master/src/test/COMPILER_TESTS.md#guide-to-the-ui-tests |
||
|
||
fn try_result_err() -> Result<u8, u8> { | ||
let val = Err(1)?; | ||
Ok(val) | ||
} | ||
assert_eq!(try_result_err(), Err(1)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(try_trait)] | ||
|
||
fn main() {} | ||
|
||
fn foo() -> Result<u32, ()> { | ||
let x: Option<u32> = None; | ||
x?; | ||
Ok(22) | ||
} | ||
|
||
fn bar() -> u32 { | ||
let x: Option<u32> = None; | ||
x?; | ||
22 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error[E0277]: the trait bound `(): std::convert::From<std::option::NoneError>` is not satisfied | ||
--> $DIR/try-on-option.rs:17:5 | ||
| | ||
17 | x?; | ||
| ^^ the trait `std::convert::From<std::option::NoneError>` is not implemented for `()` | ||
| | ||
= note: required by `std::convert::From::from` | ||
|
||
error[E0277]: the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`) | ||
--> $DIR/try-on-option.rs:23:5 | ||
| | ||
23 | x?; | ||
| -- | ||
| | | ||
| cannot use the `?` operator in a function that returns `u32` | ||
| in this macro invocation | ||
| | ||
= help: the trait `std::ops::Try` is not implemented for `u32` | ||
= note: required by `std::ops::Try::from_error` | ||
|
||
error: aborting due to 2 previous errors | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we implement
std::error::Error
for this type?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this be exposed insta-stably as
<Option<()> as Try>::Error
? Would that imply it should be#[stable]
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existence of a such a type is basically exposed, but not the precise name of it, I think. So I think it's ok to mark it unstable still.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and, I think the
Try
trait is still marked as unstable, so actually that name is not stable either.