-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
The implementation From<&[T; N]>
for Option<&[T]>
seems missing
#125600
Comments
From<&[T; N]>
for Option<&[T]>
is missingFrom<&[T; N]>
for Option<&[T]>
seems missing
|
Regarding consistency, I would argue otherwise with the example below. Isn't it inconsistent that fn bar<'a>(a: Option<&'a [i32]>>) { }
fn foo<'a>(a: impl Into<Option<&'a [i32]>>) { }
fn main() {
bar(Some(&[1, 2, 3])); // This works fine
foo(Some(&[1, 2, 3])); // This would complain
foo(&[1, 2, 3]); // This would complain as well
} Isn't it more intuitive that something that can be accepted as |
I see where you're coming from and why it's surprising. I still feel that this is trying to do too much in a single step, but I may also biased against callee-side conversions and prefer to perform them on the caller side. In particular, I avoid relying on Given your last example, it may be acceptable for consistency. |
This is probably not needed in your use cases, but it doesn't mean other people won't need it. Conversion from |
Your issue is more fundamental than a few "missing" trait impls. Unfortunately the compiler cannot figure out that there is an impl that could apply, because it doesn't try to coerce after choosing a type for a generic parameter. Your change would allow this one special case, while still not allowing all other kinds of coercion like function item to pointer, closure to function pointer, type to dyn trait, ... I don't know if this issue is solveable in the trait solver, or if it would require allowing people to write trait bounds using the This may also be a breaking change. I can't come up with a test right now, but it seems like it would cause existing code to fail with |
@oli-obk I agree that this is more than just missing some trait impls, which is also why I had been fine with using I tried to find rationale behind the current design choice within the community but failed to get much help. Folks often simply end up suggesting not using
That was my original concern as well, which was why I opened that PR, and I was surprised the PR passed all tests |
Please correct me if I'm wrong. My understanding, with my limited knowledge on the compiler, is that the current limitation is caused by the compiler failing to coerce And this is different from the |
The following code currently does not compile
Kindly raising this as an issue because something similar was recently merged #113489
The text was updated successfully, but these errors were encountered: