11pub mod document;
22pub mod tag;
33
4- use std:: cell:: Cell ;
54// #[cfg(target_pointer_width = "64")]
65// use biome_rowan::static_assert;
76use std:: hash:: { Hash , Hasher } ;
8- use std:: mem:: { self , ManuallyDrop } ;
97use std:: num:: NonZeroU32 ;
8+ use std:: ptr;
109use std:: { borrow:: Cow , ops:: Deref , rc:: Rc } ;
1110
1211use unicode_width:: { UnicodeWidthChar , UnicodeWidthStr } ;
@@ -53,7 +52,7 @@ const _: () = {
5352/// Language agnostic IR for formatting source code.
5453///
5554/// Use the helper functions like [crate::builders::space], [crate::builders::soft_line_break] etc. defined in this file to create elements.
56- #[ derive( Clone ) ]
55+ #[ derive( Clone , Eq , PartialEq ) ]
5756pub enum FormatElement < ' a > {
5857 /// A space token, see [crate::builders::space] for documentation.
5958 Space ,
@@ -150,29 +149,25 @@ impl PrintMode {
150149}
151150
152151#[ derive( Clone ) ]
153- pub struct Interned < ' a > ( ManuallyDrop < Rc < ArenaVec < ' a , FormatElement < ' a > > > > ) ;
152+ pub struct Interned < ' a > ( & ' a [ FormatElement < ' a > ] ) ;
154153
155154impl < ' a > Interned < ' a > {
156155 pub ( super ) fn new ( content : ArenaVec < ' a , FormatElement < ' a > > ) -> Self {
157- Self ( ManuallyDrop :: new ( Rc :: new ( content) ) )
156+ Self ( content. into_bump_slice ( ) )
158157 }
159158}
160159
161160impl PartialEq for Interned < ' _ > {
162161 fn eq ( & self , other : & Interned < ' _ > ) -> bool {
163- std :: ptr:: eq ( & raw const self . 0 , & raw const other. 0 )
162+ ptr:: eq ( self . 0 , other. 0 )
164163 }
165164}
166165
167166impl Eq for Interned < ' _ > { }
168167
169168impl Hash for Interned < ' _ > {
170- fn hash < H > ( & self , hasher : & mut H )
171- where
172- H : Hasher ,
173- {
174- Address :: from_ptr ( & raw const self . 0 ) . hash ( hasher) ;
175- // Rc::as_ptr(&self.0).hash(hasher);
169+ fn hash < H : Hasher > ( & self , state : & mut H ) {
170+ self . 0 . as_ptr ( ) . addr ( ) . hash ( state) ;
176171 }
177172}
178173
@@ -186,7 +181,7 @@ impl<'a> Deref for Interned<'a> {
186181 type Target = [ FormatElement < ' a > ] ;
187182
188183 fn deref ( & self ) -> & Self :: Target {
189- & self . 0
184+ self . 0
190185 }
191186}
192187
@@ -305,19 +300,12 @@ impl FormatElements for FormatElement<'_> {
305300/// can pick the best fitting variant.
306301///
307302/// Best fitting is defined as the variant that takes the most horizontal space but fits on the line.
303+ #[ derive( Clone , Eq , PartialEq ) ]
308304pub struct BestFittingElement < ' a > {
309305 /// The different variants for this element.
310306 /// The first element is the one that takes up the most space horizontally (the most flat),
311307 /// The last element takes up the least space horizontally (but most horizontal space).
312- variants : ArenaBox < ' a , [ ArenaBox < ' a , [ FormatElement < ' a > ] > ] > ,
313- }
314-
315- impl Clone for BestFittingElement < ' _ > {
316- fn clone ( & self ) -> Self {
317- // SAFETY: NOT sure if it is safe, but needed to implement Clone.
318- // TODO: Once everything works, this should be replaced with a proper clone implementation.
319- Self { variants : unsafe { mem:: transmute_copy ( self ) } }
320- }
308+ variants : & ' a [ & ' a [ FormatElement < ' a > ] ] ,
321309}
322310
323311impl < ' a > BestFittingElement < ' a > {
@@ -330,15 +318,13 @@ impl<'a> BestFittingElement<'a> {
330318 /// ## Safety
331319 /// The slice must contain at least two variants.
332320 #[ doc( hidden) ]
333- pub unsafe fn from_vec_unchecked (
334- variants : ArenaVec < ' a , ArenaBox < ' a , [ FormatElement < ' a > ] > > ,
335- ) -> Self {
321+ pub unsafe fn from_vec_unchecked ( variants : ArenaVec < ' a , & ' a [ FormatElement < ' a > ] > ) -> Self {
336322 debug_assert ! (
337323 variants. len( ) >= 2 ,
338324 "Requires at least the least expanded and most expanded variants"
339325 ) ;
340326
341- Self { variants : variants. into_boxed_slice ( ) }
327+ Self { variants : variants. into_bump_slice ( ) }
342328 }
343329
344330 /// Returns the most expanded variant
@@ -348,8 +334,8 @@ impl<'a> BestFittingElement<'a> {
348334 )
349335 }
350336
351- pub fn variants ( & self ) -> & [ ArenaBox < ' a , [ FormatElement < ' a > ] > ] {
352- & self . variants
337+ pub fn variants ( & self ) -> & [ & ' a [ FormatElement < ' a > ] ] {
338+ self . variants
353339 }
354340
355341 /// Returns the least expanded variant
@@ -362,7 +348,7 @@ impl<'a> BestFittingElement<'a> {
362348
363349impl std:: fmt:: Debug for BestFittingElement < ' _ > {
364350 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
365- f. debug_list ( ) . entries ( & * self . variants ) . finish ( )
351+ f. debug_list ( ) . entries ( self . variants ) . finish ( )
366352 }
367353}
368354
0 commit comments