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

Make 'static part of ArrayLength trait bounds? #107

Closed
cbeck88 opened this issue Jul 30, 2020 · 1 comment · Fixed by #138
Closed

Make 'static part of ArrayLength trait bounds? #107

cbeck88 opened this issue Jul 30, 2020 · 1 comment · Fixed by #138

Comments

@cbeck88
Copy link

cbeck88 commented Jul 30, 2020

In some recent code, for example here, https://docs.rs/generic-array/0.14.3/src/generic_array/sequence.rs.html#305, we have + 'static bounds on types that represent ArrayLength typenums.

unsafe impl<'a, T, N, K> Split<T, K> for &'a GenericArray<T, N>
where
    N: ArrayLength<T>,
    K: ArrayLength<T> + 'static,
    N: Sub<K>,
    Diff<N, K>: ArrayLength<T>,
{
    type First = &'a GenericArray<T, K>;
    type Second = &'a GenericArray<T, Diff<N, K>>;

    fn split(self) -> (Self::First, Self::Second) {
        unsafe {
            let ptr_to_first: *const T = self.as_ptr();
            let head = &*(ptr_to_first as *const _);
            let tail = &*(ptr_to_first.add(K::USIZE) as *const _);
            (head, tail)
        }
    }
}

Sometimes in my own code that uses generic array, I have to put 'static on the number type bounds, and it's sometimes hard to understand why.

It seems to me that we could change ArrayLength to require 'static

pub unsafe trait ArrayLength<T>: Unsigned + 'static {
    /// Associated type representing the array type for the number
    type ArrayType;
}

Since this trait is only implemented for typenum types and those are all ZWT's anyway, the 'static bound is always true and this breaks nothing in practice. It only helps the compiler to see that some things that are generic parameters are 'static without the user having to spell it out.

Then you could remove + 'static in several places in generic array itself, e.g.

    K: ArrayLength<T> + 'static,

in code like impl ... Split because the + 'static would be implied.

What do you think?

@novacrazy
Copy link
Collaborator

This seems like a good idea for a version 1.0 change, or at least a 0.15, just in case.

As far as I know, there is no motivation to try implementing typenum::Unsigned on anything that holds a reference, it's all just ZSTs, so this should be a harmless change.

@novacrazy novacrazy mentioned this issue Mar 28, 2023
Merged
2 tasks
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

Successfully merging a pull request may close this issue.

2 participants