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

EqStrUntilNul trait to compare Rust strings (str, String) against CStr16 and CString16 #462

Merged

Conversation

phip1611
Copy link
Contributor

@phip1611 phip1611 commented Jul 8, 2022

I cleaned up and improved the code of .eq()-implementations for CStr16 and CString16. Now it is easy to compare &str and String from the standard library with UEFI strings.

Let me know what you think.

According to the discussion, I changed the PR. Now there is a common trait which allows to compare Rust strings (str, String) against CStr16 and CString16.

PS: If it's okay for you, I'd be happy to support you as a maintainer/developer in uefi-rs as I'm submitting stuff to uefi-rs anyway from time to time. Just let me know if you need support

@phip1611 phip1611 force-pushed the str-partial-eq-improvements branch 2 times, most recently from 19fa556 to 688cd54 Compare July 8, 2022 14:32
@nicholasbishop
Copy link
Contributor

Thanks for the PR! Looks like there is one failing lint:

error: this creates an owned instance just for comparison
Error:    --> uefi-test-runner/src/proto/media/known_disk.rs:174:12
    |
174 |         if fs_info.volume_label().to_string() == "MbrTestDisk" {
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `fs_info.volume_label()`
    |
    = note: `-D clippy::cmp-owned` implied by `-D warnings`

I have a bit of hesitation on allowing CStr16 == str though:

  1. It differs from the standard library; core::ffi::CStr == str isn't currently allowed. Playground. (I did a quick search for rustlang issues to see if such a comparison had been discussed, but didn't find anything immediately.)
  2. Although str doesn't require a trailing null, it doesn't disallow it either. "abc\0" is a legal string. Playground. So some edge cases have to be considered. Is a CStr16 containing "abc" equal to both a str containing "abc" and a str containing "abc\0"? What about a str containing "abc\0xyz"?

That said, having the comparison does feel useful to avoid having to do a conversion of one of the operands. I wonder if maybe it would be better to add a new method to CStr16 so we could give it a more expressive name than eq, and provide a good place to document the exact behavior. Not sure what a good name would be though... CStr16::eq_str_until_nul maybe? (Got that idea from CStr::from_bytes_until_nul.)

@phip1611
Copy link
Contributor Author

It differs from the standard library; core::ffi::CStr == str isn't currently allowed

Ahh, I totally agree that uefi-rs should follow conventions and existing APIs from libstd / libcore.

CStr16::eq_str_until_nul

I like the idea, yeah! I update the PR accordingly soon.

@phip1611 phip1611 force-pushed the str-partial-eq-improvements branch from d35338e to e2958c9 Compare July 19, 2022 13:42
@phip1611 phip1611 changed the title Various improvements for .eq() on CStr16 and CString16 EqStrUntilNul trait to compare Rust strings (str, String) against CStr16 and CString16 Jul 19, 2022
@phip1611
Copy link
Contributor Author

@nicholasbishop I updated the PR. Let me know what you think.

TL;DR: new API:

        let input: &CStr16 = cstr16!("test");

        // test various comparisons with different order (left, right)
        assert!(input.eq_str_until_nul(&"test"));
        assert!(input.eq_str_until_nul(&String::from("test")));

        // now other direction
        assert!(String::from("test").eq_str_until_nul(input));
        assert!("test".eq_str_until_nul(input));

@phip1611 phip1611 force-pushed the str-partial-eq-improvements branch 6 times, most recently from 1ab6385 to 6cc6cc8 Compare July 20, 2022 09:15
@phip1611 phip1611 force-pushed the str-partial-eq-improvements branch from 6cc6cc8 to 8306f20 Compare July 26, 2022 08:56
@GabrielMajeri
Copy link
Collaborator

Thank you, the code looks good now. I'd ask @nicholasbishop to also take one final look if there's anything else left to improve before merging.

PS: If it's okay for you, I'd be happy to support you as a maintainer/developer in uefi-rs as I'm submitting stuff to uefi-rs anyway from time to time. Just let me know if you need support

I'm not against having more helping hands on board of the project 🙂 What do you say, @nicholasbishop?

@nicholasbishop
Copy link
Contributor

Looks good 👍

PS: If it's okay for you, I'd be happy to support you as a maintainer/developer in uefi-rs as I'm submitting stuff to uefi-rs anyway from time to time. Just let me know if you need support

I'm not against having more helping hands on board of the project 🙂 What do you say, @nicholasbishop?

That sounds good to me as well!

@nicholasbishop nicholasbishop merged commit f93f089 into rust-osdev:main Jul 28, 2022
@phip1611 phip1611 deleted the str-partial-eq-improvements branch July 28, 2022 07:25
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