File tree Expand file tree Collapse file tree 3 files changed +25
-6
lines changed Expand file tree Collapse file tree 3 files changed +25
-6
lines changed Original file line number Diff line number Diff line change @@ -484,6 +484,21 @@ impl DroplessArena {
484484 }
485485 }
486486
487+ /// Allocates a string slice that is copied into the `DroplessArena`, returning a
488+ /// reference to it. Will panic if passed an empty string.
489+ ///
490+ /// Panics:
491+ ///
492+ /// - Zero-length string
493+ #[ inline]
494+ pub fn alloc_str ( & self , string : & str ) -> & str
495+ {
496+ let slice = self . alloc_slice ( string. as_bytes ( ) ) ;
497+
498+ // SAFETY: the result has a copy of the same valid UTF-8 bytes.
499+ unsafe { std:: str:: from_utf8_unchecked ( slice) }
500+ }
501+
487502 /// # Safety
488503 ///
489504 /// The caller must ensure that `mem` is valid for writes up to `size_of::<T>() * len`, and that
@@ -655,6 +670,14 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
655670 self . dropless . alloc_slice ( value)
656671 }
657672
673+ #[ inline]
674+ pub fn alloc_str ( & self , string : & str ) -> & str {
675+ if string. is_empty ( ) {
676+ return "" ;
677+ }
678+ self . dropless . alloc_str ( string)
679+ }
680+
658681 #[ allow( clippy:: mut_from_ref) ]
659682 pub fn alloc_from_iter < T : ArenaAllocatable < ' tcx , C > , C > (
660683 & self ,
Original file line number Diff line number Diff line change @@ -2615,7 +2615,7 @@ pub struct SymbolName<'tcx> {
26152615impl < ' tcx > SymbolName < ' tcx > {
26162616 pub fn new ( tcx : TyCtxt < ' tcx > , name : & str ) -> SymbolName < ' tcx > {
26172617 SymbolName {
2618- name : unsafe { str :: from_utf8_unchecked ( tcx. arena . alloc_slice ( name. as_bytes ( ) ) ) } ,
2618+ name : tcx. arena . alloc_str ( name) ,
26192619 }
26202620 }
26212621}
Original file line number Diff line number Diff line change @@ -2118,11 +2118,7 @@ impl Interner {
21182118 return Symbol :: new ( idx as u32 ) ;
21192119 }
21202120
2121- // SAFETY: we convert from `&str` to `&[u8]`, clone it into the arena,
2122- // and immediately convert the clone back to `&[u8]`, all because there
2123- // is no `inner.arena.alloc_str()` method. This is clearly safe.
2124- let string: & str =
2125- unsafe { str:: from_utf8_unchecked ( inner. arena . alloc_slice ( string. as_bytes ( ) ) ) } ;
2121+ let string: & str = inner. arena . alloc_str ( string) ;
21262122
21272123 // SAFETY: we can extend the arena allocation to `'static` because we
21282124 // only access these while the arena is still alive.
You can’t perform that action at this time.
0 commit comments