英文:
Trait conflict between impl for T and something of FnMut() -> T
问题
Here's the translated code you provided:
pub trait ArrayFill<T, const LENGTH: usize>
{
fn fill_into_array(self) -> [T; LENGTH];
}
impl<T, const LENGTH: usize> ArrayFill<T, LENGTH> for T
where
T: Copy
{
fn fill_into_array(self) -> [T; LENGTH]
{
[self; LENGTH]
}
}
impl<F, T, const LENGTH: usize> ArrayFill<T, LENGTH> for F
where
F: FnMut() -> T
{
fn fill_into_array(self) -> [T; LENGTH]
{
todo!()
}
}
And the attempted solution code:
#![feature(auto_traits)]
#![feature(negative_impls)]
auto trait NotFnMutToSelf {}
impl<F> !NotFnMutToSelf for F
where
F: FnMut() -> F {}
pub trait ArrayFill<T, const LENGTH: usize>
{
fn fill_into_array(self) -> [T; LENGTH];
}
impl<T, const LENGTH: usize> ArrayFill<T, LENGTH> for T
where
T: Copy + NotFnMutToSelf
{
fn fill_into_array(self) -> [T; LENGTH]
{
[self; LENGTH]
}
}
impl<F, T, const LENGTH: usize> ArrayFill<T, LENGTH> for F
where
F: FnMut() -> T
{
fn fill_into_array(self) -> [T; LENGTH]
{
todo!()
}
}
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() -> T
. I'm guessing the only point of overlap would be the case of F: FnMut() -> F
, in which case the function F
returns itself and equals T
. Essentially, the compiler considers the possiblity of F
being an FnMut() -> (FnMut() -> (FnMut() -> ...))
.
Here's the code, which will not compile:
pub trait ArrayFill<T, const LENGTH: usize>
{
fn fill_into_array(self) -> [T; LENGTH];
}
impl<T, const LENGTH: usize> ArrayFill<T, LENGTH> for T
where
T: Copy
{
fn fill_into_array(self) -> [T; LENGTH]
{
[self; LENGTH]
}
}
impl<F, T, const LENGTH: usize> ArrayFill<T, LENGTH> for F
where
F: FnMut() -> T
{
fn fill_into_array(self) -> [T; LENGTH]
{
todo!()
}
}
I've tried using an auto-trait with a negative implementation for F: FnMut() -> 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:
#![feature(auto_traits)]
#![feature(negative_impls)]
auto trait NotFnMutToSelf {}
impl<F> !NotFnMutToSelf for F
where
F: FnMut() -> F {}
pub trait ArrayFill<T, const LENGTH: usize>
{
fn fill_into_array(self) -> [T; LENGTH];
}
impl<T, const LENGTH: usize> ArrayFill<T, LENGTH> for T
where
T: Copy + NotFnMutToSelf
{
fn fill_into_array(self) -> [T; LENGTH]
{
[self; LENGTH]
}
}
impl<F, T, const LENGTH: usize> ArrayFill<T, LENGTH> for F
where
F: FnMut() -> T
{
fn fill_into_array(self) -> [T; LENGTH]
{
todo!()
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论