Reusing the description of an existing error when creating a new error

I have the following code in Rust that does not compile but shows the intention of what I would like to do.

    pub fn parse(cursor: &mut io::Cursor<&[u8]>) -> io::Result<Ack> {
        use self::byteorder::{BigEndian, ReadBytesExt};
        use self::core::error::Error;

        match cursor.read_u16::<BigEndian>() {
            Err(byteorder::Error::Io(error)) => Err(error),
            Err(error) =>
                Err(io::Error::new(io::ErrorKind::Other, error.description(),
                                   None)),
            Ok(value) => Ok(Ack { block_number: value })
        }
    }

Essentially, I want to take a description of the error error returned by byteorder and use it to create a description of the error. I will transfer my library to the user. This is not a mistake packets.rs:166:58: 166:63 error: does not live long enough, and I understand why.

, std::io::Result byteorder::Error::Io. , , , std::io::Error, byteorder::Error. , , , .

Rust . ?

+4
2

, io::Error::new() &'static str, byteorder::Error::description() &'a str, 'a - , 'static. , io::Error.

byteorder::Error detail io::Error:

Err(error) =>
    Err(io::Error::new(
        io::ErrorKind::Other,
        "byteorder error",
        Some(error.description().to_string())
    )),

, "" . FromError -

try!(cursor.read_u16::<BigEndian>()
    .map(|value| Ack { block_number: value }))

. , , " " - / FromError .

+1

, . ref?

Err(byteorder::Error::Io(ref error)) => Err(error),
0

All Articles