Function signature when size of returned array can be computed at compile time

Hey everyone!
I am currently stuck at a problem I can't quite wrap my head around.
I have this function

fn seal(&self, m: &[u8], ad: &[u8], nonce: &[u8; 8]) -> [u8; ???]

and I known 100% that the returned array will always be of size m.len() + 16.
This should be something that is computable at compile time, is it now? So how can I express this in the return value?

It's not, since m.len() is not computable at compile-time.

But it is, or not? I can provide e.g. [0u8; 16] as m, whose size is known at compile time.

Or you can provide vec.as_slice(), whose size isn't. Current signature doesn't give the information on m.len() (and in general, it can't - Rust is not dependently-typed).

1 Like

You would need something like :

// on nightly

fn seal<const N: usize>(&self, m: &[u8; N], ad: &[u8], nonce: &[u8; 8]) -> [u8; N + 16]
                                       ^^ note I'm adding this information for the compiler, 
                                          you won't be able to pass any slice but 
                                          only references to fixed sized arrays here

Ahh yes this is exactly what I was looking for. Thank you very much!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.