generic-array icon indicating copy to clipboard operation
generic-array copied to clipboard

Generic primitive type parameter with std::marker::Sized bound

Open WeiPhil opened this issue 5 years ago • 1 comments

Hi, I am trying to implement something that looks like this for some tuple struct Array :

impl<T: std::marker::Sized> Array<T, U3> {
    pub fn new(x: T, y: T, z: T) -> Array<T, U3> {
        let data: GenericArray<T, U3> = arr![T; x, y, z];
        Array { data }
    }
}

Unfortunately this seems to be impossible for now and the compilers complains that arr![T; x, y, z] has no fixed size at compile-time. It even points me to the impl<T: std::marker::Sized> telling me the parameter nneds to be std::marker::Sized even if it is already bounded. I suppose there must be a way in the macro to add this condition on T without loss of generality but I'm not familiar with procedural macros myself. Any tip here?

Cheers, Philippe

WeiPhil avatar May 09 '20 12:05 WeiPhil

What version were you using when you tried this? As of 0.14.7, I don't reproduce something exactly like that. Perhaps your original Array struct was defined as struct Array<T: ?Sized, N: ArrayLength<T>> { ... }, which would have indeed failed to compile due to the ?Sized bound, which GenericArray does not support containing.

struct Array<T, N: ArrayLength<T>> {
    data: GenericArray<T, N>,
}

impl<T: Sized> Array<T, U3> {
    pub fn new(x: T, y: T, z: T) -> Self {
        Array {
            data: GenericArray::from([x, y, z]),
        }
    }
}

works fine, though.

EDIT: Of course, by now you may be able to use const-generics for your uses.

novacrazy avatar Mar 28 '23 11:03 novacrazy