1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::{private, ReprC, ReprRust};
impl<T, const N: usize> ReprRust<*const T> for &[T; N]
where
*const T: private::CFFISafe,
{
#[inline]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn from(ptr: *const T) -> Self {
let s = unsafe { std::slice::from_raw_parts(ptr, N) };
s.try_into().unwrap_or_else(|_| {
let ty = std::any::type_name::<T>();
panic!("impossible to convert &[{ty}] into &[{ty}; {N}]");
})
}
}
impl<T> ReprC<Vec<T>> for *const T
where
*const T: private::CFFISafe,
{
#[inline]
fn from(v: Vec<T>) -> Self {
let x = v.as_ptr();
std::mem::forget(v);
x
}
}
#[test]
fn _1() {
let x = &[1, 2, 3]; let y: &[i32; 3] = ReprRust::from(ReprC::from(x.to_vec()));
assert!(x == y);
}