/rust/registry/src/index.crates.io-1949cf8c6b5b557f/blake2-0.10.6/src/as_bytes.rs
Line | Count | Source |
1 | | // Copyright 2016 blake2-rfc Developers |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or |
4 | | // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
5 | | // http://opensource.org/licenses/MIT>, at your option. This file may not be |
6 | | // copied, modified, or distributed except according to those terms. |
7 | | |
8 | | use core::mem; |
9 | | use core::slice; |
10 | | |
11 | | #[allow(clippy::missing_safety_doc)] |
12 | | pub unsafe trait Safe {} |
13 | | |
14 | | pub trait AsBytes { |
15 | | fn as_bytes(&self) -> &[u8]; |
16 | | fn as_mut_bytes(&mut self) -> &mut [u8]; |
17 | | } |
18 | | |
19 | | impl<T: Safe> AsBytes for [T] { |
20 | | #[inline] |
21 | 64 | fn as_bytes(&self) -> &[u8] { |
22 | | unsafe { |
23 | 64 | slice::from_raw_parts(self.as_ptr() as *const u8, self.len() * mem::size_of::<T>()) |
24 | | } |
25 | 64 | } Unexecuted instantiation: <[blake2::simd::simdty::Simd4<u32>] as blake2::as_bytes::AsBytes>::as_bytes <[blake2::simd::simdty::Simd4<u64>] as blake2::as_bytes::AsBytes>::as_bytes Line | Count | Source | 21 | 64 | fn as_bytes(&self) -> &[u8] { | 22 | | unsafe { | 23 | 64 | slice::from_raw_parts(self.as_ptr() as *const u8, self.len() * mem::size_of::<T>()) | 24 | | } | 25 | 64 | } |
|
26 | | |
27 | | #[inline] |
28 | 0 | fn as_mut_bytes(&mut self) -> &mut [u8] { |
29 | 0 | unsafe { |
30 | 0 | slice::from_raw_parts_mut( |
31 | 0 | self.as_mut_ptr() as *mut u8, |
32 | 0 | self.len() * mem::size_of::<T>(), |
33 | 0 | ) |
34 | 0 | } |
35 | 0 | } |
36 | | } |
37 | | |
38 | | unsafe impl Safe for u8 {} |
39 | | unsafe impl Safe for u16 {} |
40 | | unsafe impl Safe for u32 {} |
41 | | unsafe impl Safe for u64 {} |
42 | | unsafe impl Safe for i8 {} |
43 | | unsafe impl Safe for i16 {} |
44 | | unsafe impl Safe for i32 {} |
45 | | unsafe impl Safe for i64 {} |