Match on either an error or an empty result - error-handling

I have a function that returns Result<Vec<&str>, String> for a list of nodes. My intention is to check for an error or an empty vector to return early, or continue if there there is a list.
This is what I am trying, among other things, but the compiler complains about the type of x.
let nodes = list_nodes(client, &service);
match nodes {
Err(e) => {
println!("Unable to list nodes: {:?}", e);
return;
},
Ok(x) if x.as_slice() == [] => {
println!("No nodes found for service: {}", service);
return;
}
_ => {}
}
The error is:
error[E0282]: type annotations needed
--> src/main.rs:28:18
|
28 | Ok(x) if x.as_slice() == [] => {
| ^^^^^^^^^^^^^^^^^^ cannot infer type for `A`

The problem is actually that it can't infer the type of []. The type-checker cannot assume that [] here has the same type as x.as_slice() because the PartialEq trait (where == comes from) allows instances where the right hand side is of a different type to the left. You can easily solve it by looking at the slice's length instead, or checking if the slice is empty with is_empty():
match nodes {
Err(e) => {
println!("Unable to list nodes: {:?}", e);
return;
},
Ok(ref x) if x.as_slice().is_empty() => {
println!("No nodes found for service: {}", service);
return;
}
_ => {}
}
Also, taking a reference to x (with ref x like I've done above) will prevent another error that you're likely to get, avoiding moving x when it is still owned by nodes.

Related

How can I use rust Try trait with Option NoneError?

I've written a custom protocol where I've defined my own struct for a frame and it parses from bytes. My function accepts a Vec and parses the elements accordingly. To account for invalid frames, I am returning a Result<Frame> and calling .get() on the byte array. Here's my code:
fn main(){
let emptyvec = Vec::new();
match Frame::from_bytes(emptyvec) {
Err(e) => {
println!("Received invalid frame");
},
Ok(frame) => {
println!("Received valid frame");
}
}
}
struct Frame {
txflag: u8, // indicates if chunked
msgtype: u8, // a flag for message type
sender: u8, // which node ID sent this frame?
routeoffset: u8, // size of array of route for frame
route: Vec<u8>, // a list of node IDs that frame should pass
payload: Vec<u8>, // payload data
}
impl Frame {
/// parse from raw bytes
pub fn from_bytes(bytes: &Vec<u8>) -> std::io::Result<Self> {
let txflag = bytes.get(0)?.clone();
let msgtype = bytes.get(1)?.clone();
let sender = bytes.get(2)?.clone();
let routesoffset = bytes.get(3)?.clone();
let routes = &bytes.get(4..(4+routesoffset as usize))?;
let (left, right) = bytes.split_at(2);
let data = Vec::from(right);
Ok(Frame {
txflag,
msgtype,
sender,
routeoffset: routesoffset,
route: Vec::from(routes),
payload: data
})
}
}
However when I try to use this pattern I get the following compilation error, and when attempting to implement the trait I get an error that the Try trait is unstable.
error[E0277]: `?` couldn't convert the error to `std::io::Error`
--> src/stack/frame.rs:121:34
|
121 | let txflag = bytes.get(0)?.clone();
| ^ the trait `std::convert::From<std::option::NoneError>` is not implemented for `std::io::Error`
Not quite sure how to proceed but I'd like to use stable features to solve this. The goal here is to be able to parse bytes and handle an invalid frame as necessary.
This is probably what you want
use std::io::{Error, ErrorKind};
fn main() {
let emptyvec = Vec::new();
match Frame::from_bytes(&emptyvec) {
Err(e) => {
println!("Received invalid frame");
}
Ok(frame) => {
println!("Received valid frame");
}
}
}
struct Frame {
txflag: u8,
// indicates if chunked
msgtype: u8,
// a flag for message type
sender: u8,
// which node ID sent this frame?
routeoffset: u8,
// size of array of route for frame
route: Vec<u8>,
// a list of node IDs that frame should pass
payload: Vec<u8>, // payload data
}
impl Frame {
/// parse from raw bytes
pub fn from_bytes(bytes: &Vec<u8>) -> std::io::Result<Self> {
let txflag = bytes.get(0).ok_or(Error::from(ErrorKind::InvalidData))?.clone();
let msgtype = bytes.get(1).ok_or(Error::from(ErrorKind::InvalidData))?.clone();
let sender = bytes.get(2).ok_or(Error::from(ErrorKind::InvalidData))?.clone();
let routesoffset = bytes.get(3).ok_or(Error::from(ErrorKind::InvalidData))?.clone();
let routes = bytes
.get(4..(4 + routesoffset as usize))
.ok_or(Error::from(ErrorKind::InvalidData))?
.clone();
let (_, right) = bytes.split_at(2);
let data = Vec::from(right);
Ok(Frame {
txflag,
msgtype,
sender,
routeoffset: routesoffset,
route: Vec::from(routes),
payload: data,
})
}
}
Here is Rust Playground
You are trying to call ? on Option. You have to convert Option to Result (If you still want to use ?).
I want to add to what Đorðe Zeljić said:
As he already pointed out the result of bytes.get(0) is a std::option::Option. When you use the ? operator on that you already left the grounds of stable Rust. This application is only supported in unstable Rust at the moment.
If you want to stay in stable Rust, it's probably best to do what Đorðe wrote. If you want to keep using the ? operator because it produces nicer looking code, here is what's going on:
Rust has a lot of error types, each being only able to represent what they are made for. If you are using a std::io::Result this implicitly uses the error type std::io::Error which is only able to represent typical I/O errors. This type is not able to represent “there was no value when I expected one”. That's why from applying ? to a Option with the None value, you don't get a std::io::Error but a different kind of error: std::option::NoneError.
When your Rust application grows it will happen often, that you have to return a Result that can contain different types of errors. In that case you normally define your own error type (enum), that can represent different kinds of errors. Then for each error, that can be contained, you have to define the From trait on your own enum. This can be a lot of repeated work, so there is a macro in the quick-error crate, that helps with that and implements the From trait automatically for each error that can be contained.
To get your code compiling, you could define the following error enum, that can represent std::io::Error as well as std::option::NoneError:
quick_error! {
#[derive(Debug)]
pub enum FrameError {
IoError(err: std::io::Error) {from() cause(err)}
MissingValue(err: std::option::NoneError) {from()}
}
}
Instead of std::io::Result<Self> your from_bytes function then has to return a std::result::Result that uses your new error type: Result<Self, FrameError>.
Completely assembled that looks like this:
#![feature(try_trait)]
use quick_error::*;
quick_error! {
#[derive(Debug)]
pub enum FrameError {
IoError(err: std::io::Error) {from() cause(err)}
MissingValue(err: std::option::NoneError) {from()}
}
}
fn main() {
let emptyvec = Vec::new();
match Frame::from_bytes(&emptyvec) {
Err(_e) => {
println!("Received invalid frame");
}
Ok(_frame) => {
println!("Received valid frame");
}
}
}
struct Frame {
txflag: u8, // indicates if chunked
msgtype: u8, // a flag for message type
sender: u8, // which node ID sent this frame?
routeoffset: u8, // size of array of route for frame
route: Vec<u8>, // a list of node IDs that frame should pass
payload: Vec<u8>, // payload data
}
impl Frame {
/// parse from raw bytes
pub fn from_bytes(bytes: &Vec<u8>) -> Result<Self, FrameError> {
let txflag = bytes.get(0)?.clone();
let msgtype = bytes.get(1)?.clone();
let sender = bytes.get(2)?.clone();
let routesoffset = bytes.get(3)?.clone();
let routes = bytes.get(4..(4 + routesoffset as usize))?;
let (left, right) = bytes.split_at(2);
let data = Vec::from(right);
Ok(Frame {
txflag,
msgtype,
sender,
routeoffset: routesoffset,
route: Vec::from(routes),
payload: data,
})
}
}
To use the quick-error crate, you have to add the following to your Cargo.toml:
[dependencies]
quick-error = "1.2.3"

How to retrieve the underlying error from a Failure Error?

When trying to open a broken epub/ZIP file with epub-rs, the zip-rs crate error (which doesn't use Failure) is wrapped into a failure::Error by epub-rs. I want to handle each error type of zip-rs with an distinct error handler and need a way to match against the underlying error. How can I retrieve it from Failure?
fn main() {
match epub::doc::EpubDoc::new("a.epub") {
Ok(epub) => // do something with the epub
Err(error) => {
// handle errors
}
}
}
error.downcast::<zip::result::ZipError>() fails and error.downcast_ref() returns None.
You can downcast from a Failure Error into another type that implements Fail by using one of three functions:
downcast
downcast_ref
downcast_mut
use failure; // 0.1.5
use std::{fs, io};
fn generate() -> Result<(), failure::Error> {
fs::read_to_string("/this/does/not/exist")?;
Ok(())
}
fn main() {
match generate() {
Ok(_) => panic!("Should have an error"),
Err(e) => match e.downcast_ref::<io::Error>() {
Some(e) => println!("Got an io::Error: {}", e),
None => panic!("Could not downcast"),
},
}
}
For your specific case, I'm guessing that you are either running into mismatched dependency versions (see Why is a trait not implemented for a type that clearly has it implemented? for examples and techniques on how to track this down) or that you simply are getting the wrong error type. For example, a missing file is actually an std::io::Error:
// epub = "1.2.0"
// zip = "0.4.2"
// failure = "0.1.5"
use std::io;
fn main() {
if let Err(error) = epub::doc::EpubDoc::new("a.epub") {
match error.downcast_ref::<io::Error>() {
Some(i) => println!("IO error: {}", i),
None => {
panic!("Other error: {} {:?}", error, error);
}
}
}
}

Is there a way to remove unwrapping of items in an iterator chain that filters and partitions based on a Result?

I created this example code:
fn main() {
let books = vec![
Book {
data: Ok("type1".to_owned()),
metadata: "meta1".to_owned(),
},
Book {
data: Err("-".to_owned()),
metadata: "meta2".to_owned(),
},
Book {
data: Ok("type2".to_owned()),
metadata: "meta2".to_owned(),
},
];
// metadata without data being error
let (book_type_1, book_type_2): &(Vec<_>, Vec<_>) = &books
.iter()
.filter(|f| f.data.is_ok())
.partition(|p| p.data.as_ref().unwrap() == "type1");
println!("Books {:?}", books);
println!("Type 1 {:?}", book_type_1); // Should be the original Vec<Book> with Type 1 filtered.
println!("Type 2 {:?}", book_type_2); // Should be the original Vec<Book> with Type 2 filtered.
}
#[derive(Debug)]
struct Book {
data: Result<String, String>,
metadata: String,
}
On the let (book_type_1, book_type_2) expression, I need to use Book::data twice, but I already filtered it so I know it can't be Err. Is there a way to restructure to remove the use of unwrap here?
I'm not sure exactly what you mean, but it seems like you want to use Iterator::filter_map(). It lets you filter for values that are Some(T), which then get passed on as unwrapped Ts.
So what you can do is convert your Results to Options with Result::ok(), so a Result::Ok(T) will become Some(T) which means it passes the filter as T.
fn main() {
let books = vec![
Book {
data: Ok("type1".to_owned()),
metadata: "meta1".to_owned(),
},
Book {
data: Err("-".to_owned()),
metadata: "meta2".to_owned(),
},
Book {
data: Ok("type2".to_owned()),
metadata: "meta2".to_owned(),
},
];
// metadata without data being error
let (book_type_1, book_type_2): (Vec<_>, Vec<_>) = books
.iter()
.filter_map(|f| {
match &f.data {
Ok(data) => Some((f, data)),
Err(_) => None,
}
})
.partition(|(book, data)| *data == "type1");
println!("Books {:?}", books);
println!("Type 1 {:?}", book_type_1);
println!("Type 2 {:?}", book_type_2);
}
#[derive(Debug)]
struct Book {
data: Result<String, String>,
metadata: String,
}
playground
I removed the unnecessary reference to the returned partitioned tuple.
Also note that None pertains to Option<T>, but you're using Result<T, E>. I think you knew that but just making sure.
I would use flat_map as described in the other answer, but I'd leave the yielded values as a Result. Result implements IntoIterator, so you can use it directly in flat_map.
I'd also use Result::as_ref instead of writing out the match explicitly.
I'd then use Itertools::partition_map to simultaneously select between the types and remove the extra property:
extern crate itertools;
use itertools::{Either, Itertools};
// ...
let (book_type_1, book_type_2): (Vec<_>, Vec<_>) = books
.iter()
.flat_map(|b| b.data.as_ref().map(|data| (b, data)))
.partition_map(|(b, data)| {
if data == "type1" {
Either::Left(b)
} else {
Either::Right(b)
}
});
Note:
There's no reason to take a reference to the result tuple.
This only works because you are iterating on references to books.
If you needed to operate on the owned Books, I'd move the comparison into the flat_map call and pass it along:
let (book_type_1, book_type_2): (Vec<_>, Vec<_>) = books
.into_iter()
.flat_map(|book| {
book.data
.as_ref()
.ok()
.map(|data| data == "type1")
.map(|is_type1| (is_type1, book))
})
.partition_map(|(is_type1, book)| {
if is_type1 {
Either::Left(book)
} else {
Either::Right(book)
}
});
There's also the pragmatic solution of sorting all errors into the same bin as one of the types. Since you know that there won't be any errors, this has no effect:
let (book_type_1, book_type_2): (Vec<_>, Vec<_>) = books
.iter()
.filter(|f| f.data.is_ok())
.partition(|p| p.data.as_ref().ok().map_or(false, |d| d == "type1"));
See also:
What's the most idiomatic way of working with an Iterator of Results?

How to map on a vec and use a closure with pattern matching in Rust

I'd like to use map to iterate over an array and do stuff per item and get rid of the for loop. An error which I do not understand blocks my attempt. What I want to achieve is to iterate through a vector of i32 and match on them to concat a string with string literals and then return it at the end.
Function:
pub fn convert_to_rainspeak(prime_factors: Vec<i32>) -> String {
let mut speak = String::new();
prime_factors.iter().map(|&factor| {
match factor {
3 => { speak.push_str("Pling"); },
5 => { speak.push_str("Plang"); },
7 => { speak.push_str("Plong"); },
_ => {}
}
}).collect();
speak
}
fn main() {}
Output:
error[E0282]: type annotations needed
--> src/main.rs:10:8
|
10 | }).collect();
| ^^^^^^^ cannot infer type for `B`
Iterator::collect is defined as:
fn collect<B>(self) -> B
where
B: FromIterator<Self::Item>
That is, it returns a type that is up to the caller. However, you have completely disregarded the output, so there's no way for it to infer a type. The code misuses collect when it basically wants to use for.
In your "fixed" version (which has since been edited, making this paragraph make no sense), you are being very inefficient by allocating a string in every iteration. Plus you don't need to specify any explicit types other than those on the function, and you should accept a &[i32] instead:
fn convert_to_rainspeak(prime_factors: &[i32]) -> String {
prime_factors.iter()
.map(|&factor| {
match factor {
3 => "Pling",
5 => "Plang",
7 => "Plong",
_ => "",
}
})
.collect()
}
fn main() {
println!("{}", convert_to_rainspeak(&[1, 2, 3, 4, 5]));
}
The error in this case is that the compiler can't figure out what type you are collecting into. If you add a type annotation to collect, then it will work:
pub fn convert_to_rainspeak(prime_factors:Vec<i32>) -> String{
let mut speak = String::new();
prime_factors.iter().map(| &factor| {
match factor {
3 => {speak.push_str("Pling");},
5 => {speak.push_str("Plang");},
7 => {speak.push_str("Plong");},
_ => {}
}
}).collect::<Vec<_>>();
speak
}
However, this is really not the idiomatic way to do this. You should use for instead:
pub fn convert_to_rainspeak(prime_factors:Vec<i32>) -> String {
let mut speak = String::new();
for factor in prime_factors.iter() {
match *factor {
3 => speak.push_str("Pling"),
5 => speak.push_str("Plang"),
7 => speak.push_str("Plong"),
_ => {}
}
}
speak
}
You could also use flat_map() instead of map().
This way you can map to Option and return None instead of empty string if there is no corresponding value.
fn convert_to_rainspeak(prime_factors: &[i32]) -> String {
prime_factors
.iter()
.flat_map(|&factor| match factor {
3 => Some("Pling"),
5 => Some("Plang"),
7 => Some("Plong"),
_ => None,
})
.collect()
}
fn main() {
println!("{}", convert_to_rainspeak(&[1, 2, 3, 7]));
}
few minutes later I solved it myself (any upgrade appreciated):
pub fn convert_to_rainspeak(prime_factors:Vec<i32>) -> String{
let mut speak:String = prime_factors.iter().map(|&factor| {
match factor {
3 => {"Pling"},
5 => {"Plang"},
7 => {"Plong"},
_ => {""}
}
}).collect();
speak
}
The issue was that I was not aware that .collect() is awaiting the result of map. I then assigned prime_factors.iter()... to a string and rearranged var bindings so it now all works.
EDIT: refactored redundant assignments to the speak vector

How can I match on a specific io::Error type?

I am trying to read in a file until the end 2 bytes at a time and I want to catch the EOF error:
use byteorder::{BigEndian, ReadBytesExt}; // 1.3.4
use std::fs::File;
fn main() {
let filename = "/etc/hosts";
let mut file = File::open(filename).expect("Cannot open file");
loop {
let binary = match file.read_u16::<BigEndian>() {
Ok(binary) => binary,
Err(e) => panic!("Can't read from file: {}, err {}", filename, e),
// Can I catch the EOF error here?
};
println!("{:?}", binary);
}
}
This works in Rust version 1.17.0 (and probably back to Rust 1.0):
let binary = match file.read_u16::<BigEndian>() {
Err(ref e) if e.kind() == std::io::ErrorKind::UnexpectedEof => break,
Err(e) => panic!("Can't read from file: {}, err {}", filename, e),
Ok(binary) => binary,
};
I find...
Err(e) => match e.kind() {
EndOfFile => break,
SomeOtherError => do_something(),
_ => panic!("Can't read from file: {}, err {}", filename, e),
},
... to be more readable than...
Ok(binary) => binary,
Err(ref e) if e.kind() == EndOfFile => break,
Err(ref e) if e.kind() == SomeOtherError => do_something(),
Err(e) => panic!("Can't read from file: {}, err {}", filename, e),
(I'm not sure what other errors we could expect to get...)
In other situations where the match guards might not be the same - the way that we're repeating e.kind() - we couldn't use the nested match.
This works as of Rust 1.25.
Editor's note: This code example is from a version of Rust prior to 1.0 and does not apply to stable Rust 1.0 io::Error. The concept of nested pattern matching still applies in other contexts.
You can match the kind as part of the pattern, using some more advanced features of pattern matching:
Err(IoError { kind: IoErrorKind::EndOfFile, .. }) => break,
Err(e) => panic!("Can't read from file: {}, err {}", filename, e),
The first variant means “an Err containing an IoError where kind is IoErrorKind::EndOfFile and all the other fields are whatever you like”. The second then means “any other Err, binding the contained value to the variable name e”.
I figured it out. I changed this line to check the error type! Hope this helps others.
Err(e) => if e.kind == IoErrorKind::EndOfFile { break } else { panic!("Can't read from file: {}, err {}", filename, e) },
Here is an example of matching a MySQL IoError:
match pool.prep_exec("SELECT SLEEP(10)", ()) {
Ok(_) => (),
Err(mysql::Error::IoError(e)) => {
eprintln!("IoError: {}", e);
do_something();
}
Err(e) => {
eprintln!("{}", e);
return;
}
}