rust: unsafe as_bytes_mut instead of AsMut
Unsafe access should be propagated, so the caller has to mark their
code as unsafe and ensure they do only safe operations.
@@ -30,7 +30,7 @@ pub extern "C" fn rust_util_all_ascii(cstr: CStr) -> bool {
#[no_mangle]
pub extern "C" fn rust_util_uint8_to_hex(buf: Bytes, mut out: CStrMut) {
// UNSAFE: We promise that we never write non-utf8 valid bytes to the `str`.
- let out = out.as_mut();
+ let out = unsafe { out.as_bytes_mut() };
let out_len = out.len();
hex::encode_to_slice(&buf, &mut out[0..out_len - 1]).unwrap();
out[out_len - 1] = b'\0';
@@ -125,17 +125,17 @@ impl AsRef<str> for CStrMut {
}
}
-impl AsMut<[u8]> for CStrMut {
+impl CStrMut {
/// Create a slice to a buffer. Only allowed for non-null pointers with length or null pointers
/// with 0 length due to limitation in `core::slice`.
/// The caller must ensure that the string is null terminated.
- fn as_mut(&mut self) -> &mut [u8] {
+ pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
let mut buf = self.buf;
if self.len == 0 && self.buf.is_null() {
buf = core::ptr::NonNull::dangling().as_ptr();
}
assert!(!buf.is_null());
- unsafe { core::slice::from_raw_parts_mut(buf, self.len) }
+ core::slice::from_raw_parts_mut(buf, self.len)
}
}
