Skip to content

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

Closed
thomaseizinger wants to merge 2 commits intolibp2p:masterfrom
thomaseizinger:feature/add-from-error-for-io-error
Closed

src/error: Add From<ConnectionError> for io::Error implementation#136
thomaseizinger wants to merge 2 commits intolibp2p:masterfrom
thomaseizinger:feature/add-from-error-for-io-error

Conversation

@thomaseizinger
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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