/rust/registry/src/index.crates.io-1949cf8c6b5b557f/arrayref-0.3.9/src/lib.rs
Line | Count | Source |
1 | | //! This package contains just four macros, which enable the creation |
2 | | //! of array references to portions of arrays or slices (or things |
3 | | //! that can be sliced). |
4 | | //! |
5 | | //! # Examples |
6 | | //! |
7 | | //! Here is a simple example of slicing and dicing a slice into array |
8 | | //! references with these macros. Here we implement a simple |
9 | | //! little-endian conversion from bytes to `u16`, and demonstrate code |
10 | | //! that uses `array_ref!` to extract an array reference from a larger |
11 | | //! array. Note that the documentation for each macro also has an |
12 | | //! example of its use. |
13 | | //! |
14 | | //! ``` |
15 | | //! #[macro_use] |
16 | | //! extern crate arrayref; |
17 | | //! |
18 | | //! fn read_u16(bytes: &[u8; 2]) -> u16 { |
19 | | //! bytes[0] as u16 + ((bytes[1] as u16) << 8) |
20 | | //! } |
21 | | //! // ... |
22 | | //! # fn main() { |
23 | | //! let data = [0,1,2,3,4,0,6,7,8,9]; |
24 | | //! assert_eq!(256, read_u16(array_ref![data,0,2])); |
25 | | //! assert_eq!(4, read_u16(array_ref![data,4,2])); |
26 | | //! # } |
27 | | //! ``` |
28 | | #![deny(warnings)] |
29 | | #![no_std] |
30 | | |
31 | | #[cfg(test)] |
32 | | #[macro_use] |
33 | | extern crate std; |
34 | | |
35 | | /// You can use `array_ref` to generate an array reference to a subset |
36 | | /// of a sliceable bit of data (which could be an array, or a slice, |
37 | | /// or a Vec). |
38 | | /// |
39 | | /// **Panics** if the slice is out of bounds. |
40 | | /// |
41 | | /// ``` |
42 | | /// #[macro_use] |
43 | | /// extern crate arrayref; |
44 | | /// |
45 | | /// fn read_u16(bytes: &[u8; 2]) -> u16 { |
46 | | /// bytes[0] as u16 + ((bytes[1] as u16) << 8) |
47 | | /// } |
48 | | /// // ... |
49 | | /// # fn main() { |
50 | | /// let data = [0,1,2,3,4,0,6,7,8,9]; |
51 | | /// assert_eq!(256, read_u16(array_ref![data,0,2])); |
52 | | /// assert_eq!(4, read_u16(array_ref![data,4,2])); |
53 | | /// # } |
54 | | /// ``` |
55 | | |
56 | | #[macro_export] |
57 | | macro_rules! array_ref { |
58 | | ($arr:expr, $offset:expr, $len:expr) => {{ |
59 | | { |
60 | | #[inline] |
61 | 256k | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { |
62 | 256k | &*(slice.as_ptr() as *const [_; $len]) |
63 | 256k | } <opensk::ctap::hid::CtapHid<_>>::process_single_packet::as_array::<u8> Line | Count | Source | 61 | 48.0k | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 48.0k | &*(slice.as_ptr() as *const [_; $len]) | 63 | 48.0k | } |
<opensk::ctap::hid::CtapHid<_>>::process_single_packet::as_array::<u8> Line | Count | Source | 61 | 63.9k | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 63.9k | &*(slice.as_ptr() as *const [_; $len]) | 63 | 63.9k | } |
fuzz_helper::raw_to_message::as_array::<u8> Line | Count | Source | 61 | 12.1k | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 12.1k | &*(slice.as_ptr() as *const [_; $len]) | 63 | 12.1k | } |
Unexecuted instantiation: opensk::ctap::client_pin::check_and_store_new_pin::as_array::<u8> Unexecuted instantiation: opensk::ctap::client_pin::check_and_store_new_pin::as_array::<u8> opensk::api::key_store::get_master_keys::as_array::<u8> Line | Count | Source | 61 | 26.2k | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 26.2k | &*(slice.as_ptr() as *const [_; $len]) | 63 | 26.2k | } |
opensk::api::key_store::get_master_keys::as_array::<u8> Line | Count | Source | 61 | 26.2k | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 26.2k | &*(slice.as_ptr() as *const [_; $len]) | 63 | 26.2k | } |
opensk::api::key_store::get_master_keys::as_array::<u8> Line | Count | Source | 61 | 26.2k | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 26.2k | &*(slice.as_ptr() as *const [_; $len]) | 63 | 26.2k | } |
opensk::api::key_store::get_master_keys::as_array::<u8> Line | Count | Source | 61 | 26.2k | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 26.2k | &*(slice.as_ptr() as *const [_; $len]) | 63 | 26.2k | } |
<_ as opensk::api::key_store::KeyStore>::unwrap_credential::as_array::<u8> Line | Count | Source | 61 | 23.8k | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 23.8k | &*(slice.as_ptr() as *const [_; $len]) | 63 | 23.8k | } |
Unexecuted instantiation: opensk::api::persist::Persist::global_signature_counter::as_array::<u8> Unexecuted instantiation: opensk::api::persist::Persist::pin_hash::as_array::<u8> Unexecuted instantiation: opensk::ctap::crypto_wrapper::aes256_cbc_decrypt::as_array::<u8> opensk::ctap::crypto_wrapper::aes256_cbc_encrypt::as_array::<u8> Line | Count | Source | 61 | 2.38k | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 2.38k | &*(slice.as_ptr() as *const [_; $len]) | 63 | 2.38k | } |
Unexecuted instantiation: opensk::ctap::pin_protocol::verify_v1::as_array::<u8> Unexecuted instantiation: opensk::ctap::pin_protocol::verify_v2::as_array::<u8> <opensk::ctap::ctap1::U2fCommand as core::convert::TryFrom<&[u8]>>::try_from::as_array::<u8> Line | Count | Source | 61 | 9 | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 9 | &*(slice.as_ptr() as *const [_; $len]) | 63 | 9 | } |
<opensk::ctap::ctap1::U2fCommand as core::convert::TryFrom<&[u8]>>::try_from::as_array::<u8> Line | Count | Source | 61 | 284 | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 284 | &*(slice.as_ptr() as *const [_; $len]) | 63 | 284 | } |
<opensk::ctap::ctap1::U2fCommand as core::convert::TryFrom<&[u8]>>::try_from::as_array::<u8> Line | Count | Source | 61 | 284 | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 284 | &*(slice.as_ptr() as *const [_; $len]) | 63 | 284 | } |
<opensk::ctap::ctap1::U2fCommand as core::convert::TryFrom<&[u8]>>::try_from::as_array::<u8> Line | Count | Source | 61 | 9 | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 9 | &*(slice.as_ptr() as *const [_; $len]) | 63 | 9 | } |
<opensk::ctap::apdu::Apdu as core::convert::TryFrom<&[u8]>>::try_from::as_array::<u8> Line | Count | Source | 61 | 83 | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 83 | &*(slice.as_ptr() as *const [_; $len]) | 63 | 83 | } |
<opensk::ctap::apdu::Apdu as core::convert::TryFrom<&[u8]>>::try_from::as_array::<u8> Line | Count | Source | 61 | 228 | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 228 | &*(slice.as_ptr() as *const [_; $len]) | 63 | 228 | } |
<opensk::ctap::apdu::Apdu as core::convert::TryFrom<&[u8]>>::try_from::as_array::<u8> Line | Count | Source | 61 | 57 | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 57 | &*(slice.as_ptr() as *const [_; $len]) | 63 | 57 | } |
<opensk::ctap::apdu::Apdu as core::convert::TryFrom<&[u8]>>::try_from::as_array::<u8> Line | Count | Source | 61 | 413 | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 413 | &*(slice.as_ptr() as *const [_; $len]) | 63 | 413 | } |
<opensk::ctap::apdu::Apdu as core::convert::TryFrom<&[u8]>>::try_from::as_array::<u8> Line | Count | Source | 61 | 20 | const unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] { | 62 | 20 | &*(slice.as_ptr() as *const [_; $len]) | 63 | 20 | } |
|
64 | | let offset = $offset; |
65 | | let slice = &$arr[offset..offset + $len]; |
66 | | #[allow(unused_unsafe)] |
67 | | unsafe { |
68 | | as_array(slice) |
69 | | } |
70 | | } |
71 | | }}; |
72 | | } |
73 | | |
74 | | /// You can use `array_refs` to generate a series of array references |
75 | | /// to an input array reference. The idea is if you want to break an |
76 | | /// array into a series of contiguous and non-overlapping arrays. |
77 | | /// `array_refs` is a bit funny in that it insists on slicing up the |
78 | | /// *entire* array. This is intentional, as I find it handy to make |
79 | | /// me ensure that my sub-arrays add up to the entire array. This |
80 | | /// macro will *never* panic, since the sizes are all checked at |
81 | | /// compile time. |
82 | | /// |
83 | | /// Note that unlike `array_ref!`, `array_refs` *requires* that the |
84 | | /// first argument be an array reference. The following arguments are |
85 | | /// the lengths of each subarray you wish a reference to. The total |
86 | | /// of these arguments *must* equal the size of the array itself. |
87 | | /// |
88 | | /// ``` |
89 | | /// #[macro_use] |
90 | | /// extern crate arrayref; |
91 | | /// |
92 | | /// fn read_u16(bytes: &[u8; 2]) -> u16 { |
93 | | /// bytes[0] as u16 + ((bytes[1] as u16) << 8) |
94 | | /// } |
95 | | /// // ... |
96 | | /// # fn main() { |
97 | | /// let data = [0,1,2,3,4,0,6,7]; |
98 | | /// let (a,b,c) = array_refs![&data,2,2,4]; |
99 | | /// assert_eq!(read_u16(a), 256); |
100 | | /// assert_eq!(read_u16(b), 3*256+2); |
101 | | /// assert_eq!(*c, [4,0,6,7]); |
102 | | /// # } |
103 | | /// ``` |
104 | | #[macro_export] |
105 | | macro_rules! array_refs { |
106 | | ( $arr:expr, $( $pre:expr ),* ; .. ; $( $post:expr ),* ) => {{ |
107 | | { |
108 | | use core::slice; |
109 | | #[inline] |
110 | | #[allow(unused_assignments)] |
111 | | #[allow(clippy::eval_order_dependence)] |
112 | | const unsafe fn as_arrays<T>(a: &[T]) -> ( $( &[T; $pre], )* &[T], $( &[T; $post], )*) { |
113 | | const MIN_LEN: usize = 0usize $( .saturating_add($pre) )* $( .saturating_add($post) )*; |
114 | | assert!(MIN_LEN < usize::MAX, "Your arrays are too big, are you trying to hack yourself?!"); |
115 | | let var_len = a.len() - MIN_LEN; |
116 | | assert!(a.len() >= MIN_LEN); |
117 | | let mut p = a.as_ptr(); |
118 | | ( $( { |
119 | | let aref = & *(p as *const [T; $pre]); |
120 | | p = p.add($pre); |
121 | | aref |
122 | | }, )* { |
123 | | let sl = slice::from_raw_parts(p as *const T, var_len); |
124 | | p = p.add(var_len); |
125 | | sl |
126 | | }, $( { |
127 | | let aref = & *(p as *const [T; $post]); |
128 | | p = p.add($post); |
129 | | aref |
130 | | }, )*) |
131 | | } |
132 | | let input = $arr; |
133 | | #[allow(unused_unsafe)] |
134 | | unsafe { |
135 | | as_arrays(input) |
136 | | } |
137 | | } |
138 | | }}; |
139 | | ( $arr:expr, $( $len:expr ),* ) => {{ |
140 | | { |
141 | | #[inline] |
142 | | #[allow(unused_assignments)] |
143 | | #[allow(clippy::eval_order_dependence)] |
144 | 111k | const unsafe fn as_arrays<T>(a: &[T; $( $len + )* 0 ]) -> ( $( &[T; $len], )* ) { |
145 | 111k | let mut p = a.as_ptr(); |
146 | 111k | ( $( { |
147 | 111k | let aref = &*(p as *const [T; $len]); |
148 | 111k | p = p.offset($len as isize); |
149 | 111k | aref |
150 | 111k | }, )* ) |
151 | 111k | } |
152 | | let input = $arr; |
153 | | #[allow(unused_unsafe)] |
154 | | unsafe { |
155 | | as_arrays(input) |
156 | | } |
157 | | } |
158 | | }} |
159 | | } |
160 | | |
161 | | /// You can use `mut_array_refs` to generate a series of mutable array |
162 | | /// references to an input mutable array reference. The idea is if |
163 | | /// you want to break an array into a series of contiguous and |
164 | | /// non-overlapping mutable array references. Like `array_refs!`, |
165 | | /// `mut_array_refs!` is a bit funny in that it insists on slicing up |
166 | | /// the *entire* array. This is intentional, as I find it handy to |
167 | | /// make me ensure that my sub-arrays add up to the entire array. |
168 | | /// This macro will *never* panic, since the sizes are all checked at |
169 | | /// compile time. |
170 | | /// |
171 | | /// Note that unlike `array_mut_ref!`, `mut_array_refs` *requires* |
172 | | /// that the first argument be a mutable array reference. The |
173 | | /// following arguments are the lengths of each subarray you wish a |
174 | | /// reference to. The total of these arguments *must* equal the size |
175 | | /// of the array itself. Also note that this macro allows you to take |
176 | | /// out multiple mutable references to a single object, which is both |
177 | | /// weird and powerful. |
178 | | /// |
179 | | /// ``` |
180 | | /// #[macro_use] |
181 | | /// extern crate arrayref; |
182 | | /// |
183 | | /// fn write_u16(bytes: &mut [u8; 2], num: u16) { |
184 | | /// bytes[0] = num as u8; |
185 | | /// bytes[1] = (num >> 8) as u8; |
186 | | /// } |
187 | | /// fn write_u32(bytes: &mut [u8; 4], num: u32) { |
188 | | /// bytes[0] = num as u8; |
189 | | /// bytes[1] = (num >> 8) as u8; // this is buggy to save space... |
190 | | /// } |
191 | | /// // ... |
192 | | /// # fn main() { |
193 | | /// let mut data = [0,1,2,3,4,0,6,7]; |
194 | | /// let (a,b,c) = mut_array_refs![&mut data,2,2,4]; |
195 | | /// // let's write out some nice prime numbers! |
196 | | /// write_u16(a, 37); |
197 | | /// write_u16(b, 73); |
198 | | /// write_u32(c, 137); // approximate inverse of the fine structure constant! |
199 | | /// # } |
200 | | /// ``` |
201 | | #[macro_export] |
202 | | macro_rules! mut_array_refs { |
203 | | ( $arr:expr, $( $pre:expr ),* ; .. ; $( $post:expr ),* ) => {{ |
204 | | { |
205 | | use core::slice; |
206 | | #[inline] |
207 | | #[allow(unused_assignments)] |
208 | | #[allow(clippy::eval_order_dependence)] |
209 | | unsafe fn as_arrays<T>(a: &mut [T]) -> ( $( &mut [T; $pre], )* &mut [T], $( &mut [T; $post], )*) { |
210 | | const MIN_LEN: usize = 0usize $( .saturating_add($pre) )* $( .saturating_add($post) )*; |
211 | | assert!(MIN_LEN < usize::MAX, "Your arrays are too big, are you trying to hack yourself?!"); |
212 | | let var_len = a.len() - MIN_LEN; |
213 | | assert!(a.len() >= MIN_LEN); |
214 | | let mut p = a.as_mut_ptr(); |
215 | | ( $( { |
216 | | let aref = &mut *(p as *mut [T; $pre]); |
217 | | p = p.add($pre); |
218 | | aref |
219 | | }, )* { |
220 | | let sl = slice::from_raw_parts_mut(p as *mut T, var_len); |
221 | | p = p.add(var_len); |
222 | | sl |
223 | | }, $( { |
224 | | let aref = &mut *(p as *mut [T; $post]); |
225 | | p = p.add($post); |
226 | | aref |
227 | | }, )*) |
228 | | } |
229 | | let input = $arr; |
230 | | #[allow(unused_unsafe)] |
231 | | unsafe { |
232 | | as_arrays(input) |
233 | | } |
234 | | } |
235 | | }}; |
236 | | ( $arr:expr, $( $len:expr ),* ) => {{ |
237 | | { |
238 | | #[inline] |
239 | | #[allow(unused_assignments)] |
240 | | #[allow(clippy::eval_order_dependence)] |
241 | 0 | unsafe fn as_arrays<T>(a: &mut [T; $( $len + )* 0 ]) -> ( $( &mut [T; $len], )* ) { |
242 | 0 | let mut p = a.as_mut_ptr(); |
243 | 0 | ( $( { |
244 | 0 | let aref = &mut *(p as *mut [T; $len]); |
245 | 0 | p = p.add($len); |
246 | 0 | aref |
247 | 0 | }, )* ) |
248 | 0 | } |
249 | | let input = $arr; |
250 | | #[allow(unused_unsafe)] |
251 | | unsafe { |
252 | | as_arrays(input) |
253 | | } |
254 | | } |
255 | | }}; |
256 | | } |
257 | | |
258 | | /// You can use `array_mut_ref` to generate a mutable array reference |
259 | | /// to a subset of a sliceable bit of data (which could be an array, |
260 | | /// or a slice, or a Vec). |
261 | | /// |
262 | | /// **Panics** if the slice is out of bounds. |
263 | | /// |
264 | | /// ``` |
265 | | /// #[macro_use] |
266 | | /// extern crate arrayref; |
267 | | /// |
268 | | /// fn write_u16(bytes: &mut [u8; 2], num: u16) { |
269 | | /// bytes[0] = num as u8; |
270 | | /// bytes[1] = (num >> 8) as u8; |
271 | | /// } |
272 | | /// // ... |
273 | | /// # fn main() { |
274 | | /// let mut data = [0,1,2,3,4,0,6,7,8,9]; |
275 | | /// write_u16(array_mut_ref![data,0,2], 1); |
276 | | /// write_u16(array_mut_ref![data,2,2], 5); |
277 | | /// assert_eq!(*array_ref![data,0,4], [1,0,5,0]); |
278 | | /// *array_mut_ref![data,4,5] = [4,3,2,1,0]; |
279 | | /// assert_eq!(data, [1,0,5,0,4,3,2,1,0,9]); |
280 | | /// # } |
281 | | /// ``` |
282 | | #[macro_export] |
283 | | macro_rules! array_mut_ref { |
284 | | ($arr:expr, $offset:expr, $len:expr) => {{ |
285 | | { |
286 | | #[inline] |
287 | 0 | unsafe fn as_array<T>(slice: &mut [T]) -> &mut [T; $len] { |
288 | 0 | &mut *(slice.as_mut_ptr() as *mut [_; $len]) |
289 | 0 | } Unexecuted instantiation: <opensk::ctap::client_pin::ClientPin<_>>::process_hmac_secret::as_array::<u8> Unexecuted instantiation: <opensk::ctap::client_pin::ClientPin<_>>::process_hmac_secret::as_array::<u8> |
290 | | let offset = $offset; |
291 | | let slice = &mut $arr[offset..offset + $len]; |
292 | | #[allow(unused_unsafe)] |
293 | | unsafe { |
294 | | as_array(slice) |
295 | | } |
296 | | } |
297 | | }}; |
298 | | } |
299 | | |
300 | | #[allow(clippy::all)] |
301 | | #[cfg(test)] |
302 | | mod test { |
303 | | |
304 | | extern crate quickcheck; |
305 | | |
306 | | use std::vec::Vec; |
307 | | |
308 | | // use super::*; |
309 | | |
310 | | #[test] |
311 | | #[should_panic] |
312 | | fn checks_bounds() { |
313 | | let foo: [u8; 11] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
314 | | let bar = array_ref!(foo, 1, 11); |
315 | | println!("I am checking that I can dereference bar[0] = {}", bar[0]); |
316 | | } |
317 | | |
318 | | #[test] |
319 | | fn simple_case_works() { |
320 | | fn check(expected: [u8; 3], actual: &[u8; 3]) { |
321 | | for (e, a) in (&expected).iter().zip(actual.iter()) { |
322 | | assert_eq!(e, a) |
323 | | } |
324 | | } |
325 | | let mut foo: [u8; 11] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
326 | | { |
327 | | let bar = array_ref!(foo, 2, 3); |
328 | | check([2, 3, 4], bar); |
329 | | } |
330 | | check([0, 1, 2], array_ref!(foo, 0, 3)); |
331 | | fn zero2(x: &mut [u8; 2]) { |
332 | | x[0] = 0; |
333 | | x[1] = 0; |
334 | | } |
335 | | zero2(array_mut_ref!(foo, 8, 2)); |
336 | | check([0, 0, 10], array_ref!(foo, 8, 3)); |
337 | | } |
338 | | |
339 | | #[test] |
340 | | fn check_array_ref_5() { |
341 | | fn f(data: Vec<u8>, offset: usize) -> quickcheck::TestResult { |
342 | | // Compute the following, with correct results even if the sum would overflow: |
343 | | // if data.len() < offset + 5 |
344 | | if data.len() < 5 || data.len() - 5 < offset { |
345 | | return quickcheck::TestResult::discard(); |
346 | | } |
347 | | let out = array_ref!(data, offset, 5); |
348 | | quickcheck::TestResult::from_bool(out.len() == 5) |
349 | | } |
350 | | quickcheck::quickcheck(f as fn(Vec<u8>, usize) -> quickcheck::TestResult); |
351 | | } |
352 | | |
353 | | #[test] |
354 | | fn check_array_ref_out_of_bounds_5() { |
355 | | fn f(data: Vec<u8>, offset: usize) -> quickcheck::TestResult { |
356 | | // Compute the following, with correct results even if the sum would overflow: |
357 | | // if data.len() >= offset + 5 |
358 | | if data.len() >= 5 && data.len() - 5 >= offset { |
359 | | return quickcheck::TestResult::discard(); |
360 | | } |
361 | | quickcheck::TestResult::must_fail(move || { |
362 | | array_ref!(data, offset, 5); |
363 | | }) |
364 | | } |
365 | | quickcheck::quickcheck(f as fn(Vec<u8>, usize) -> quickcheck::TestResult); |
366 | | } |
367 | | |
368 | | #[test] |
369 | | fn check_array_mut_ref_7() { |
370 | | fn f(mut data: Vec<u8>, offset: usize) -> quickcheck::TestResult { |
371 | | // Compute the following, with correct results even if the sum would overflow: |
372 | | // if data.len() < offset + 7 |
373 | | if data.len() < 7 || data.len() - 7 < offset { |
374 | | return quickcheck::TestResult::discard(); |
375 | | } |
376 | | let out = array_mut_ref!(data, offset, 7); |
377 | | out[6] = 3; |
378 | | quickcheck::TestResult::from_bool(out.len() == 7) |
379 | | } |
380 | | quickcheck::quickcheck(f as fn(Vec<u8>, usize) -> quickcheck::TestResult); |
381 | | } |
382 | | |
383 | | #[test] |
384 | | fn check_array_mut_ref_out_of_bounds_32() { |
385 | | fn f(mut data: Vec<u8>, offset: usize) -> quickcheck::TestResult { |
386 | | // Compute the following, with correct results even if the sum would overflow: |
387 | | // if data.len() >= offset + 32 |
388 | | if data.len() >= 32 && data.len() - 32 >= offset { |
389 | | return quickcheck::TestResult::discard(); |
390 | | } |
391 | | quickcheck::TestResult::must_fail(move || { |
392 | | array_mut_ref!(data, offset, 32); |
393 | | }) |
394 | | } |
395 | | quickcheck::quickcheck(f as fn(Vec<u8>, usize) -> quickcheck::TestResult); |
396 | | } |
397 | | |
398 | | #[test] |
399 | | fn test_5_array_refs() { |
400 | | let mut data: [usize; 128] = [0; 128]; |
401 | | for i in 0..128 { |
402 | | data[i] = i; |
403 | | } |
404 | | let data = data; |
405 | | let (a, b, c, d, e) = array_refs!(&data, 1, 14, 3, 100, 10); |
406 | | assert_eq!(a.len(), 1 as usize); |
407 | | assert_eq!(b.len(), 14 as usize); |
408 | | assert_eq!(c.len(), 3 as usize); |
409 | | assert_eq!(d.len(), 100 as usize); |
410 | | assert_eq!(e.len(), 10 as usize); |
411 | | assert_eq!(a, array_ref![data, 0, 1]); |
412 | | assert_eq!(b, array_ref![data, 1, 14]); |
413 | | assert_eq!(c, array_ref![data, 15, 3]); |
414 | | assert_eq!(e, array_ref![data, 118, 10]); |
415 | | } |
416 | | |
417 | | #[test] |
418 | | fn test_5_array_refs_dotdot() { |
419 | | let mut data: [usize; 128] = [0; 128]; |
420 | | for i in 0..128 { |
421 | | data[i] = i; |
422 | | } |
423 | | let data = data; |
424 | | let (a, b, c, d, e) = array_refs!(&data, 1, 14, 3; ..; 10); |
425 | | assert_eq!(a.len(), 1 as usize); |
426 | | assert_eq!(b.len(), 14 as usize); |
427 | | assert_eq!(c.len(), 3 as usize); |
428 | | assert_eq!(d.len(), 100 as usize); |
429 | | assert_eq!(e.len(), 10 as usize); |
430 | | assert_eq!(a, array_ref![data, 0, 1]); |
431 | | assert_eq!(b, array_ref![data, 1, 14]); |
432 | | assert_eq!(c, array_ref![data, 15, 3]); |
433 | | assert_eq!(e, array_ref![data, 118, 10]); |
434 | | } |
435 | | |
436 | | #[test] |
437 | | fn test_5_mut_xarray_refs() { |
438 | | let mut data: [usize; 128] = [0; 128]; |
439 | | { |
440 | | // temporarily borrow the data to modify it. |
441 | | let (a, b, c, d, e) = mut_array_refs!(&mut data, 1, 14, 3, 100, 10); |
442 | | assert_eq!(a.len(), 1 as usize); |
443 | | assert_eq!(b.len(), 14 as usize); |
444 | | assert_eq!(c.len(), 3 as usize); |
445 | | assert_eq!(d.len(), 100 as usize); |
446 | | assert_eq!(e.len(), 10 as usize); |
447 | | *a = [1; 1]; |
448 | | *b = [14; 14]; |
449 | | *c = [3; 3]; |
450 | | *d = [100; 100]; |
451 | | *e = [10; 10]; |
452 | | } |
453 | | assert_eq!(&[1; 1], array_ref![data, 0, 1]); |
454 | | assert_eq!(&[14; 14], array_ref![data, 1, 14]); |
455 | | assert_eq!(&[3; 3], array_ref![data, 15, 3]); |
456 | | assert_eq!(&[10; 10], array_ref![data, 118, 10]); |
457 | | } |
458 | | |
459 | | #[test] |
460 | | fn test_5_mut_xarray_refs_with_dotdot() { |
461 | | let mut data: [usize; 128] = [0; 128]; |
462 | | { |
463 | | // temporarily borrow the data to modify it. |
464 | | let (a, b, c, d, e) = mut_array_refs!(&mut data, 1, 14, 3; ..; 10); |
465 | | assert_eq!(a.len(), 1 as usize); |
466 | | assert_eq!(b.len(), 14 as usize); |
467 | | assert_eq!(c.len(), 3 as usize); |
468 | | assert_eq!(d.len(), 100 as usize); |
469 | | assert_eq!(e.len(), 10 as usize); |
470 | | *a = [1; 1]; |
471 | | *b = [14; 14]; |
472 | | *c = [3; 3]; |
473 | | *e = [10; 10]; |
474 | | } |
475 | | assert_eq!(&[1; 1], array_ref![data, 0, 1]); |
476 | | assert_eq!(&[14; 14], array_ref![data, 1, 14]); |
477 | | assert_eq!(&[3; 3], array_ref![data, 15, 3]); |
478 | | assert_eq!(&[10; 10], array_ref![data, 118, 10]); |
479 | | } |
480 | | |
481 | | #[forbid(clippy::ptr_offset_with_cast)] |
482 | | #[test] |
483 | | fn forbidden_clippy_lints_do_not_fire() { |
484 | | let mut data = [0u8; 32]; |
485 | | let _ = array_refs![&data, 8; .. ;]; |
486 | | let _ = mut_array_refs![&mut data, 8; .. ; 10]; |
487 | | } |
488 | | |
489 | | #[test] |
490 | | fn single_arg_refs() { |
491 | | let mut data = [0u8; 8]; |
492 | | let (_,) = array_refs![&data, 8]; |
493 | | let (_,) = mut_array_refs![&mut data, 8]; |
494 | | |
495 | | let (_, _) = array_refs![&data, 4; ..;]; |
496 | | let (_, _) = mut_array_refs![&mut data, 4; ..;]; |
497 | | |
498 | | let (_, _) = array_refs![&data,; ..; 4]; |
499 | | let (_, _) = mut_array_refs![&mut data,; ..; 4]; |
500 | | |
501 | | let (_,) = array_refs![&data,; ..;]; |
502 | | let (_,) = mut_array_refs![&mut data,; ..;]; |
503 | | } |
504 | | } // mod test |