mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
std: Unsafe-wrap in Wtf8 impl
This commit is contained in:
parent
df353a0cc3
commit
64fb2366da
@ -16,7 +16,6 @@
|
||||
|
||||
#![allow(missing_docs)]
|
||||
#![allow(missing_debug_implementations)]
|
||||
#![allow(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
@ -602,7 +602,8 @@ impl Wtf8 {
|
||||
/// marked unsafe.
|
||||
#[inline]
|
||||
pub unsafe fn from_bytes_unchecked(value: &[u8]) -> &Wtf8 {
|
||||
mem::transmute(value)
|
||||
// SAFETY: start with &[u8], end with fancy &[u8]
|
||||
unsafe { &*(value as *const [u8] as *const Wtf8) }
|
||||
}
|
||||
|
||||
/// Creates a mutable WTF-8 slice from a mutable WTF-8 byte slice.
|
||||
@ -611,7 +612,8 @@ impl Wtf8 {
|
||||
/// marked unsafe.
|
||||
#[inline]
|
||||
unsafe fn from_mut_bytes_unchecked(value: &mut [u8]) -> &mut Wtf8 {
|
||||
mem::transmute(value)
|
||||
// SAFETY: start with &mut [u8], end with fancy &mut [u8]
|
||||
unsafe { &mut *(value as *mut [u8] as *mut Wtf8) }
|
||||
}
|
||||
|
||||
/// Returns the length, in WTF-8 bytes.
|
||||
@ -942,8 +944,12 @@ pub fn check_utf8_boundary(slice: &Wtf8, index: usize) {
|
||||
/// Copied from core::str::raw::slice_unchecked
|
||||
#[inline]
|
||||
pub unsafe fn slice_unchecked(s: &Wtf8, begin: usize, end: usize) -> &Wtf8 {
|
||||
// memory layout of a &[u8] and &Wtf8 are the same
|
||||
Wtf8::from_bytes_unchecked(slice::from_raw_parts(s.bytes.as_ptr().add(begin), end - begin))
|
||||
// SAFETY: memory layout of a &[u8] and &Wtf8 are the same
|
||||
unsafe {
|
||||
let len = end - begin;
|
||||
let start = s.as_bytes().as_ptr().add(begin);
|
||||
Wtf8::from_bytes_unchecked(slice::from_raw_parts(start, len))
|
||||
}
|
||||
}
|
||||
|
||||
/// Copied from core::str::raw::slice_error_fail
|
||||
|
Loading…
Reference in New Issue
Block a user