Skip to content

Commit 735e5dd

Browse files
committed
Add fat pointer &str (for &'static str, etc)
1 parent 49a8bdf commit 735e5dd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/cache.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! Todo: verify this module!
44
55
use core::slice;
6+
use core::str;
67

78
/// Convert a buffer or a pointer into ones with uncached address.
89
///
@@ -59,6 +60,32 @@ impl<T> Uncache for &mut [T] {
5960
}
6061
}
6162

63+
impl Uncache for &str {
64+
#[inline]
65+
fn uncache(self) -> Self {
66+
let addr = self.as_ptr() as usize;
67+
assert_addr_cached(addr);
68+
let new_ptr = (addr - 0x4000_0000) as *const u8;
69+
// note(unsafe): source address is safe; passing ownership
70+
let slice = unsafe { slice::from_raw_parts(new_ptr, self.len()) };
71+
// note(unsafe): source slice is guaranteed valid in UTF-8
72+
unsafe { str::from_utf8_unchecked(slice) }
73+
}
74+
}
75+
76+
impl Uncache for &mut str {
77+
#[inline]
78+
fn uncache(self) -> Self {
79+
let addr = self.as_ptr() as usize;
80+
assert_addr_cached(addr);
81+
let new_ptr = (addr - 0x4000_0000) as *mut u8;
82+
// note(unsafe): source address is safe; passing ownership
83+
let slice = unsafe { slice::from_raw_parts_mut(new_ptr, self.len()) };
84+
// note(unsafe): source slice is guaranteed valid in UTF-8
85+
unsafe { str::from_utf8_unchecked_mut(slice) }
86+
}
87+
}
88+
6289
impl<T> Uncache for *const T {
6390
#[inline]
6491
fn uncache(self) -> Self {

0 commit comments

Comments
 (0)