Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
WeiPhil opened this issue May 9, 2020 · 1 comment
Open

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

WeiPhil opened this issue May 9, 2020 · 1 comment

Comments

@WeiPhil
Copy link

WeiPhil commented May 9, 2020

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

@novacrazy
Copy link
Collaborator

novacrazy commented Mar 28, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants