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

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

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() -&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:

pub trait ArrayFill&lt;T, const LENGTH: usize&gt;
{
    fn fill_into_array(self) -&gt; [T; LENGTH];
}

impl&lt;T, const LENGTH: usize&gt; ArrayFill&lt;T, LENGTH&gt; for T
where
    T: Copy
{
    fn fill_into_array(self) -&gt; [T; LENGTH]
    {
        [self; LENGTH]
    }
}
impl&lt;F, T, const LENGTH: usize&gt; ArrayFill&lt;T, LENGTH&gt; for F
where
    F: FnMut() -&gt; T
{
    fn fill_into_array(self) -&gt; [T; LENGTH]
    {
        todo!()
    }
}

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:

#![feature(auto_traits)]
#![feature(negative_impls)]

auto trait NotFnMutToSelf {}
impl&lt;F&gt; !NotFnMutToSelf for F
where
    F: FnMut() -&gt; F {}

pub trait ArrayFill&lt;T, const LENGTH: usize&gt;
{
    fn fill_into_array(self) -&gt; [T; LENGTH];
}

impl&lt;T, const LENGTH: usize&gt; ArrayFill&lt;T, LENGTH&gt; for T
where
    T: Copy + NotFnMutToSelf
{
    fn fill_into_array(self) -&gt; [T; LENGTH]
    {
        [self; LENGTH]
    }
}
impl&lt;F, T, const LENGTH: usize&gt; ArrayFill&lt;T, LENGTH&gt; for F
where
    F: FnMut() -&gt; T
{
    fn fill_into_array(self) -&gt; [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.

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:

确定