zxcvbn-rs icon indicating copy to clipboard operation
zxcvbn-rs copied to clipboard

Change feedback to return Option<&Feedback>

Open Grafcube opened this issue 2 years ago • 1 comments

This PR changes the Entropy::feedback() function to return Option<&Feedback> instead of &Option<Feedback>. This makes the API significantly easier to use as Option<&...> implements Copy and it's also more accurate, semantically.

Say I want to call map on the return value of the function:

let feedback = entropy.feedback().map(|f| f.to_owned());

This results in an error with &Option<Feedback>:

error[E0507]: cannot move out of a shared reference
    --> ui/src/routes/auth.rs:233:38
     |
233  |                         feedback.set(entropy.feedback().map(|f| f.to_owned()));
     |                                      ^^^^^^^^^^^^^^^^^^ --------------------- value moved due to this method call
     |                                      |
     |                                      help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
     |                                      move occurs because value has type `std::option::Option<Feedback>`, which does not implement the `Copy` trait
     |
note: `std::option::Option::<T>::map` takes ownership of the receiver `self`, which moves value

Using Option<&Feedback> makes this compile without any errors.

Grafcube avatar Oct 24 '23 15:10 Grafcube

@shssoichiro does this PR look okay?

Grafcube avatar Nov 13 '23 11:11 Grafcube

Sorry for not looking at this sooner. Yes, this looks good. Thanks.

shssoichiro avatar May 23 '24 15:05 shssoichiro