File tree 3 files changed +24
-8
lines changed
3 files changed +24
-8
lines changed Original file line number Diff line number Diff line change @@ -484,6 +484,20 @@ 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
+ let slice = self . alloc_slice ( string. as_bytes ( ) ) ;
496
+
497
+ // SAFETY: the result has a copy of the same valid UTF-8 bytes.
498
+ unsafe { std:: str:: from_utf8_unchecked ( slice) }
499
+ }
500
+
487
501
/// # Safety
488
502
///
489
503
/// The caller must ensure that `mem` is valid for writes up to `size_of::<T>() * len`, and that
@@ -655,6 +669,14 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
655
669
self . dropless . alloc_slice ( value)
656
670
}
657
671
672
+ #[ inline]
673
+ pub fn alloc_str ( & self , string : & str ) -> & str {
674
+ if string. is_empty ( ) {
675
+ return "" ;
676
+ }
677
+ self . dropless . alloc_str ( string)
678
+ }
679
+
658
680
#[ allow( clippy:: mut_from_ref) ]
659
681
pub fn alloc_from_iter < T : ArenaAllocatable < ' tcx , C > , C > (
660
682
& self ,
Original file line number Diff line number Diff line change @@ -2614,9 +2614,7 @@ pub struct SymbolName<'tcx> {
2614
2614
2615
2615
impl < ' tcx > SymbolName < ' tcx > {
2616
2616
pub fn new ( tcx : TyCtxt < ' tcx > , name : & str ) -> SymbolName < ' tcx > {
2617
- SymbolName {
2618
- name : unsafe { str:: from_utf8_unchecked ( tcx. arena . alloc_slice ( name. as_bytes ( ) ) ) } ,
2619
- }
2617
+ SymbolName { name : tcx. arena . alloc_str ( name) }
2620
2618
}
2621
2619
}
2622
2620
Original file line number Diff line number Diff line change @@ -2119,11 +2119,7 @@ impl Interner {
2119
2119
return Symbol :: new ( idx as u32 ) ;
2120
2120
}
2121
2121
2122
- // SAFETY: we convert from `&str` to `&[u8]`, clone it into the arena,
2123
- // and immediately convert the clone back to `&[u8]`, all because there
2124
- // is no `inner.arena.alloc_str()` method. This is clearly safe.
2125
- let string: & str =
2126
- unsafe { str:: from_utf8_unchecked ( inner. arena . alloc_slice ( string. as_bytes ( ) ) ) } ;
2122
+ let string: & str = inner. arena . alloc_str ( string) ;
2127
2123
2128
2124
// SAFETY: we can extend the arena allocation to `'static` because we
2129
2125
// only access these while the arena is still alive.
You can’t perform that action at this time.
0 commit comments