File tree 3 files changed +25
-6
lines changed
3 files changed +25
-6
lines changed Original file line number Diff line number Diff line change @@ -484,6 +484,21 @@ impl DroplessArena {
484
484
}
485
485
}
486
486
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
+
487
502
/// # Safety
488
503
///
489
504
/// 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,)*]) {
655
670
self . dropless . alloc_slice ( value)
656
671
}
657
672
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
+
658
681
#[ allow( clippy:: mut_from_ref) ]
659
682
pub fn alloc_from_iter < T : ArenaAllocatable < ' tcx , C > , C > (
660
683
& self ,
Original file line number Diff line number Diff line change @@ -2615,7 +2615,7 @@ pub struct SymbolName<'tcx> {
2615
2615
impl < ' tcx > SymbolName < ' tcx > {
2616
2616
pub fn new ( tcx : TyCtxt < ' tcx > , name : & str ) -> SymbolName < ' tcx > {
2617
2617
SymbolName {
2618
- name : unsafe { str :: from_utf8_unchecked ( tcx. arena . alloc_slice ( name. as_bytes ( ) ) ) } ,
2618
+ name : tcx. arena . alloc_str ( name) ,
2619
2619
}
2620
2620
}
2621
2621
}
Original file line number Diff line number Diff line change @@ -2118,11 +2118,7 @@ impl Interner {
2118
2118
return Symbol :: new ( idx as u32 ) ;
2119
2119
}
2120
2120
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) ;
2126
2122
2127
2123
// SAFETY: we can extend the arena allocation to `'static` because we
2128
2124
// only access these while the arena is still alive.
You can’t perform that action at this time.
0 commit comments