Skip to content

Commit 8cb9e97

Browse files
committed
replace hex-simd with hex and fix CI
1 parent 39e26c6 commit 8cb9e97

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ clippy-utilities = "0.1.0"
1616
enumflags2 = "0.7.3"
1717
errno = "0.2.7"
1818
futures = "0.3.17"
19-
hex-simd = "0.5.0"
19+
hex = "0.4.3"
2020
lazy_static = "1.4.0"
2121
libc = "0.2.104"
2222
lockfree-cuckoohash = "0.1.0"

src/device.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rdma_sys::{ibv_get_device_guid, ibv_get_device_name};
77

88
use std::ffi::CStr;
99
use std::io;
10-
use std::mem::MaybeUninit;
1110
use std::ops::Deref;
1211
use std::os::raw::c_int;
1312
use std::ptr::NonNull;
@@ -181,35 +180,36 @@ impl fmt::Debug for Guid {
181180
}
182181

183182
/// Encodes a guid to a hex string and process it
184-
fn guid_to_hex<R>(guid: Guid, case: hex_simd::AsciiCase, f: impl FnOnce(&str) -> R) -> R {
183+
fn guid_to_hex<R>(guid: Guid, uppercase: bool, f: impl FnOnce(&str) -> R) -> R {
185184
let src: &[u8; 8] = guid.as_bytes();
186-
let mut buf: MaybeUninit<[u8; 16]> = MaybeUninit::uninit();
187-
let ans = {
188-
// SAFETY: uninit project
189-
let bytes = unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr().cast(), 16) };
190-
let dst = hex_simd::OutBuf::from_uninit_mut(bytes);
191-
let result = hex_simd::encode_as_str(src, dst, case);
192-
// SAFETY: the encoding never fails
193-
unsafe { result.unwrap_unchecked() }
185+
let mut buf: [u8; 16] = [0; 16];
186+
// SAFETY: The buf is two times of src, which is required by hex::encode_to_slice.
187+
// Therefore, the unwrap_unchecked on hex::encode_to_slice is safe.
188+
// After the hex encoding, the bytes in buf are valid UTF-8, because hex::encode_to_slice
189+
// only produces bytes in the ASCII range (0x00 - 0x7F), which are valid UTF-8.
190+
// Therefore, the unwrap_unchecked on std::str::from_utf8 is also safe.
191+
let ans = unsafe {
192+
hex::encode_to_slice(src, &mut buf).unwrap_unchecked();
193+
if uppercase {
194+
std::str::from_utf8(&buf).unwrap_unchecked().to_uppercase()
195+
} else {
196+
std::str::from_utf8(&buf).unwrap_unchecked().to_lowercase()
197+
}
194198
};
195-
f(ans)
199+
f(&ans)
196200
}
197201

198202
impl fmt::LowerHex for Guid {
199203
#[inline]
200204
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201-
guid_to_hex(*self, hex_simd::AsciiCase::Lower, |s| {
202-
<str as fmt::Display>::fmt(s, f)
203-
})
205+
guid_to_hex(*self, false, |s| <str as fmt::Display>::fmt(s, f))
204206
}
205207
}
206208

207209
impl fmt::UpperHex for Guid {
208210
#[inline]
209211
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
210-
guid_to_hex(*self, hex_simd::AsciiCase::Upper, |s| {
211-
<str as fmt::Display>::fmt(s, f)
212-
})
212+
guid_to_hex(*self, true, |s| <str as fmt::Display>::fmt(s, f))
213213
}
214214
}
215215

0 commit comments

Comments
 (0)