Skip to content

Commit 1e2d8b0

Browse files
mina86BurntSushi
authored andcommitted
impl: eliminate unsafe_write_num_bytes
unsafe_write_num_bytes macro can be replaced by a perfectly safe code which uses `n::to_xx_bytes()` to convert given number to desired representation and `buf[..N].copy_from_slice(...)` which writes that into the buffer. Closes #193
1 parent 368cb55 commit 1e2d8b0

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

src/lib.rs

+8-25
Original file line numberDiff line numberDiff line change
@@ -1898,23 +1898,6 @@ pub type NativeEndian = LittleEndian;
18981898
#[cfg(target_endian = "big")]
18991899
pub type NativeEndian = BigEndian;
19001900

1901-
/// Copies $size bytes from a number $n to a &mut [u8] $dst. $ty represents the
1902-
/// numeric type of $n and $which must be either to_be or to_le, depending on
1903-
/// which endianness one wants to use when writing to $dst.
1904-
///
1905-
/// This macro is only safe to call when $ty is a numeric type and $size ==
1906-
/// size_of::<$ty>() and where $dst is a &mut [u8].
1907-
macro_rules! unsafe_write_num_bytes {
1908-
($ty:ty, $size:expr, $n:expr, $dst:expr, $which:ident) => {{
1909-
assert!($size <= $dst.len());
1910-
unsafe {
1911-
// N.B. https://github.com/rust-lang/rust/issues/22776
1912-
let bytes = *(&$n.$which() as *const _ as *const [u8; $size]);
1913-
copy_nonoverlapping((&bytes).as_ptr(), $dst.as_mut_ptr(), $size);
1914-
}
1915-
}};
1916-
}
1917-
19181901
/// Copies a &[u8] $src into a &mut [$ty] $dst for the endianness given by
19191902
/// $from_bytes (must be either from_be_bytes or from_le_bytes).
19201903
///
@@ -2002,22 +1985,22 @@ impl ByteOrder for BigEndian {
20021985

20031986
#[inline]
20041987
fn write_u16(buf: &mut [u8], n: u16) {
2005-
unsafe_write_num_bytes!(u16, 2, n, buf, to_be);
1988+
buf[..2].copy_from_slice(&n.to_be_bytes());
20061989
}
20071990

20081991
#[inline]
20091992
fn write_u32(buf: &mut [u8], n: u32) {
2010-
unsafe_write_num_bytes!(u32, 4, n, buf, to_be);
1993+
buf[..4].copy_from_slice(&n.to_be_bytes());
20111994
}
20121995

20131996
#[inline]
20141997
fn write_u64(buf: &mut [u8], n: u64) {
2015-
unsafe_write_num_bytes!(u64, 8, n, buf, to_be);
1998+
buf[..8].copy_from_slice(&n.to_be_bytes());
20161999
}
20172000

20182001
#[inline]
20192002
fn write_u128(buf: &mut [u8], n: u128) {
2020-
unsafe_write_num_bytes!(u128, 16, n, buf, to_be);
2003+
buf[..16].copy_from_slice(&n.to_be_bytes());
20212004
}
20222005

20232006
#[inline]
@@ -2194,22 +2177,22 @@ impl ByteOrder for LittleEndian {
21942177

21952178
#[inline]
21962179
fn write_u16(buf: &mut [u8], n: u16) {
2197-
unsafe_write_num_bytes!(u16, 2, n, buf, to_le);
2180+
buf[..2].copy_from_slice(&n.to_le_bytes());
21982181
}
21992182

22002183
#[inline]
22012184
fn write_u32(buf: &mut [u8], n: u32) {
2202-
unsafe_write_num_bytes!(u32, 4, n, buf, to_le);
2185+
buf[..4].copy_from_slice(&n.to_le_bytes());
22032186
}
22042187

22052188
#[inline]
22062189
fn write_u64(buf: &mut [u8], n: u64) {
2207-
unsafe_write_num_bytes!(u64, 8, n, buf, to_le);
2190+
buf[..8].copy_from_slice(&n.to_le_bytes());
22082191
}
22092192

22102193
#[inline]
22112194
fn write_u128(buf: &mut [u8], n: u128) {
2212-
unsafe_write_num_bytes!(u128, 16, n, buf, to_le);
2195+
buf[..16].copy_from_slice(&n.to_le_bytes());
22132196
}
22142197

22152198
#[inline]

0 commit comments

Comments
 (0)