11use crate :: throw_sg_err;
2- use alloc:: { borrow:: ToOwned , string:: String } ;
32use core:: {
4- borrow:: Borrow ,
53 fmt:: { Debug , Display } ,
64 ops:: Deref ,
75} ;
86use proc_macro2:: { Span , TokenStream as TokenStream2 } ;
7+ use std:: ffi:: OsStr ;
98
109/// The actual literal expression, written as "./test".
1110#[ repr( transparent) ]
12- pub struct ExprLit {
13- data : str ,
14- }
11+ pub struct ExprLit ( str ) ;
1512
1613impl PartialEq < ExprLit > for ExprLit {
1714 #[ inline]
@@ -27,6 +24,13 @@ impl PartialEq<str> for ExprLit {
2724 }
2825}
2926
27+ impl AsRef < OsStr > for ExprLit {
28+ #[ inline]
29+ fn as_ref ( & self ) -> & OsStr {
30+ AsRef :: as_ref ( self . as_str ( ) )
31+ }
32+ }
33+
3034/// Errors received in case of a
3135/// literal expression parsing error.
3236#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord ) ]
@@ -39,6 +43,11 @@ pub enum ExprLitTryNewErr {
3943}
4044
4145impl ExprLitTryNewErr {
46+ #[ inline]
47+ pub const fn expr_len ( current : usize , exp : usize ) -> Self {
48+ Self :: ExpLen { current, exp }
49+ }
50+
4251 /// Convert an error to a syntax tree.
4352 #[ inline]
4453 pub fn into_tt_err ( self , span : Span ) -> TokenStream2 {
@@ -76,22 +85,6 @@ impl Display for ExprLit {
7685 }
7786}
7887
79- impl ToOwned for ExprLit {
80- type Owned = String ;
81-
82- #[ inline]
83- fn to_owned ( & self ) -> Self :: Owned {
84- self . as_str ( ) . to_owned ( )
85- }
86- }
87-
88- impl Borrow < ExprLit > for String {
89- #[ inline]
90- fn borrow ( & self ) -> & ExprLit {
91- unsafe { ExprLit :: unchecked ( self . as_str ( ) ) } // TODO
92- }
93- }
94-
9588impl ExprLit {
9689 /// Creating `ExprLit` without clipping.
9790 #[ inline]
@@ -103,7 +96,7 @@ impl ExprLit {
10396
10497 /// Creating `ExprLit` without clipping.
10598 #[ inline]
106- pub const unsafe fn unchecked ( a : & str ) -> & Self {
99+ pub const unsafe fn new_unchecked ( a : & str ) -> & Self {
107100 Self :: __new ( a)
108101 }
109102
@@ -124,10 +117,7 @@ impl ExprLit {
124117
125118 let len = a_array. len ( ) ;
126119 if len < 2 {
127- return err ( ExprLitTryNewErr :: ExpLen {
128- current : len,
129- exp : 2 ,
130- } ) ;
120+ return err ( ExprLitTryNewErr :: expr_len ( len, 2 ) ) ;
131121 }
132122 debug_assert ! ( {
133123 #[ allow( clippy:: get_first) ]
@@ -142,6 +132,7 @@ impl ExprLit {
142132 /*
143133 This is safe, the extra necessary checks are done in a separate `if` above.
144134 */
135+
145136 match unsafe { ( a_array. get_unchecked ( 0 ) , a_array. get_unchecked ( len - 1 ) ) } {
146137 ( b'"' , b'"' ) =>
147138 /* line */
@@ -152,10 +143,7 @@ impl ExprLit {
152143 We exclude the possibility of using `'` as more
153144 than one character.
154145 */
155- return err ( ExprLitTryNewErr :: ExpLen {
156- current : len,
157- exp : 3 ,
158- } ) ;
146+ return err ( ExprLitTryNewErr :: expr_len ( len, 3 ) ) ;
159147 }
160148 ( b'\'' , b'\'' ) =>
161149 /* line */
@@ -172,16 +160,17 @@ impl ExprLit {
172160 } )
173161 }
174162
163+ #[ allow( dead_code) ]
175164 #[ inline]
176165 /// Returns `true` if self has a length of zero bytes.
177166 pub const fn is_empty ( & self ) -> bool {
178- self . data . is_empty ( )
167+ self . 0 . is_empty ( )
179168 }
180169
181170 /// Getting a string of actual data.
182171 #[ inline]
183172 pub const fn as_str ( & self ) -> & str {
184- & self . data
173+ & self . 0
185174 }
186175}
187176
@@ -191,13 +180,10 @@ fn test_literal() {
191180 /*
192181 Checking the correct operation of ExprLit.
193182 */
194- assert_eq ! (
195- ExprLit :: try_new( "" ) ,
196- Err ( ExprLitTryNewErr :: ExpLen { current: 0 , exp: 2 } )
197- ) ;
183+ assert_eq ! ( ExprLit :: try_new( "" ) , Err ( ExprLitTryNewErr :: expr_len( 0 , 2 ) ) ) ;
198184 assert_eq ! (
199185 ExprLit :: try_new( "\" " ) ,
200- Err ( ExprLitTryNewErr :: ExpLen { current : 1 , exp : 2 } )
186+ Err ( ExprLitTryNewErr :: expr_len ( 1 , 2 ) )
201187 ) ;
202188 assert_eq ! ( ExprLit :: try_new( "\" \" " ) , Ok ( ExprLit :: __new( "" ) ) , ) ;
203189 assert_eq ! ( ExprLit :: try_new( "'\\ '" ) , Ok ( ExprLit :: __new( "\\ " ) ) , ) ;
0 commit comments