JulianSchmid / etherparse

A rust library for parsing ethernet & ethernet using protocols.
Apache License 2.0
286 stars 54 forks source link

The "etherparse::ReadError" enum does not implement the "std::error::Error" trait, which does not seem to allow for using the "?" operator #8

Closed p1-mmr closed 4 years ago

p1-mmr commented 4 years ago

Hello,

Thank you for your great project.

I would note that when writing a function like this:

use std::error::Error;
use etherparse::Ipv4Header;

extern crate etherparse;

fn some_parsing_function(input_payload: &[u8]) -> Result<Option<&[u8]>, Box<dyn Error>> {
    let (ip_header, inner_payload): (Ipv4Header, &[u8]) = Ipv4Header::read_from_slice(input_payload)?;
    Ok(None)
}

Will cause the compiler to raise the following error:

error[E0277]: the trait bound `etherparse::ReadError: std::error::Error` is not satisfied
  --> src/main.rs:16:116
   |
16 |             let (ip_header, inner_payload): (Ipv4Header, &[u8]) = Ipv4Header::read_from_slice(input_payload)?;
   |                                                                                                             ^ the trait `std::error::Error` is not implemented for `etherparse::ReadError`
   |
   = note: required because of the requirements on the impl of `std::convert::From<etherparse::ReadError>` for `std::boxed::Box<dyn std::error::Error>`
   = note: required by `std::convert::From::from`

It seems to be caused by the fact that etherparse::ReadError does not implement std::error::Error.

I think that it would be handy to be able to use the ? operator in this case (maybe that there is another way?).

Regards,

JulianSchmid commented 4 years ago

Hi p1-mmr,

Good idear. When I originally wrote the library, main functions could not yet return directly with the ? operator. But implementing the std::error::Error trait makes sense anyways.

I will have look at implementing the std::error::Error trait for my error enums during the next week or so.

Greets Julian

JulianSchmid commented 4 years ago

Hi,

I added std::error::Error implementations for ReadError, WriteError & ValueError in version https://github.com/JulianSchmid/etherparse/releases/tag/0.9.0 .

Greets Julian