Issue
This doesn't compile:
fn foo() -> std::io::Result<()> {
todo!()
}
pub fn bar() -> anyhow::Result<()> {
foo()
}
Nor does this:
pub fn bar() -> anyhow::Result<()> {
foo().into()
}
But this does:
pub fn bar() -> anyhow::Result<()> {
Ok(foo()?)
}
What is the mechanism behind the conversion that ?
does, and is there any better way to do this conversion than Ok(...?)
?
Solution
It is something like:
// r?
match r {
Ok(v)=>v,
Err(x)=>return Err(std::convert::Into::into(x)),
}
So your last code is something like this:
pub fn bar() -> anyhow::Result<()> {
Ok(match foo(){
Ok(x)=>x,
Err(x)=>return Err(std::convert::Into::into(x)),
})
}
Or this:
pub fn bar() -> anyhow::Result<()> {
let r: () = match foo(){
Ok(x)=>x,
Err(x)=>return Err(std::convert::Into::into(x)),
};
Ok(r)
}
UPD
As @Masklinn said, it uses From trait instead Into and Try trait too. You can learn more here. It is quite surprising because documentation for trait Into says:
Prefer using Into over From when specifying trait bounds on a generic function to ensure that types that only implement Into can be used as well.
Finally, result code would look like this:
pub fn bar() -> anyhow::Result<()> {
let r: () = match Try::into_result(foo()){
Ok(x)=>x,
Err(e)=>return Try::from_error(From::from(e)),
};
Ok(r)
}
Answered By - Angelicos Phosphoros Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.