@@ -10,11 +10,16 @@ use crate::sys_common::wtf8::{Wtf8, Wtf8Buf, check_utf8_boundary};
1010use crate :: sys_common:: { AsInner , FromInner , IntoInner } ;
1111use crate :: { fmt, mem} ;
1212
13- #[ derive( Clone , Hash ) ]
13+ #[ derive( Hash ) ]
1414pub struct Buf {
1515 pub inner : Wtf8Buf ,
1616}
1717
18+ #[ repr( transparent) ]
19+ pub struct Slice {
20+ pub inner : Wtf8 ,
21+ }
22+
1823impl IntoInner < Wtf8Buf > for Buf {
1924 fn into_inner ( self ) -> Wtf8Buf {
2025 self . inner
@@ -35,31 +40,38 @@ impl AsInner<Wtf8> for Buf {
3540}
3641
3742impl fmt:: Debug for Buf {
38- fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
39- fmt:: Debug :: fmt ( self . as_slice ( ) , formatter )
43+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
44+ fmt:: Debug :: fmt ( self . as_slice ( ) , f )
4045 }
4146}
4247
4348impl fmt:: Display for Buf {
44- fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
45- fmt:: Display :: fmt ( self . as_slice ( ) , formatter )
49+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
50+ fmt:: Display :: fmt ( self . as_slice ( ) , f )
4651 }
4752}
4853
49- #[ repr( transparent) ]
50- pub struct Slice {
51- pub inner : Wtf8 ,
52- }
53-
5454impl fmt:: Debug for Slice {
55- fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
56- fmt:: Debug :: fmt ( & self . inner , formatter )
55+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
56+ fmt:: Debug :: fmt ( & self . inner , f )
5757 }
5858}
5959
6060impl fmt:: Display for Slice {
61- fn fmt ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
62- fmt:: Display :: fmt ( & self . inner , formatter)
61+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
62+ fmt:: Display :: fmt ( & self . inner , f)
63+ }
64+ }
65+
66+ impl Clone for Buf {
67+ #[ inline]
68+ fn clone ( & self ) -> Self {
69+ Buf { inner : self . inner . clone ( ) }
70+ }
71+
72+ #[ inline]
73+ fn clone_from ( & mut self , source : & Self ) {
74+ self . inner . clone_from ( & source. inner )
6375 }
6476}
6577
@@ -74,62 +86,57 @@ impl Buf {
7486 unsafe { Self { inner : Wtf8Buf :: from_bytes_unchecked ( s) } }
7587 }
7688
77- pub fn with_capacity ( capacity : usize ) -> Buf {
78- Buf { inner : Wtf8Buf :: with_capacity ( capacity) }
79- }
80-
81- pub fn clear ( & mut self ) {
82- self . inner . clear ( )
83- }
84-
85- pub fn capacity ( & self ) -> usize {
86- self . inner . capacity ( )
89+ #[ inline]
90+ pub fn into_string ( self ) -> Result < String , Buf > {
91+ self . inner . into_string ( ) . map_err ( |buf| Buf { inner : buf } )
8792 }
8893
94+ #[ inline]
8995 pub fn from_string ( s : String ) -> Buf {
9096 Buf { inner : Wtf8Buf :: from_string ( s) }
9197 }
9298
93- pub fn as_slice ( & self ) -> & Slice {
94- // SAFETY: Slice is just a wrapper for Wtf8,
95- // and self.inner.as_slice() returns &Wtf8.
96- // Therefore, transmuting &Wtf8 to &Slice is safe.
97- unsafe { mem:: transmute ( self . inner . as_slice ( ) ) }
99+ #[ inline]
100+ pub fn with_capacity ( capacity : usize ) -> Buf {
101+ Buf { inner : Wtf8Buf :: with_capacity ( capacity) }
98102 }
99103
100- pub fn as_mut_slice ( & mut self ) -> & mut Slice {
101- // SAFETY: Slice is just a wrapper for Wtf8,
102- // and self.inner.as_mut_slice() returns &mut Wtf8.
103- // Therefore, transmuting &mut Wtf8 to &mut Slice is safe.
104- // Additionally, care should be taken to ensure the slice
105- // is always valid Wtf8.
106- unsafe { mem:: transmute ( self . inner . as_mut_slice ( ) ) }
104+ #[ inline]
105+ pub fn clear ( & mut self ) {
106+ self . inner . clear ( )
107107 }
108108
109- pub fn into_string ( self ) -> Result < String , Buf > {
110- self . inner . into_string ( ) . map_err ( |buf| Buf { inner : buf } )
109+ #[ inline]
110+ pub fn capacity ( & self ) -> usize {
111+ self . inner . capacity ( )
111112 }
112113
114+ #[ inline]
113115 pub fn push_slice ( & mut self , s : & Slice ) {
114116 self . inner . push_wtf8 ( & s. inner )
115117 }
116118
119+ #[ inline]
117120 pub fn reserve ( & mut self , additional : usize ) {
118121 self . inner . reserve ( additional)
119122 }
120123
124+ #[ inline]
121125 pub fn try_reserve ( & mut self , additional : usize ) -> Result < ( ) , TryReserveError > {
122126 self . inner . try_reserve ( additional)
123127 }
124128
129+ #[ inline]
125130 pub fn reserve_exact ( & mut self , additional : usize ) {
126131 self . inner . reserve_exact ( additional)
127132 }
128133
134+ #[ inline]
129135 pub fn try_reserve_exact ( & mut self , additional : usize ) -> Result < ( ) , TryReserveError > {
130136 self . inner . try_reserve_exact ( additional)
131137 }
132138
139+ #[ inline]
133140 pub fn shrink_to_fit ( & mut self ) {
134141 self . inner . shrink_to_fit ( )
135142 }
@@ -139,6 +146,24 @@ impl Buf {
139146 self . inner . shrink_to ( min_capacity)
140147 }
141148
149+ #[ inline]
150+ pub fn as_slice ( & self ) -> & Slice {
151+ // SAFETY: Slice is just a wrapper for Wtf8,
152+ // and self.inner.as_slice() returns &Wtf8.
153+ // Therefore, transmuting &Wtf8 to &Slice is safe.
154+ unsafe { mem:: transmute ( self . inner . as_slice ( ) ) }
155+ }
156+
157+ #[ inline]
158+ pub fn as_mut_slice ( & mut self ) -> & mut Slice {
159+ // SAFETY: Slice is just a wrapper for Wtf8,
160+ // and self.inner.as_mut_slice() returns &mut Wtf8.
161+ // Therefore, transmuting &mut Wtf8 to &mut Slice is safe.
162+ // Additionally, care should be taken to ensure the slice
163+ // is always valid Wtf8.
164+ unsafe { mem:: transmute ( self . inner . as_mut_slice ( ) ) }
165+ }
166+
142167 #[ inline]
143168 pub fn leak < ' a > ( self ) -> & ' a mut Slice {
144169 unsafe { mem:: transmute ( self . inner . leak ( ) ) }
@@ -194,6 +219,7 @@ impl Slice {
194219 }
195220
196221 #[ track_caller]
222+ #[ inline]
197223 pub fn check_public_boundary ( & self , index : usize ) {
198224 check_utf8_boundary ( & self . inner , index) ;
199225 }
@@ -203,18 +229,22 @@ impl Slice {
203229 unsafe { mem:: transmute ( Wtf8 :: from_str ( s) ) }
204230 }
205231
232+ #[ inline]
206233 pub fn to_str ( & self ) -> Result < & str , crate :: str:: Utf8Error > {
207234 self . inner . as_str ( )
208235 }
209236
237+ #[ inline]
210238 pub fn to_string_lossy ( & self ) -> Cow < ' _ , str > {
211239 self . inner . to_string_lossy ( )
212240 }
213241
242+ #[ inline]
214243 pub fn to_owned ( & self ) -> Buf {
215244 Buf { inner : self . inner . to_owned ( ) }
216245 }
217246
247+ #[ inline]
218248 pub fn clone_into ( & self , buf : & mut Buf ) {
219249 self . inner . clone_into ( & mut buf. inner )
220250 }
@@ -224,6 +254,7 @@ impl Slice {
224254 unsafe { mem:: transmute ( self . inner . into_box ( ) ) }
225255 }
226256
257+ #[ inline]
227258 pub fn empty_box ( ) -> Box < Slice > {
228259 unsafe { mem:: transmute ( Wtf8 :: empty_box ( ) ) }
229260 }
0 commit comments