I create the following custom container type S and some inner type T.
struct T(u32);
struct S(Vec<T>);
impl std::str::FromStr for S {...}
I can't just
type S = Vec<T>;
because impl FromStr for S errors:
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
This is fine.
Now I'm writing some tests to validate from_str() behaviour.
Using Tracking Issue for assert_matches · Issue #82775 · rust-lang/rust · GitHub to get nice error messages on test failures I write the following
let s = "1 2 3";
let parsed = s.parse::<S>();
assert_matches!(parsed, Ok(S([T(1), T(2), T(3)])));
Aside from whether it makes sense; it's intuitive for me to write it like this.
Unfortunately this doesn't work
error[E0529]: expected an array or slice, found `Vec<T>`
--> src/main.rs:20:34
|
20 | assert_matches!(parsed, Ok(S([T(1), T(2), T(3)])));
| ^^^^^^^^^^^^^^^^^^ pattern cannot match with input type `Vec<T>`
|
help: consider using `as_deref` here
|
20 | assert_matches!(parsed.as_deref(), Ok(S([T(1), T(2), T(3)])));
| +++++++++++
For more information about this error, try `rustc --explain E0529`.
When I used the compiler hint above, it didn't resolve the issue.
I can however do this:
assert_matches!(parsed, Ok(S(..)));
let s_inner = parsed.unwrap().0;
assert_matches!(s_inner.as_slice(), [T(1), T(2), T(3)]);
So my questions here are:
- How should I've written the code to be idiomatic in the scenario above?
- How should the compiler hint above be changed?