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

add example for converting to I/O error #82

Merged
merged 1 commit into from Oct 7, 2017

Conversation

Projects
None yet
3 participants
@nivkner
Copy link
Contributor

nivkner commented Sep 22, 2017

fixes #81

@nivkner nivkner force-pushed the nivkner:from_err branch 5 times, most recently from e511bb6 to 78d0c68 Sep 22, 2017

@KodrAus

This comment has been minimized.

Copy link
Contributor

KodrAus commented Oct 1, 2017

Thanks @nivkner! I like the idea of offering an io_error method on walkdir's Error type that returns &io::Error. It might be a bit clearer and more compact than From::from in cases where you just want to inspect part of the error.

What do you think?

@nivkner nivkner force-pushed the nivkner:from_err branch from 78d0c68 to a149b49 Oct 1, 2017

@nivkner

This comment has been minimized.

Copy link
Contributor Author

nivkner commented Oct 1, 2017

I think it's a good idea since you probably don't want to consume the error just to check the io::Error.

However, I did make one change: return Option<&io::Error> since there is the case where Error doesn't represent an I/O error but rather a cycle.

Unlike From where you could just construct a new error, here the return value must be a borrow, where I have nothing to borrow.

Returning something new as a borrow just results in does not live long enough error.

@nivkner nivkner force-pushed the nivkner:from_err branch 3 times, most recently from d5f0822 to 9a9fb47 Oct 1, 2017

@KodrAus
Copy link
Contributor

KodrAus left a comment

This is looking good! Thanks @nivkner.

I'd like to know what you and @BurntSushi think about using Cow for the result of io_error instead of Option so it's always got a value. It might be that using a less common type isn't worthwhile.

src/lib.rs Outdated
///
/// [`None`]: https://doc.rust-lang.org/stable/std/option/enum.Option.html#variant.None
/// [`io::Error`]: https://doc.rust-lang.org/stable/std/io/struct.Error.html
pub fn io_error(&self) -> Option<&io::Error> {

This comment has been minimized.

@KodrAus

KodrAus Oct 2, 2017

Contributor

I wonder if this could be a Cow<'a, io::Error>? Then you could always return an error, but if it's not already an io::Error then you'll get Cow::Owned.

You should just be able to match on err.io_error().kind() then.

This comment has been minimized.

@nivkner

nivkner Oct 2, 2017

Author Contributor

That would be great, except for the fact that to do that, io::Error needs to implement ToOwned to be enclosed in Cow, which it doesn't.

This comment has been minimized.

@KodrAus

KodrAus Oct 2, 2017

Contributor

Ah right. That's a bit of a shame

src/lib.rs Outdated
@@ -1277,6 +1277,44 @@ impl Error {
self.depth
}

/// Inspect the underlying [`io::Error`] if there is one.
/// If a cycle was detected instead, [`None`] is returned.

This comment has been minimized.

@KodrAus

KodrAus Oct 2, 2017

Contributor

I would just say here that if the underlying error isn't an io::Error than the result is None. My other comment might make this obsolete though.

@nivkner nivkner force-pushed the nivkner:from_err branch from 9a9fb47 to dde9279 Oct 2, 2017

@BurntSushi
Copy link
Owner

BurntSushi left a comment

Overall this looks great! Thanks for helping to improve the documentation. :-)

I added a bunch of mostly stylistic nits that I hope we can fix before merging.

src/lib.rs Outdated
/// [`io::Error`]: https://doc.rust-lang.org/stable/std/io/struct.Error.html
pub fn io_error(&self) -> Option<&io::Error> {
match self.inner {
ErrorInner::Io { path: _ , ref err } => Some(err),

This comment has been minimized.

@BurntSushi

BurntSushi Oct 2, 2017

Owner

You don't need to mention path here since you aren't using it. This should work: ErrorInner::Io { ref err, .. }.

src/lib.rs Outdated
/// continue
/// }
/// };
/// println!("{}", entry.path().display());

This comment has been minimized.

@BurntSushi

BurntSushi Oct 2, 2017

Owner

I feel like the let entry = ... here is unnecessary. Maybe just move the println! into the match expression arm?

src/lib.rs Outdated
///
/// let mut it = WalkDir::new("foo").into_iter();
/// loop {
/// let entry = match it.next() {

This comment has been minimized.

@BurntSushi

BurntSushi Oct 2, 2017

Owner

Could you please reformat this code so that it fits into 79 columns inclusive?

src/lib.rs Outdated
/// If the underlying error isn't an [`io::Error`], [`None`] is returned.
///
/// ```rust,no-run
/// use walkdir::{WalkDir};

This comment has been minimized.

@BurntSushi

BurntSushi Oct 2, 2017

Owner

Please remove superfluous curly braces.

src/lib.rs Outdated
/// use std::path::Path;
///
/// let mut it = WalkDir::new("foo").into_iter();
/// loop {

This comment has been minimized.

@BurntSushi

BurntSushi Oct 2, 2017

Owner

Can you use a normal for loop here?

src/lib.rs Outdated
@@ -1277,6 +1277,44 @@ impl Error {
self.depth
}

/// Inspect the underlying [`io::Error`] if there is one.
/// If the underlying error isn't an [`io::Error`], [`None`] is returned.

This comment has been minimized.

@BurntSushi

BurntSushi Oct 2, 2017

Owner

Could the documentation be expanded to mention when this might not contain an io::Error? e.g., "In some circumstances, this Error won't correspond to an io::Error, for example, when the error was produced because a cycle was found in the directory tree while following symbolic links."

This comment has been minimized.

@BurntSushi

BurntSushi Oct 2, 2017

Owner

I think the docs should also mention that if the caller wants to acquire an owned io::Error, then they can convert this error into an io::Error using io::Error::from(err). And state that if this error doesn't correspond to an io::Error, then one is created.

@@ -1277,6 +1277,44 @@ impl Error {
self.depth
}

/// Inspect the underlying [`io::Error`] if there is one.

This comment has been minimized.

@BurntSushi

BurntSushi Oct 2, 2017

Owner

Could you please insert a blank line here?

@nivkner nivkner force-pushed the nivkner:from_err branch 4 times, most recently from 50be3b8 to dbadf08 Oct 4, 2017

@nivkner nivkner force-pushed the nivkner:from_err branch from dbadf08 to 8cdb7e5 Oct 7, 2017

@nivkner

This comment has been minimized.

Copy link
Contributor Author

nivkner commented Oct 7, 2017

Fixed the PR as per @BurntSushi's review.

@BurntSushi
Copy link
Owner

BurntSushi left a comment

Thanks!

@BurntSushi BurntSushi merged commit 64e50c8 into BurntSushi:master Oct 7, 2017

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.