Skip to content

Commit

Permalink
Add test of #[from] on optional source
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Aug 28, 2021
1 parent b087faf commit 6159aba
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/test_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ pub struct ErrorStruct {
source: io::Error,
}

#[derive(Error, Debug)]
#[error("...")]
pub struct ErrorStructOptional {
#[from]
source: Option<io::Error>,
}

#[derive(Error, Debug)]
#[error("...")]
pub struct ErrorTuple(#[from] io::Error);

#[derive(Error, Debug)]
#[error("...")]
pub struct ErrorTupleOptional(#[from] Option<io::Error>);

#[derive(Error, Debug)]
#[error("...")]
pub enum ErrorEnum {
Expand All @@ -27,6 +38,15 @@ pub enum ErrorEnum {
},
}

#[derive(Error, Debug)]
#[error("...")]
pub enum ErrorEnumOptional {
Test {
#[from]
source: Option<io::Error>,
},
}

#[derive(Error, Debug)]
#[error("...")]
pub enum Many {
Expand All @@ -39,7 +59,10 @@ fn assert_impl<T: From<io::Error>>() {}
#[test]
fn test_from() {
assert_impl::<ErrorStruct>();
assert_impl::<ErrorStructOptional>();
assert_impl::<ErrorTuple>();
assert_impl::<ErrorTupleOptional>();
assert_impl::<ErrorEnum>();
assert_impl::<ErrorEnumOptional>();
assert_impl::<Many>();
}

0 comments on commit 6159aba

Please sign in to comment.