1

I want to do this

// I cannot alter this function
fn a_ffi_function(buffer: &[u8; 16]) {
    unsafe {

    }
}

fn main() {
    let buffer = [0u8; 64];
    for i in 0..4 {
        a_ffi_function(&buffer[i*16..(i+1)*16]);
    }
}

But the compiler does not allow me to

error[E0308]: mismatched types
  --> src/main.rs:10:24
   |
10 |         a_ffi_function(&buffer[i*16..(i+1)*16]);
   |                        ^^^^^^^^^^^^^^^^^^^^^^^ expected array of 16 elements, found slice
   |
   = note: expected type `&[u8; 16]`
              found type `&[u8]`

I cannot find a way to cast from &[u8] to &[u8; 16]. What can I do?

I don't want to copy memory. This application is time-sensitive.

I am using rustc 1.27.0-nightly (ad610bed8 2018-04-11).

buckle2000
  • 139
  • 8
  • 1
    You may especially be interested in [this answer](https://stackoverflow.com/a/38253365/1870153) in the duplicate. – ljedrz Apr 20 '18 at 11:13
  • 2
    "I don't want to copy memory. This application is time-sensitive." Two registers? That's less difference than whether the function gets inlined or not. – Dan Hulme Apr 20 '18 at 11:33
  • 1
    `fn a_ffi_function(buffer: &[u8; 16])` — There is no concept of an array in the C ABI. The FFI function should just take an `*const u8` and then none of this matters. – Shepmaster Apr 20 '18 at 16:00

0 Answers0