diff --git a/src/fast_api.rs b/src/fast_api.rs index db76fb69..3611c360 100644 --- a/src/fast_api.rs +++ b/src/fast_api.rs @@ -218,14 +218,9 @@ pub struct FastApiOneByteString { impl FastApiOneByteString { #[inline(always)] - pub fn as_str(&self) -> &str { - // SAFETY: The string is guaranteed to be valid UTF-8. - unsafe { - std::str::from_utf8_unchecked(std::slice::from_raw_parts( - self.data, - self.length as usize, - )) - } + pub fn as_bytes(&self) -> &[u8] { + // SAFETY: The data is guaranteed to be valid for the length of the string. + unsafe { std::slice::from_raw_parts(self.data, self.length as usize) } } } diff --git a/tests/test_api.rs b/tests/test_api.rs index dea0bf27..d95ffd97 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -8915,8 +8915,8 @@ fn test_fast_calls_onebytestring() { data: *const fast_api::FastApiOneByteString, ) -> u32 { unsafe { WHO = "fast" }; - let data = unsafe { &*data }.as_str(); - assert_eq!("hello", data); + let data = unsafe { &*data }.as_bytes(); + assert_eq!(b"hello", data); data.len() as u32 }