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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use ;
use ;
/// Casts `&T` to `&[u8]`
///
/// # Example
///
/// ```rust
/// use constmuck::bytes_of;
///
/// const BYTES: &[u8] = bytes_of(&987654321u32);
///
/// assert_eq!(*BYTES, 987654321u32.to_ne_bytes());
/// ```
pub const
where
T: NoUninit,
/// Casts `&[T]` to `&[U]`
///
/// If this function does not panic,
/// the length of the returned slice is `from.len() * size_of::<T>() / size_of::<U>()`.
///
/// # Panics
///
/// This function panics in the cases where [`try_cast_slice_alt`]
/// returns [an error](crate::try_cast_slice_alt#errors).
///
/// # Difference with `bytemuck`
///
/// This function requires `T` to have an alignment larger than or equal to `U`.
/// [`bytemuck::cast_slice`] allows `T` to have a lower alignment than `U`,
/// so long as the `from` reference happens to be aligned to `U`.
///
/// # Example
///
/// ```
/// use constmuck::cast_slice_alt;
///
/// const TRIPLES: &[[u8; 3]] = cast_slice_alt(&[3u8, 5, 8, 13, 21, 34, 55, 89, 144]);
///
/// assert_eq!(*TRIPLES, [[3, 5, 8], [13, 21, 34], [55, 89, 144]]);
///
/// ```
pub const
where
T: NoUninit,
U: AnyBitPattern,
/// Tries to cast `&[T]` to `&[U]`
///
/// If this function returns successfully,
/// the length of the returned slice is `from.len() * size_of::<T>() / size_of::<U>()`.
///
/// # Errors
///
/// This function returns errors in these cases:
/// - The alignment of `T` is larger than `U`, returning a
/// `Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)`.
///
/// - `T` xor `U` is zero-sized, but the other type parameter isn't zero-sized,
/// returning a `Err(PodCastError::SizeMismatch)`.
///
/// - `from.len() * size_of::<T>()` does not divide evenly into `size_of::<U>()`,
/// returning a `Err(PodCastError::OutputSliceWouldHaveSlop)`.
///
///
/// <span id="differences"></span>
/// # Difference with `bytemuck`
///
/// This function requires `T` to have an alignment larger than or equal to `U`.
/// [`bytemuck::try_cast_slice`] allows `T` to have a lower alignment than `U`,
/// so long as the `from` reference happens to be aligned to `U`.
///
/// # Example
///
/// ```
/// use constmuck::PodCastError;
/// use constmuck::try_cast_slice_alt;
///
/// type Res<T> = Result<T, PodCastError>;
///
/// // casting the slice to element of different length,
/// // this works as long as the slice's size divides evenly into the new element size.
/// const I8_PAIRS: Res<&[[i8; 2]]> = try_cast_slice_alt(&[100u8, 101, 102, 253, 254, 255]);
/// assert_eq!(I8_PAIRS, Ok(&[[100i8, 101], [102, -3], [-2, -1]][..]));
///
/// // this function can't be used to cast slices from ZSTs to non-ZSTs and vice versa.
/// const ERR_ZST: Res<&[()]> = try_cast_slice_alt(&[0u8]);
/// assert_eq!(ERR_ZST, Err(PodCastError::SizeMismatch));
///
/// // this produces an error, since the slice's size (in bytes) does not
/// // divide evenly into `[u8; 2]`'s size.
/// const ERR_SLOP: Res<&[[u8; 2]]> = try_cast_slice_alt(&[3u8, 5, 8]);
/// assert_eq!(ERR_SLOP, Err(PodCastError::OutputSliceWouldHaveSlop));
///
/// // this produces an error because the element's alignment is increased.
/// const ERR_ALIGN: Res<&[u16]> = try_cast_slice_alt(&[3u8, 5, 8, 13]);
/// assert_eq!(ERR_ALIGN, Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned));
///
///
/// ```
pub const
// Returns dividend / divisor iff the division has no remainder,
// otherwise returns None.
//
// Panics if `divisor == 0`.
const