Trait conflict between impl for T and something of FnMut() -> T

huangapple go评论97阅读模式
英文:

Trait conflict between impl for T and something of FnMut() -> T

问题

Here's the translated code you provided:

  1. pub trait ArrayFill<T, const LENGTH: usize>
  2. {
  3. fn fill_into_array(self) -> [T; LENGTH];
  4. }
  5. impl<T, const LENGTH: usize> ArrayFill<T, LENGTH> for T
  6. where
  7. T: Copy
  8. {
  9. fn fill_into_array(self) -> [T; LENGTH]
  10. {
  11. [self; LENGTH]
  12. }
  13. }
  14. impl<F, T, const LENGTH: usize> ArrayFill<T, LENGTH> for F
  15. where
  16. F: FnMut() -> T
  17. {
  18. fn fill_into_array(self) -> [T; LENGTH]
  19. {
  20. todo!()
  21. }
  22. }

And the attempted solution code:

  1. #![feature(auto_traits)]
  2. #![feature(negative_impls)]
  3. auto trait NotFnMutToSelf {}
  4. impl<F> !NotFnMutToSelf for F
  5. where
  6. F: FnMut() -> F {}
  7. pub trait ArrayFill<T, const LENGTH: usize>
  8. {
  9. fn fill_into_array(self) -> [T; LENGTH];
  10. }
  11. impl<T, const LENGTH: usize> ArrayFill<T, LENGTH> for T
  12. where
  13. T: Copy + NotFnMutToSelf
  14. {
  15. fn fill_into_array(self) -> [T; LENGTH]
  16. {
  17. [self; LENGTH]
  18. }
  19. }
  20. impl<F, T, const LENGTH: usize> ArrayFill<T, LENGTH> for F
  21. where
  22. F: FnMut() -> T
  23. {
  24. fn fill_into_array(self) -> [T; LENGTH]
  25. {
  26. todo!()
  27. }
  28. }

I've provided the code translations as you requested. If you have any further questions or need assistance with specific parts of the code, please feel free to ask.

英文:

So i have this trait for filling an array from various types, however i get conflicting implementations for T and F: FnMut() -&gt; T. I'm guessing the only point of overlap would be the case of F: FnMut() -&gt; F, in which case the function F returns itself and equals T. Essentially, the compiler considers the possiblity of F being an FnMut() -&gt; (FnMut() -&gt; (FnMut() -&gt; ...)).

Here's the code, which will not compile:

  1. pub trait ArrayFill&lt;T, const LENGTH: usize&gt;
  2. {
  3. fn fill_into_array(self) -&gt; [T; LENGTH];
  4. }
  5. impl&lt;T, const LENGTH: usize&gt; ArrayFill&lt;T, LENGTH&gt; for T
  6. where
  7. T: Copy
  8. {
  9. fn fill_into_array(self) -&gt; [T; LENGTH]
  10. {
  11. [self; LENGTH]
  12. }
  13. }
  14. impl&lt;F, T, const LENGTH: usize&gt; ArrayFill&lt;T, LENGTH&gt; for F
  15. where
  16. F: FnMut() -&gt; T
  17. {
  18. fn fill_into_array(self) -&gt; [T; LENGTH]
  19. {
  20. todo!()
  21. }
  22. }

I've tried using an auto-trait with a negative implementation for F: FnMut() -&gt; F, but even then conflict persists, as i don't think the compiler is able to reason properly with the negative implementation. The attempted solution is shown below:

  1. #![feature(auto_traits)]
  2. #![feature(negative_impls)]
  3. auto trait NotFnMutToSelf {}
  4. impl&lt;F&gt; !NotFnMutToSelf for F
  5. where
  6. F: FnMut() -&gt; F {}
  7. pub trait ArrayFill&lt;T, const LENGTH: usize&gt;
  8. {
  9. fn fill_into_array(self) -&gt; [T; LENGTH];
  10. }
  11. impl&lt;T, const LENGTH: usize&gt; ArrayFill&lt;T, LENGTH&gt; for T
  12. where
  13. T: Copy + NotFnMutToSelf
  14. {
  15. fn fill_into_array(self) -&gt; [T; LENGTH]
  16. {
  17. [self; LENGTH]
  18. }
  19. }
  20. impl&lt;F, T, const LENGTH: usize&gt; ArrayFill&lt;T, LENGTH&gt; for F
  21. where
  22. F: FnMut() -&gt; T
  23. {
  24. fn fill_into_array(self) -&gt; [T; LENGTH]
  25. {
  26. todo!()
  27. }
  28. }

I don't think specialization could solve it, because although the implementations overlap, neither of them overlap the other completely.

Any idea how to resolve this conflict?

答案1

得分: 2

很遗憾,这是不可能的。像这样的情况甚至在专业化RFC中被描述为提案RFC 不会 解决的问题

英文:

Unfortunately, this is impossible. A situation like that was even described in the specialization RFC, as something the proposed RFC will not solve.

huangapple
  • 本文由 发表于 2023年7月3日 19:59:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定