How are string io::Errors created - error-handling

Rust docs show a std::io::Error being created using
let custom_error = Error::new(ErrorKind::Other, "oh no!");
and new has a type signature of
fn new<E>(kind: ErrorKind, error: E) -> Error
where E: Into<Box<std::error::Error + Send + Sync>>
I've found an implementation of impl<T: std::error::Error> std::error::Error for Box<T> but cannot find one for &'static str like the one used in the example.
What trait is used to achieve Into<Error> for strings?

Given:
impl<'a> From<&'a str> for Box<Error> (see the From docs)
impl<T, U> Into<U> for T where U: From<T> (see the Into docs)
Substitute T = &'a str and U = Box<Error>, which gives you:
impl<'a> Into<Box<Error>> for &'a str

Related

How can I bound an associated type to work with the `?` operator?

Given the following Rust:
struct E;
trait Fallible {
type Error: Into<E>;
fn e(&self) -> Result<(), Self::Error>;
}
fn test(f: &impl Fallible) -> Result<(), E> {
Ok(f.e()?)
}
I am trying to express that the Fallible::Error type can be converted to an E and therefore should be usable with the ? operator. But, for some reason the ? is based on the From trait, which I'm not sure that it is possible to bound.
This currently fails with:
error[E0277]: `?` couldn't convert the error to `E`
--> src/lib.rs:9:13
|
9 | Ok(f.e()?)
| ^ the trait `std::convert::From<<impl Fallible as Fallible>::Error>` is not implemented for `E`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= note: required by `std::convert::From::from`
While you cannot bind at the trait level yet, you can do so on the function:
struct E;
trait Fallible {
type Error: Into<E>;
fn e(&self) -> Result<(), Self::Error>;
}
fn test<T>(f: &T) -> Result<(), E>
where
T: Faillible,
E: From<T::Error>,
{
Ok(f.e()?)
}

How do I use the Option::ok_or() method correctly?

I'm trying to understand how to use the question mark operator for error handling in Rust. I have this code:
fn main() -> Result<(), &'static str> {
let foo: i32 = Some("1")
.ok_or(Err("error 1"))?
.parse()
.or(Err("error 2"))?;
Ok(())
}
This code can not be compiled for some reason:
error[E0277]: the trait bound `&str: std::convert::From<std::result::Result<_, &str>>` is not satisfied
--> src/main.rs:2:20
|
2 | let foo: i32 = Some("1")
| ____________________^
3 | | .ok_or(Err("error 1"))?
| |_______________________________^ the trait `std::convert::From<std::result::Result<_, &str>>` is not implemented for `&str`
|
= note: required by `std::convert::From::from`
The Rust book has an example usage of the question mark operator:
use std::io;
use std::io::Read;
use std::fs::File;
fn read_username_from_file() -> Result<String, io::Error> {
let mut s = String::new();
File::open("hello.txt")?.read_to_string(&mut s)?;
Ok(s)
}
In my opinion, it doesn't differ much from my example in sense of handling errors. I cannot see a reason for my code to be invalid. If the From trait should be implemented for all kinds of Result why does the code from the Rust book work fine?
Unlike or, ok_or takes an E, not a full Result<T, E> (because it wouldn't have anything to do if passed an Ok). Just pass the error string directly:
fn main() -> Result<(), &'static str> {
let foo: i32 = Some("1")
.ok_or("error 1")?
.parse()
.or(Err("error 2"))?;
Ok(())
}
The reason the error message mentions the From trait is because ? implicitly uses From to convert the expression's error type into the return value's error type. If it worked, .ok_or(Err("error 1")) would return a value of Result<&'static str, Result<_, &'static str>> (_ could be almost anything, since Err doesn't specify). The ? operator attempts to find an implementation of From that would convert Result<_, &'static str> (the expression's error type) into &'static str (the return value's error type). Since no such From implementation exists, the compiler emits an error.

What does Rhs refer to in a compiler error message about PartialEq?

I am trying to create a function that takes two iterators as as parameters and iterates over the items by reference. Each Iterator item should implement PartialEq.
My first attempt was:
fn compute<T: Iterator>(first: T, second: T, len: usize) -> usize
where
T::Item: std::cmp::PartialEq,
{
// ...
}
This compiles but iterates (as far as I understand) not by reference but by value and the compiler complains about a move when iterating.
My second attempt was something like:
fn compute<'a, T>(first: T, second: T, len: usize) -> usize
where
T: Iterator<Item = &'a std::cmp::PartialEq>,
{
//...
}
resulting in a compiler error:
error[E0393]: the type parameter `Rhs` must be explicitly specified
--> src/main.rs:3:28
|
3 | T: Iterator<Item = &'a std::cmp::PartialEq>,
| ^^^^^^^^^^^^^^^^^^^ missing reference to `Rhs`
|
= note: because of the default `Self` reference, type parameters must be specified on object types
What does the Rhs (Right hand side?) the compiler refers to here mean? Why do I need a reference to it? How do I pass a bounded reference-based Iterator into a function?
PartialEq is a trait that allows you to compare two values. Those two values do not have to be of the same type! The generic type Rhs is used to specify what type we are comparing with. By default, the value of Rhs is the same as the type that is being compared to:
pub trait PartialEq<Rhs = Self>
where
Rhs: ?Sized,
In this case, you are actually requesting that the iterator value be the trait object &PartialEq. As the error message states:
because of the default Self reference, type parameters must be specified on object types
We could specify it:
fn compute<'a, T>(first: T, second: T, len: usize) -> usize
where
T: Iterator<Item = &'a std::cmp::PartialEq<i32>>,
or
fn compute<'a, T: 'a>(first: T, second: T, len: usize) -> usize
where
T: Iterator<Item = &'a std::cmp::PartialEq<&'a T>>,
but iterates (as far as I understand) not by reference but by value
It's quite possible for it to iterate by reference. Remember that T is any type and that i32, &i32, and &mut i32 are all types. Your first example is the formulation of the signature I would use:
fn compute<T: Iterator>(first: T, second: T, len: usize) -> usize
where
T::Item: std::cmp::PartialEq,
{
42
}
fn main() {
let a = [1, 2, 3];
let b = [4, 5, 6];
compute(a.iter(), b.iter(), 1);
compute(a.iter(), b.iter(), 2);
compute(a.iter(), b.iter(), 3);
}

Figuring out return type of closure

I'm having troubles figuring out the type signature of the fn filter function in following example.
The Node and Descendant definition is just there for syntax . It's not meant to do anything!
use std::iter::Filter;
#[derive(Clone)]
pub struct Node<'a> {
s: &'a str,
}
pub struct Descendants<'a>{
iter: Node<'a>
}
impl<'a> Iterator for Descendants<'a> {
type Item = Node<'a>;
fn next(&mut self) -> Option<Node<'a>> {
Some(Node {s: self.iter.s})
}
}
impl<'a> Node<'a> {
pub fn descendants(&self) -> Descendants<'a> {
Descendants{ iter: Node{s: self.s} }
}
pub fn filter(&self, criteria: &str) -> Filter<Descendants<'a>, fn(&'a Node<'a>)->bool > {
self.descendants()
.filter(|node| node.s == "meh")
}
}
fn main() {
let doc = Node{s: "str"};
}
(Playground link)
The error I get is following:
<anon>:27:28: 27:34 error: the type of this value must be known in this context
<anon>:27 .filter(|node| node.s == "meh")
^~~~~~
<anon>:27:21: 27:43 error: mismatched types:
expected `fn(&Node<'_>) -> bool`,
found `[closure <anon>:27:21: 27:43]`
(expected fn pointer,
found closure) [E0308]
<anon>:27 .filter(|node| node.s == "meh")
^~~~~~~~~~~~~~~~~~~~~~
<anon>:27:14: 27:44 error: type mismatch: the type `fn(&Node<'_>) -> bool` implements the trait `core::ops::FnMut<(&Node<'_>,)>`, but the trait `for<'r> core::ops::FnMut<(&'r Node<'_>,)>` is required (expected concrete lifetime, found bound lifetime parameter ) [E0281]
<anon>:27 .filter(|node| node.s == "meh")
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:27:14: 27:44 error: type mismatch resolving `for<'r> <fn(&Node<'_>) -> bool as core::ops::FnOnce<(&'r Node<'_>,)>>::Output == bool`:
expected bound lifetime parameter ,
found concrete lifetime [E0271]
<anon>:27 .filter(|node| node.s == "meh")
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 4 previous errors
playpen: application terminated with error code 101
When I as per this question Correct way to return an Iterator? I tried to replace pub fn filter(&self, criteria: &str) -> Filter<Descendants<'a>, fn(&'a Node<'a>)->bool > with pub fn filter(&self, criteria: &str) -> () I get
<anon>:26:9: 27:44 error: mismatched types:
expected `()`,
found `core::iter::Filter<Descendants<'_>, [closure <anon>:27:21: 27:43]>`
What I'm supposed to replace closure with?
Alternatively, if it's too hard and finicky to return a Filter, how do I write the Wrapper for fn filter() return type?
I clearly remember that this was answered before a few times (I even wrote about it in an answer a few minutes before) but I can't find a link now, so here it goes.
The problem with your code is that you use a closure as filter() argument:
.filter(|node| node.s == "meh")
Unboxed closures in Rust are implemented as anonymous types which, naturally, can't be named, so there is no way to write a signature of a function which returns an iterator which uses a closure. That's what the error message you're getting is about:
expected `fn(&Node<'_>) -> bool`,
found `[closure <anon>:27:21: 27:43]`
(expected fn pointer,
found closure) [E0308]
There are several ways around this, one of them is to use trait objects:
pub fn filter<'b>(&'b self, criteria: &'b str) -> Box<Iterator<Item=Node<'a>+'b>>
{
Box::new(self.descendants().filter(move |node| node.s == criteria))
}
Given that your closure has a non-empty environment, this is the only way for your code to work. If your closure didn't capture anything, you could use a static function whose type can be written out:
pub fn filter(&self) -> Filter<Descendants<'a>, fn(&Node<'a>) -> bool> {
fn filter_fn<'b>(node: &Node<'b>) -> bool {
node.s == "meh"
}
self.descendants().filter(filter_fn)
}

Implementing a "cautious" take_while using Peekable

I'd like to use Peekable as the basis for a new cautious_take_while operation that acts like take_while from IteratorExt but without consuming the first failed item. (There's a side question of whether this is a good idea, and whether there are better ways to accomplish this goal in Rust -- I'd be happy for hints in that direction, but mostly I'm trying to understand where my code is breaking).
The API I'm trying to enable is basically:
let mut chars = "abcdefg.".chars().peekable();
let abc : String = chars.by_ref().cautious_take_while(|&x| x != 'd');
let defg : String = chars.by_ref().cautious_take_while(|&x| x != '.');
// yielding (abc = "abc", defg = "defg")
I've taken a crack at creating a MCVE here, but I'm getting:
:10:5: 10:19 error: cannot move out of borrowed content
:10 chars.by_ref().cautious_take_while(|&x| x != '.');
As far as I can tell, I'm following the same pattern as Rust's own TakeWhile in terms of my function signatures, but I'm seeing different different behavior from the borrow checker. Can someone point out what I'm doing wrong?
The funny thing with by_ref() is that it returns a mutable reference to itself:
pub trait IteratorExt: Iterator + Sized {
fn by_ref(&mut self) -> &mut Self { self }
}
It works because the Iterator trait is implemented for the mutable pointer to Iterator type. Smart!
impl<'a, I> Iterator for &'a mut I where I: Iterator, I: ?Sized { ... }
The standard take_while function works because it uses the trait Iterator, that is automatically resolved to &mut Peekable<T>.
But your code does not work because Peekable is a struct, not a trait, so your CautiousTakeWhileable must specify the type, and you are trying to take ownership of it, but you cannot, because you have a mutable pointer.
Solution, do not take a Peekable<T> but &mut Peekable<T>. You will need to specify the lifetime too:
impl <'a, T: Iterator, P> Iterator for CautiousTakeWhile<&'a mut Peekable<T>, P>
where P: FnMut(&T::Item) -> bool {
//...
}
impl <'a, T: Iterator> CautiousTakeWhileable for &'a mut Peekable<T> {
fn cautious_take_while<P>(self, f: P) -> CautiousTakeWhile<&'a mut Peekable<T>, P>
where P: FnMut(&T::Item) -> bool {
CautiousTakeWhile{inner: self, condition: f,}
}
}
A curious side effect of this solution is that now by_ref is not needed, because cautious_take_while() takes a mutable reference, so it does not steal ownership. The by_ref() call is needed for take_while() because it can take either Peekable<T> or &mut Peekable<T>, and it defaults to the first one. With the by_ref() call it will resolve to the second one.
And now that I finally understand it, I think it might be a good idea to change the definition of struct CautiousTakeWhile to include the peekable bit into the struct itself. The difficulty is that the lifetime has to be specified manually, if I'm right. Something like:
struct CautiousTakeWhile<'a, T: Iterator + 'a, P>
where T::Item : 'a {
inner: &'a mut Peekable<T>,
condition: P,
}
trait CautiousTakeWhileable<'a, T>: Iterator {
fn cautious_take_while<P>(self, P) -> CautiousTakeWhile<'a, T, P> where
P: FnMut(&Self::Item) -> bool;
}
and the rest is more or less straightforward.
This was a tricky one! I'll lead with the meat of the code, then attempt to explain it (if I understand it...). It's also the ugly, unsugared version, as I wanted to reduce incidental complexity.
use std::iter::Peekable;
fn main() {
let mut chars = "abcdefg.".chars().peekable();
let abc: String = CautiousTakeWhile{inner: chars.by_ref(), condition: |&x| x != 'd'}.collect();
let defg: String = CautiousTakeWhile{inner: chars.by_ref(), condition: |&x| x != '.'}.collect();
println!("{}, {}", abc, defg);
}
struct CautiousTakeWhile<'a, I, P> //'
where I::Item: 'a, //'
I: Iterator + 'a, //'
P: FnMut(&I::Item) -> bool,
{
inner: &'a mut Peekable<I>, //'
condition: P,
}
impl<'a, I, P> Iterator for CautiousTakeWhile<'a, I, P>
where I::Item: 'a, //'
I: Iterator + 'a, //'
P: FnMut(&I::Item) -> bool
{
type Item = I::Item;
fn next(&mut self) -> Option<I::Item> {
let return_next =
match self.inner.peek() {
Some(ref v) => (self.condition)(v),
_ => false,
};
if return_next { self.inner.next() } else { None }
}
}
Actually, Rodrigo seems to have a good explanation, so I'll defer to that, unless you'd like me to explain something specific.