-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the need for allocation from
is_utf8_string
.
Previously, `IsEncodedStringMatcher` converted its actual value input to a `Vec` and constructed a `String` out of that in order to match it. This is because using a string slice caused problems with the borrow checker as described in #323. The fundamental problem is that the type against which the inner matcher is matching is a reference whose lifetime must be named in the trait bound: ``` impl<ActualT: AsRef<[u8]> + Debug, InnerMatcherT> Matcher for IsEncodedStringMatcher<ActualT, InnerMatcherT> where InnerMatcherT: Matcher<ActualT = &'what str>, ^^^^ What goes here??? ``` One option is just to remove the reference: ``` impl<ActualT: AsRef<[u8]> + Debug, InnerMatcherT> Matcher for IsEncodedStringMatcher<ActualT, InnerMatcherT> where InnerMatcherT: Matcher<ActualT = str>, ``` This doesn't work when the inner matcher is `eq`, since one would then be comparing an `str` and a string slice: ``` verify_that!("A string".as_bytes(), is_utf8_string(eq("A string"))) // Compile error: cannot compare str and &str ``` However, this problem does not occur with `StrMatcher`: ``` verify_that!("A string".as_bytes(), is_utf8_string(constains_substring("A string"))) // Passes ``` So this also introduces an easy way to obtain a `StrMatcher` to check string equality: ``` verify_that!("A string".as_bytes(), is_utf8_string(eq_str("A string"))) // Passes ``` This is slightly inconvenient, since one must use a different matcher to compare string equality in this case. But it is well-documented and not too inconvenient to use. So it should be okay. The introduction of `eq_str` also allows fixing a longstanding limitation of the matcher `property!` -- that it could not match on properties returning string slices. This PR therefore also updates the documentation accordingly and adds an appropriate test.
- Loading branch information
Showing
6 changed files
with
107 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters