Debian Codegraph @

debian/missing-sources/rand_core/src/le.rs

raw symbols SCIP
1 // Copyright 2018 Developers of the Rand project.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 //! Little-Endian utilities
10 //!
11 //! Little-Endian order has been chosen for internal usage; this makes some
12 //! useful functions available.
13
14 /// Reads unsigned 32 bit integers from `src` into `dst`.
15 ///
16 /// # Panics
17 ///
18 /// If `dst` has insufficient space (`4*dst.len() < src.len()`).
19 #[inline]
20 #[track_caller]
21 pub fn read_u32_intorust-analyzer cargo rand_core 0.9.5 le/read_u32_into().(src: &[u8], dst: &mut [u32]) {
22 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(src.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() >= 4 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). dst.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().());
23 for (out, chunk) in dst.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(src.chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(4)) {
24 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().out = u32::from_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_le_bytes().(chunk.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryInto#try_into().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
25 }
26 }
27
28 /// Reads unsigned 64 bit integers from `src` into `dst`.
29 ///
30 /// # Panics
31 ///
32 /// If `dst` has insufficient space (`8*dst.len() < src.len()`).
33 #[inline]
34 #[track_caller]
35 pub fn read_u64_intorust-analyzer cargo rand_core 0.9.5 le/read_u64_into().(src: &[u8], dst: &mut [u64]) {
36 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(src.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() >= 8 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). dst.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().());
37 for (out, chunk) in dst.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(src.chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(8)) {
38 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().out = u64::from_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]from_le_bytes().(chunk.try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryInto#try_into().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
39 }
40 }
41
42 #[test]
43 fn test_read() {
44 let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
45
46 let mut buf = [0u32; 4];
47 read_u32_intorust-analyzer cargo rand_core 0.9.5 le/read_u32_into().(&bytes, &mut buf);
48 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf[0], 0x04030201);
49 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf[3], 0x100F0E0D);
50
51 let mut buf = [0u32; 3];
52 read_u32_intorust-analyzer cargo rand_core 0.9.5 le/read_u32_into().(&bytes[1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#13], &mut buf); // unaligned
53 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf[0], 0x05040302);
54 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf[2], 0x0D0C0B0A);
55
56 let mut buf = [0u64; 2];
57 read_u64_intorust-analyzer cargo rand_core 0.9.5 le/read_u64_into().(&bytes, &mut buf);
58 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf[0], 0x0807060504030201);
59 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf[1], 0x100F0E0D0C0B0A09);
60
61 let mut buf = [0u64; 1];
62 read_u64_intorust-analyzer cargo rand_core 0.9.5 le/read_u64_into().(&bytes[7..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#15], &mut buf); // unaligned
63 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(buf[0], 0x0F0E0D0C0B0A0908);
64 }