Skip to content

Follow std library convention and return Option for to_ascii() and into_ascii() instead of failing #12320

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 4 commits into from

Conversation

mneumann
Copy link
Contributor

Return an Option for to_ascii() and into_ascii() functions. Remove to_ascii_opt() and into_ascii_opt().

Use explicit unwrap() where needed. Basically rename:

* to_ascii_opt -> to_ascii
* into_ascii_opt -> into_ascii

and get rid of the version which uses fail!
/// Take ownership and cast to an ascii vector. Return None on non-ASCII input.
#[inline]
fn into_ascii_opt(self) -> Option<~[Ascii]> {
Copy link
Member

Choose a reason for hiding this comment

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

Will users want the value of self back if the conversion fails? It may be worth changing the return type of this to Result<~[Ascii], Self>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, good idea.

Return Err(self) from into_ascii() in case of error, so the original value is
not lost.
@mneumann
Copy link
Contributor Author

I changed into_ascii() to return a Result as you suggested.

@mneumann
Copy link
Contributor Author

Fixed by eca39b0

@@ -529,21 +513,21 @@ mod tests {
#[test]
fn test_ascii_vec() {
let test = &[40u8, 32u8, 59u8];
assert_eq!(test.to_ascii(), v2ascii!([40, 32, 59]));
assert_eq!("( ;".to_ascii(), v2ascii!([40, 32, 59]));
assert_eq!(test.to_ascii(), Some(v2ascii!([40, 32, 59])));
Copy link
Contributor

Choose a reason for hiding this comment

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

So assert_eq!(test.to_ascii(), Some(v2ascii!(&[40, 32, 59]))); instead?

@alexcrichton
Copy link
Member

Closing due to inactivity, but feel free to reopen with a rebase!

bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 25, 2022
Hide closure ret hints if ret type is specified

Fixes rust-lang#12319
flip1995 pushed a commit to flip1995/rust that referenced this pull request Dec 26, 2024
…t-lang#13830)

fix rust-lang#12320

When there're multiple attributes, clippy suggests leaving an extra
comma and it makes an error.

changelog: [`must_use_unit`]: No longer make incorrect suggestions when
multiple attributes present.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants