@@ -230,13 +230,14 @@ use core::hash::{Hash, Hasher};
230
230
use core:: intrinsics:: { abort, assume} ;
231
231
use core:: marker;
232
232
use core:: marker:: Unsize ;
233
- use core:: mem:: { self , align_of_val, forget, size_of_val, uninitialized} ;
233
+ use core:: mem:: { self , align_of_val, forget, size_of , size_of_val, uninitialized} ;
234
234
use core:: ops:: Deref ;
235
235
use core:: ops:: CoerceUnsized ;
236
236
use core:: ptr:: { self , Shared } ;
237
237
use core:: convert:: From ;
238
238
239
239
use heap:: deallocate;
240
+ use raw_vec:: RawVec ;
240
241
241
242
struct RcBox < T : ?Sized > {
242
243
strong : Cell < usize > ,
@@ -365,6 +366,30 @@ impl<T> Rc<T> {
365
366
}
366
367
}
367
368
369
+ impl Rc < str > {
370
+ /// Constructs a new `Rc<str>` from a string slice.
371
+ #[ doc( hidden) ]
372
+ #[ unstable( feature = "rustc_private" ,
373
+ reason = "for internal use in rustc" ,
374
+ issue = "0" ) ]
375
+ pub fn __from_str ( value : & str ) -> Rc < str > {
376
+ unsafe {
377
+ // Allocate enough space for `RcBox<str>`.
378
+ let aligned_len = ( value. len ( ) + size_of :: < usize > ( ) - 1 ) / size_of :: < usize > ( ) ;
379
+ let vec = RawVec :: < usize > :: with_capacity ( 2 + aligned_len) ;
380
+ let ptr = vec. ptr ( ) ;
381
+ forget ( vec) ;
382
+ // Initialize fields of `RcBox<str>`.
383
+ * ptr. offset ( 0 ) = 1 ; // strong: Cell::new(1)
384
+ * ptr. offset ( 1 ) = 1 ; // weak: Cell::new(1)
385
+ ptr:: copy_nonoverlapping ( value. as_ptr ( ) , ptr. offset ( 2 ) as * mut u8 , value. len ( ) ) ;
386
+ // Combine the allocation address and the string length into a fat pointer to `RcBox`.
387
+ let rcbox_ptr = mem:: transmute ( [ ptr as usize , value. len ( ) ] ) ;
388
+ Rc { ptr : Shared :: new ( rcbox_ptr) }
389
+ }
390
+ }
391
+ }
392
+
368
393
impl < T : ?Sized > Rc < T > {
369
394
/// Creates a new [`Weak`][weak] pointer to this value.
370
395
///
0 commit comments