@@ -26,7 +26,7 @@ mod tests;
2626
2727use self :: { iter:: Windows , str:: JsSliceIndex } ;
2828use crate :: display:: { JsStrDisplayEscaped , JsStrDisplayLossy , JsStringDebugInfo } ;
29- use crate :: r#type:: { Latin1 , Latin1SequenceString , StringType , Utf16 , Utf16SequenceString } ;
29+ use crate :: r#type:: { Latin1 , Utf16 } ;
3030pub use crate :: vtable:: StaticString ;
3131use crate :: vtable:: { SequenceString , SliceString } ;
3232#[ doc( inline) ]
@@ -38,14 +38,13 @@ pub use crate::{
3838 str:: { JsStr , JsStrVariant } ,
3939} ;
4040use std:: marker:: PhantomData ;
41+ use std:: { borrow:: Cow , mem:: ManuallyDrop } ;
4142use std:: {
42- alloc:: { Layout , alloc} ,
4343 convert:: Infallible ,
4444 hash:: { Hash , Hasher } ,
4545 ptr:: { self , NonNull } ,
4646 str:: FromStr ,
4747} ;
48- use std:: { borrow:: Cow , mem:: ManuallyDrop } ;
4948use vtable:: JsStringVTable ;
5049
5150fn alloc_overflow ( ) -> ! {
@@ -103,10 +102,10 @@ pub struct RawJsString {
103102#[ derive( Debug , Clone , Copy , Eq , PartialEq ) ]
104103#[ repr( u8 ) ]
105104pub ( crate ) enum JsStringKind {
106- /// A sequential memory slice of Latin1 bytes. See [`Latin1SequenceString `].
105+ /// A sequential memory slice of Latin1 bytes. See [`SequenceString `].
107106 Latin1Sequence = 0 ,
108107
109- /// A sequential memory slice of UTF-16 code units. See [`Utf16SequenceString `].
108+ /// A sequential memory slice of UTF-16 code units. See [`SequenceString `].
110109 Utf16Sequence = 1 ,
111110
112111 /// A slice of an existing string. See [`SliceString`].
@@ -565,11 +564,11 @@ impl JsString {
565564 }
566565
567566 let ( ptr, data_offset) = if latin1_encoding {
568- let p = Self :: allocate_seq :: < Latin1 > ( full_count) ;
569- ( p. cast :: < u8 > ( ) , size_of :: < Latin1SequenceString > ( ) )
567+ let p = SequenceString :: < Latin1 > :: allocate ( full_count) ;
568+ ( p. cast :: < u8 > ( ) , size_of :: < SequenceString < Latin1 > > ( ) )
570569 } else {
571- let p = Self :: allocate_seq :: < Utf16 > ( full_count) ;
572- ( p. cast :: < u8 > ( ) , size_of :: < Utf16SequenceString > ( ) )
570+ let p = SequenceString :: < Utf16 > :: allocate ( full_count) ;
571+ ( p. cast :: < u8 > ( ) , size_of :: < SequenceString < Utf16 > > ( ) )
573572 } ;
574573
575574 let string = {
@@ -623,78 +622,6 @@ impl JsString {
623622 StaticJsStrings :: get_string ( & string. as_str ( ) ) . unwrap_or ( string)
624623 }
625624
626- /// Allocates a new [`Latin1SequenceString`] with an internal capacity of `str_len` bytes.
627- ///
628- /// # Panics
629- ///
630- /// Panics if `try_allocate_seq` returns `Err`.
631- fn allocate_seq < T : StringType > ( str_len : usize ) -> NonNull < SequenceString < T > > {
632- match Self :: try_allocate_seq :: < T > ( str_len) {
633- Ok ( v) => v,
634- Err ( None ) => alloc_overflow ( ) ,
635- Err ( Some ( layout) ) => std:: alloc:: handle_alloc_error ( layout) ,
636- }
637- }
638-
639- // This is marked as safe because it is always valid to call this function to request any number
640- // of bytes, since this function ought to fail on an OOM error.
641- /// Allocates a new [`SequenceString`] with an internal capacity of `str_len` bytes.
642- ///
643- /// # Errors
644- ///
645- /// Returns `Err(None)` on integer overflows `usize::MAX`.
646- /// Returns `Err(Some(Layout))` on allocation error.
647- fn try_allocate_seq < T : StringType > (
648- str_len : usize ,
649- ) -> Result < NonNull < SequenceString < T > > , Option < Layout > > {
650- let ( layout, offset) = Layout :: array :: < T :: Byte > ( str_len)
651- . and_then ( |arr| T :: base_layout ( ) . extend ( arr) )
652- . map ( |( layout, offset) | ( layout. pad_to_align ( ) , offset) )
653- . map_err ( |_| None ) ?;
654-
655- debug_assert_eq ! ( offset, T :: DATA_OFFSET ) ;
656- debug_assert_eq ! ( layout. align( ) , align_of:: <SequenceString <T >>( ) ) ;
657-
658- #[ allow( clippy:: cast_ptr_alignment) ]
659- // SAFETY:
660- // The layout size of `SequenceString` is never zero, since it has to store
661- // the length of the string and the reference count.
662- let inner = unsafe { alloc ( layout) . cast :: < SequenceString < T > > ( ) } ;
663-
664- // We need to verify that the pointer returned by `alloc` is not null, otherwise
665- // we should abort, since an allocation error is pretty unrecoverable for us
666- // right now.
667- let inner = NonNull :: new ( inner) . ok_or ( Some ( layout) ) ?;
668-
669- // SAFETY:
670- // `NonNull` verified for us that the pointer returned by `alloc` is valid,
671- // meaning we can write to its pointed memory.
672- unsafe {
673- // Write the first part, the `SequenceString`.
674- inner. as_ptr ( ) . write ( SequenceString :: < T > :: new ( str_len) ) ;
675- }
676-
677- debug_assert ! ( {
678- let inner = inner. as_ptr( ) ;
679- // SAFETY:
680- // - `inner` must be a valid pointer, since it comes from a `NonNull`,
681- // meaning we can safely dereference it to `SequenceString`.
682- // - `offset` should point us to the beginning of the array,
683- // and since we requested a `SequenceString` layout with a trailing
684- // `[T::Byte; str_len]`, the memory of the array must be in the `usize`
685- // range for the allocation to succeed.
686- unsafe {
687- // This is `<u8>` as the offset is in bytes.
688- ptr:: eq(
689- inner. cast:: <u8 >( ) . add( offset) . cast( ) ,
690- ( * inner) . data( ) . cast_mut( ) ,
691- )
692- }
693- } ) ;
694-
695- Ok ( inner)
696- }
697-
698625 /// Creates a new [`JsString`] from `data`, without checking if the string is in the interner.
699626 fn from_slice_skip_interning ( string : JsStr < ' _ > ) -> Self {
700627 let count = string. len ( ) ;
@@ -712,14 +639,14 @@ impl JsString {
712639 #[ allow( clippy:: cast_ptr_alignment) ]
713640 match string. variant ( ) {
714641 JsStrVariant :: Latin1 ( s) => {
715- let ptr = Self :: allocate_seq :: < Latin1 > ( count) ;
642+ let ptr = SequenceString :: < Latin1 > :: allocate ( count) ;
716643 let data = ( & raw mut ( * ptr. as_ptr ( ) ) . data )
717644 . cast :: < <Latin1 as r#type:: sealed:: InternalStringType >:: Byte > ( ) ;
718645 ptr:: copy_nonoverlapping ( s. as_ptr ( ) , data, count) ;
719646 Self { ptr : ptr. cast ( ) }
720647 }
721648 JsStrVariant :: Utf16 ( s) => {
722- let ptr = Self :: allocate_seq :: < Utf16 > ( count) ;
649+ let ptr = SequenceString :: < Utf16 > :: allocate ( count) ;
723650 let data = ( & raw mut ( * ptr. as_ptr ( ) ) . data )
724651 . cast :: < <Utf16 as r#type:: sealed:: InternalStringType >:: Byte > ( ) ;
725652 ptr:: copy_nonoverlapping ( s. as_ptr ( ) , data, count) ;
0 commit comments