1
+ #[ cfg( feature = "unstable-static-encoding-str" ) ]
2
+ use super :: Encode ;
1
3
use super :: Encoding ;
2
4
3
5
pub ( crate ) const fn static_int_str_len ( mut n : u128 ) -> usize {
@@ -41,8 +43,7 @@ pub(crate) const fn static_encoding_str_len(encoding: Encoding<'_>) -> usize {
41
43
42
44
match encoding {
43
45
Char | Short | Int | Long | LongLong | UChar | UShort | UInt | ULong | ULongLong
44
- | Float | Double | LongDouble | Bool | Void | String | Object | Class | Sel
45
- | Unknown => 1 ,
46
+ | Float | Double | LongDouble | Bool | Void | String | Object | Class | Sel | Unknown => 1 ,
46
47
Block | FloatComplex | DoubleComplex | LongDoubleComplex => 2 ,
47
48
BitField ( b, _type) => 1 + static_int_str_len ( b as u128 ) ,
48
49
Pointer ( & t) => 1 + static_encoding_str_len ( t) ,
@@ -61,7 +62,9 @@ pub(crate) const fn static_encoding_str_len(encoding: Encoding<'_>) -> usize {
61
62
}
62
63
}
63
64
64
- pub ( crate ) const fn static_encoding_str_array < const LEN : usize > ( encoding : Encoding < ' _ > ) -> [ u8 ; LEN ] {
65
+ pub ( crate ) const fn static_encoding_str_array < const LEN : usize > (
66
+ encoding : Encoding < ' _ > ,
67
+ ) -> [ u8 ; LEN ] {
65
68
use Encoding :: * ;
66
69
67
70
let mut res: [ u8 ; LEN ] = [ 0 ; LEN ] ;
@@ -204,6 +207,38 @@ pub(crate) const fn static_encoding_str_array<const LEN: usize>(encoding: Encodi
204
207
res
205
208
}
206
209
210
+ /// Workaround since we can't specify the correct `where` bound on `Encode`.
211
+ #[ cfg( feature = "unstable-static-encoding-str" ) ]
212
+ pub struct EncodingHelper < T > ( T ) ;
213
+
214
+ #[ cfg( feature = "unstable-static-encoding-str" ) ]
215
+ impl < T : super :: Encode > EncodingHelper < T >
216
+ where
217
+ [ u8 ; static_encoding_str_len ( T :: ENCODING ) + 1 ] : Sized ,
218
+ {
219
+ #[ doc( hidden) ]
220
+ // Contains null byte at the end
221
+ const __ENCODING_CSTR_BYTES: [ u8 ; static_encoding_str_len ( T :: ENCODING ) + 1 ] =
222
+ static_encoding_str_array ( T :: ENCODING ) ;
223
+
224
+ /// TODO
225
+ pub const ENCODING_CSTR : * const u8 = Self :: __ENCODING_CSTR_BYTES. as_ptr ( ) ;
226
+ }
227
+
228
+ #[ cfg( feature = "unstable-static-encoding-str" ) ]
229
+ impl < T : Encode > EncodingHelper < T >
230
+ where
231
+ [ u8 ; static_encoding_str_len ( T :: ENCODING ) ] : Sized ,
232
+ {
233
+ #[ doc( hidden) ]
234
+ const __ENCODING_STR_BYTES: [ u8 ; static_encoding_str_len ( T :: ENCODING ) ] =
235
+ static_encoding_str_array ( T :: ENCODING ) ;
236
+
237
+ /// TODO
238
+ pub const ENCODING_STR : & ' static str =
239
+ unsafe { core:: mem:: transmute :: < & [ u8 ] , & str > ( & Self :: __ENCODING_STR_BYTES) } ;
240
+ }
241
+
207
242
#[ cfg( test) ]
208
243
mod tests {
209
244
use super :: * ;
0 commit comments