Generic array of double length

Hello

I am trying to use typenum and generic_array to create the following struct:

use generic_array::{ArrayLength, GenericArray};
use typenum::{Prod, U2};

pub struct Foo<N>
where
    N: ArrayLength<bool>,
{
    a: GenericArray<bool, N>,
    b: GenericArray<bool, Prod<N, U2>>,
}

The goal is to have the second array with double the size of the first. However this will not compile.
The type of N do not allow me to use Prod. How to make this work?

Prod is just an alias to the associated type of the std::ops::Mul trait, so with two additional trait bounds this should work:

use generic_array::{ArrayLength, GenericArray};
use typenum::{Prod, U2};
use std::ops::Mul;

pub struct Foo<N>
where
    N: ArrayLength<bool> + Mul<U2>,
    Prod<N, U2>: ArrayLength<bool>,
{
    a: GenericArray<bool, N>,
    b: GenericArray<bool, Prod<N, U2>>,
}
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.