]> git.proxmox.com Git - rustc.git/blame - vendor/byte-tools/src/write_single.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / vendor / byte-tools / src / write_single.rs
CommitLineData
9fa01778
XL
1use core::{mem, ptr};
2
3macro_rules! write_single {
4 ($dst:expr, $n:expr, $size:expr, $which:ident) => ({
5 assert!($size == $dst.len());
6 unsafe {
7 let bytes = mem::transmute::<_, [u8; $size]>($n.$which());
8 ptr::copy_nonoverlapping((&bytes).as_ptr(), $dst.as_mut_ptr(), $size);
9 }
10 });
11}
12
13/// Write a u32 into a vector, which must be 4 bytes long. The value is written
14/// in little-endian format.
15#[inline]
16pub fn write_u32_le(dst: &mut [u8], n: u32) {
17 write_single!(dst, n, 4, to_le);
18}
19
20/// Write a u32 into a vector, which must be 4 bytes long. The value is written
21/// in big-endian format.
22#[inline]
23pub fn write_u32_be(dst: &mut [u8], n: u32) {
24 write_single!(dst, n, 4, to_be);
25}
26
27/// Write a u64 into a vector, which must be 8 bytes long. The value is written
28/// in little-endian format.
29#[inline]
30pub fn write_u64_le(dst: &mut [u8], n: u64) {
31 write_single!(dst, n, 8, to_le);
32}
33
34/// Write a u64 into a vector, which must be 8 bytes long. The value is written
35/// in big-endian format.
36#[inline]
37pub fn write_u64_be(dst: &mut [u8], n: u64) {
38 write_single!(dst, n, 8, to_be);
39}