rustc_arena: add alloc_str
Two places called `from_utf8_unchecked` for strings from `alloc_slice`, and one's SAFETY comment said this was for lack of `alloc_str` -- so let's just add that instead!
This commit is contained in:
parent
e9013ac0e4
commit
92bf40ffe3
@ -484,6 +484,20 @@ pub fn alloc_slice<T>(&self, slice: &[T]) -> &mut [T]
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Allocates a string slice that is copied into the `DroplessArena`, returning a
|
||||||
|
/// reference to it. Will panic if passed an empty string.
|
||||||
|
///
|
||||||
|
/// Panics:
|
||||||
|
///
|
||||||
|
/// - Zero-length string
|
||||||
|
#[inline]
|
||||||
|
pub fn alloc_str(&self, string: &str) -> &str {
|
||||||
|
let slice = self.alloc_slice(string.as_bytes());
|
||||||
|
|
||||||
|
// SAFETY: the result has a copy of the same valid UTF-8 bytes.
|
||||||
|
unsafe { std::str::from_utf8_unchecked(slice) }
|
||||||
|
}
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
///
|
///
|
||||||
/// The caller must ensure that `mem` is valid for writes up to `size_of::<T>() * len`, and that
|
/// The caller must ensure that `mem` is valid for writes up to `size_of::<T>() * len`, and that
|
||||||
@ -655,6 +669,14 @@ pub fn alloc_slice<T: ::std::marker::Copy>(&self, value: &[T]) -> &mut [T] {
|
|||||||
self.dropless.alloc_slice(value)
|
self.dropless.alloc_slice(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn alloc_str(&self, string: &str) -> &str {
|
||||||
|
if string.is_empty() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
self.dropless.alloc_str(string)
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(clippy::mut_from_ref)]
|
#[allow(clippy::mut_from_ref)]
|
||||||
pub fn alloc_from_iter<T: ArenaAllocatable<'tcx, C>, C>(
|
pub fn alloc_from_iter<T: ArenaAllocatable<'tcx, C>, C>(
|
||||||
&self,
|
&self,
|
||||||
|
@ -2614,9 +2614,7 @@ pub struct SymbolName<'tcx> {
|
|||||||
|
|
||||||
impl<'tcx> SymbolName<'tcx> {
|
impl<'tcx> SymbolName<'tcx> {
|
||||||
pub fn new(tcx: TyCtxt<'tcx>, name: &str) -> SymbolName<'tcx> {
|
pub fn new(tcx: TyCtxt<'tcx>, name: &str) -> SymbolName<'tcx> {
|
||||||
SymbolName {
|
SymbolName { name: tcx.arena.alloc_str(name) }
|
||||||
name: unsafe { str::from_utf8_unchecked(tcx.arena.alloc_slice(name.as_bytes())) },
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2118,11 +2118,7 @@ fn intern(&self, string: &str) -> Symbol {
|
|||||||
return Symbol::new(idx as u32);
|
return Symbol::new(idx as u32);
|
||||||
}
|
}
|
||||||
|
|
||||||
// SAFETY: we convert from `&str` to `&[u8]`, clone it into the arena,
|
let string: &str = inner.arena.alloc_str(string);
|
||||||
// and immediately convert the clone back to `&[u8]`, all because there
|
|
||||||
// is no `inner.arena.alloc_str()` method. This is clearly safe.
|
|
||||||
let string: &str =
|
|
||||||
unsafe { str::from_utf8_unchecked(inner.arena.alloc_slice(string.as_bytes())) };
|
|
||||||
|
|
||||||
// SAFETY: we can extend the arena allocation to `'static` because we
|
// SAFETY: we can extend the arena allocation to `'static` because we
|
||||||
// only access these while the arena is still alive.
|
// only access these while the arena is still alive.
|
||||||
|
Loading…
Reference in New Issue
Block a user