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

Avoid unwrap() #45

Merged
merged 1 commit into from
Dec 3, 2015
Merged

Avoid unwrap() #45

merged 1 commit into from
Dec 3, 2015

Conversation

crackofdusk
Copy link
Contributor

This addresses #34 partially.

There are a few cases that I wasn't sure how to handle:

It seems to me that in those cases one could return an empty iterator.

I was also unsure whether I could really ignore the result of delete statement executions. That's one reason I put those changes in a separate commit.

If nothing else, this pull request locates occurrences of unwrap() in the current documentation.

match users.filter(name.eq(target_name)).load(connection) {
Ok(results) => results.collect(),
Err(_) => vec![]
}
Copy link
Member

Choose a reason for hiding this comment

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

Let's just change the return type of the function to QueryResult<Vec<(...)>>. I think this match distracts from the example too much.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

QueryResult<Vec<(...)>> is awkward here because load returns QueryResult<Cursor<_,_>>.

Would the following be okay instead?

fn users_with_name(connection: &Connection, target_name: &str)
    -> Option<Vec<(i32, String, Option<String>)>>
{
    use self::users::dsl::*;
    users.filter(name.eq(target_name)).load(connection)
        .ok()
        .map(|x| x.collect())
}

Copy link
Member

Choose a reason for hiding this comment

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

Why not this?

fn users_with_name(connection: &Connection, target_name: &str)
    -> QueryResult<Vec<(i32, String, Option<String>)>>
{
    use self::users::dsl::*;
    users.filter(name.eq(target_name)).load(connection)
        .map(|x| x.collect())
}

@sgrif
Copy link
Member

sgrif commented Dec 3, 2015

In theory we shouldn't be ignoring the result of delete statements. It's technically fine, since we're asserting it was successful one line down, but I don't know if encouraging not checking results is better than encouraging unwrapping. At least this results in a compiler warning.

@crackofdusk
Copy link
Contributor Author

I wrapped the delete statements in try! instead so that we still acknowledge the possibility of an error.

@@ -82,12 +88,18 @@ pub fn update<T: UpdateTarget>(source: T) -> IncompleteUpdateStatement<T> {
/// # }
/// #
/// # fn main() {
/// # delete();
/// # ()
Copy link
Member

Choose a reason for hiding this comment

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

You can ✂️ this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course, silly me.

@sgrif
Copy link
Member

sgrif commented Dec 3, 2015

Looks good other than that comment. Can you squash the commits?

@crackofdusk
Copy link
Contributor Author

All squashed, thanks for your feedback!

sgrif added a commit that referenced this pull request Dec 3, 2015
@sgrif sgrif merged commit bc0a99b into diesel-rs:master Dec 3, 2015
@crackofdusk crackofdusk deleted the avoid_unwrap branch December 3, 2015 21:39
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.

None yet

2 participants