Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/error: Add From<ConnectionError> for io::Error implementation #136

Conversation

thomaseizinger
Copy link
Contributor

This allows the use of ? in functions that return io::Error.

This allows the use of `?` in functions that return `io::Error`.
Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with this.

Just needs a changelog entry and patch version bump.

src/error.rs Outdated
Comment on lines 103 to 117
#[test]
fn connection_error_can_be_question_marked_to_io_error() {
returns_io_error().expect_err("to fail");
}

fn returns_io_error() -> std::io::Result<()> {
always_err()?;

Ok(())
}

fn always_err() -> Result<(), ConnectionError> {
Err(ConnectionError::Closed)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[test]
fn connection_error_can_be_question_marked_to_io_error() {
returns_io_error().expect_err("to fail");
}
fn returns_io_error() -> std::io::Result<()> {
always_err()?;
Ok(())
}
fn always_err() -> Result<(), ConnectionError> {
Err(ConnectionError::Closed)
}
}
#[test]
fn connection_error_can_be_question_marked_to_io_error() {
fn returns_io_error() -> std::io::Result<()> {
always_err()?;
Ok(())
}
fn always_err() -> Result<(), ConnectionError> {
Err(ConnectionError::Closed)
}
returns_io_error().expect_err("to fail");
}
}

How about nesting them?

src/error.rs Outdated
Comment on lines 99 to 117
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn connection_error_can_be_question_marked_to_io_error() {
returns_io_error().expect_err("to fail");
}

fn returns_io_error() -> std::io::Result<()> {
always_err()?;

Ok(())
}

fn always_err() -> Result<(), ConnectionError> {
Err(ConnectionError::Closed)
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connection_error_can_be_question_marked_to_io_error

I have to admit that I don't really see the point of this test. It's general rust logic that we can question mark one error into another if From<> is implemented. Since we implemented impl From<ConnectionError> for std::io::Error we know for sure that this will not fail.
From my understanding, tests should check for implementation errors, edge cases, side effect, ... etc. This is not the case here. What is the gain from this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to test that what I wanted to achieve in rust-libp2p actually works. Happy to delete the test again if you think it is bloat (I think I agree with you).

We now know that this test works, so we don't need to keep it around.
@thomaseizinger
Copy link
Contributor Author

Closing in favor of libp2p/rust-libp2p#2710.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants