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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use ;
/// Just a wrapper around `std::mem::transmute`. Used to map rkyv Archived primitive types back to
/// their un-archived versions. For primitive types such as f32 / f64 / u32 / u64, or arrays of
/// them, this should be fine assuming that you are not compiling for a target architecture with a
/// different endianness from the archived data. Where this is used in a non-rkyv context, T will
/// equal U and so this invocation of std::mem::transmute will have no effect at all.
pub
/// Converts a slice of one type (`&[U]`) into a slice of another type (`&[T]`) without
/// performing any data transformation but rather reinterpreting the raw memory.
///
/// Used to reinterpret slices of rkyv-Archived primitive types into their original form, where valid to do so.
///
/// # Type Parameters
/// - `T`: Target type to convert to.
/// - `U`: Source type to convert from.
///
/// # Parameters
/// - `items`: A reference to a slice of type `U` which will be reinterpreted as a slice of type `T`.
///
/// # Returns
/// A slice of type `T` with the same length and memory layout as the input slice `&[U]`.
///
/// # Panics
/// This function will panic in debug mode if:
/// - The size of `T` does not match the size of `U`.
/// - The alignment of `T` is greater than the alignment of `U`.
///
/// # Safety
/// This function is marked as `unsafe` because:
/// - It performs a reinterpretation of the raw memory of the input slice.
/// - You must ensure that the memory layout of `U` is compatible with `T` to avoid undefined behavior.
/// - Misusing this function with incompatible types can lead to data corruption, undefined behavior, or program crashes.
///
/// # Examples
/// ```ignore
/// // Transforming a &[Archived<u32>] slice back into a &[u32]-compatible view
/// let bytes: &[u8] = &[0x12, 0x34, 0x56, 0x78];
/// let words: &[u32] = transform_slice(bytes);
///
/// assert_eq!(words.len(), 1);
/// assert_eq!(words[0], 0x78563412);
/// ```
///
/// Be cautious while using this function, as improper usage may result in undefined behavior.
pub