PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 14, 2022

[FIXED] How does the question mark operator convert error types?

 November 14, 2022     error-handling, rust     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing