我已经写了一个类型级别的添加,工作正常 . 每个数字都表示为 Inc<Inc<...<Zero>...>> . 有一个特征 Add ,然后结构 Bop<Left, Right> (二元操作)实现 Add 特征 . 用法是 <Bop<Inc<Inc<Inc<Zero>>>, Inc<Inc<Zero>>> as Add>::Get 将是 Inc<Inc<Inc<Inc<Inc<Zero>>>>> ,即3 2 = 5 .

然后我根据添加内容编写斐波纳契数列:

use std::marker::PhantomData;
struct Zero {}
struct Inc<T> {
    _phantom: PhantomData<T>,
}

struct Bop<Left, Right> {
    _phantom_l: PhantomData<Left>,
    _phantom_r: PhantomData<Right>,
}

trait Add {
    type Get;
}

impl<Left> Add for Bop<Left, Zero> {
    type Get = Left;
}

impl<Left, RightInner> Add for Bop<Left, Inc<RightInner>>
where
    Bop<Left, RightInner>: Add,
{
    type Get = Inc<<Bop<Left, RightInner> as Add>::Get>;
}

type One = Inc<Zero>;
type Two = Inc<One>;
type Three = Inc<Two>;
type Four = Inc<Three>;
type Five = Inc<Four>;
// then <Bop<Three, Two> as Add>::Get will be type Five

// 0 1 2 3 4 5 6 7
// 0 1 1 2 3 5 8 13
// usage: <Inc<Inc<Inc<Zero>>> as Fib>::Get should be the third fib number,
// i.e. 2, or Inc<Inc<Zero>>
trait Fib {
    type Get;
}
impl Fib for Zero {
    type Get = Zero;
}
impl Fib for One {
    type Get = One;
}

impl<T> Fib for Inc<Inc<T>>
where
    T: Fib,
    Inc<T>: Fib,
    Bop<<T as Fib>::Get, <Inc<T> as Fib>::Get>: Add,
{
    type Get = <Bop<<T as Fib>::Get, <Inc<T> as Fib>::Get> as Add>::Get;
}

fn main() {}

complete example

编译器在找出 impl<T> Fib for Uop<Inc<Inc<T>>> 部分时遇到了麻烦,它抱怨道:

error[E0275]: overflow evaluating the requirement `Inc<_>: Fib`
  |
  = help: consider adding a `#![recursion_limit="128"]` attribute to your crate
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<_>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<_>>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<Inc<_>>>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<Inc<Inc<_>>>>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<Inc<Inc<Inc<_>>>>>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<Inc<Inc<Inc<Inc<_>>>>>>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<_>>>>>>>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<_>>>>>>>>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<_>>>>>>>>>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<_>>>>>>>>>>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<_>>>>>>>>>>>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<_>>>>>>>>>>>>>`
  = note: required because of the requirements on the impl of `Fib` for `Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<Inc<_>>>>>>>>>>>>>>`

// cut off more

错误的原因是什么?怎么解决?