1
- #![ cfg_attr( not( feature = "std" ) , no_std) ]
2
-
3
- #[ cfg( not( feature = "std" ) ) ]
4
- extern crate core as std;
5
-
6
- #[ cfg( not( feature = "std" ) ) ]
1
+ #![ no_std]
7
2
extern crate alloc;
8
3
9
- use core:: convert:: Infallible ;
10
- use std:: {
4
+ use alloc:: {
5
+ string:: { String , ToString } ,
6
+ sync:: Arc ,
7
+ } ;
8
+ use core:: {
11
9
borrow:: Borrow ,
12
10
cmp:: { self , Ordering } ,
11
+ convert:: Infallible ,
13
12
fmt, hash, iter,
14
13
ops:: Deref ,
15
14
str:: FromStr ,
16
15
} ;
17
16
18
- #[ cfg( not( feature = "std" ) ) ]
19
- use alloc:: {
20
- string:: { String , ToString } ,
21
- sync:: Arc ,
22
- } ;
23
-
24
- #[ cfg( feature = "std" ) ]
25
- use std:: sync:: Arc ;
26
-
27
17
/// A `SmolStr` is a string type that has the following properties:
28
18
///
29
19
/// * `size_of::<SmolStr>() == size_of::<String>()`
@@ -131,7 +121,7 @@ impl SmolStr {
131
121
if size + len > INLINE_CAP {
132
122
let ( min_remaining, _) = iter. size_hint ( ) ;
133
123
let mut heap = String :: with_capacity ( size + len + min_remaining) ;
134
- heap. push_str ( std :: str:: from_utf8 ( & buf[ ..len] ) . unwrap ( ) ) ;
124
+ heap. push_str ( core :: str:: from_utf8 ( & buf[ ..len] ) . unwrap ( ) ) ;
135
125
heap. push ( ch) ;
136
126
heap. extend ( iter) ;
137
127
return SmolStr ( Repr :: Heap ( heap. into_boxed_str ( ) . into ( ) ) ) ;
@@ -265,7 +255,7 @@ where
265
255
let size = slice. len ( ) ;
266
256
if size + len > INLINE_CAP {
267
257
let mut heap = String :: with_capacity ( size + len) ;
268
- heap. push_str ( std :: str:: from_utf8 ( & buf[ ..len] ) . unwrap ( ) ) ;
258
+ heap. push_str ( core :: str:: from_utf8 ( & buf[ ..len] ) . unwrap ( ) ) ;
269
259
heap. push_str ( & slice) ;
270
260
heap. extend ( iter) ;
271
261
return SmolStr ( Repr :: Heap ( heap. into_boxed_str ( ) . into ( ) ) ) ;
@@ -411,7 +401,7 @@ impl Repr {
411
401
Repr :: Inline { len, buf } => {
412
402
let len = * len as usize ;
413
403
let buf = & buf[ ..len] ;
414
- unsafe { :: std :: str:: from_utf8_unchecked ( buf) }
404
+ unsafe { :: core :: str:: from_utf8_unchecked ( buf) }
415
405
}
416
406
Repr :: Substring { newlines, spaces } => {
417
407
let newlines = * newlines;
@@ -425,9 +415,12 @@ impl Repr {
425
415
426
416
#[ cfg( feature = "serde" ) ]
427
417
mod serde {
428
- use super :: SmolStr ;
429
- use :: serde:: de:: { Deserializer , Error , Unexpected , Visitor } ;
430
- use std:: fmt;
418
+ use alloc:: { string:: String , vec:: Vec } ;
419
+ use core:: fmt;
420
+
421
+ use serde:: de:: { Deserializer , Error , Unexpected , Visitor } ;
422
+
423
+ use crate :: SmolStr ;
431
424
432
425
// https://github.com/serde-rs/serde/blob/629802f2abfd1a54a6072992888fea7ca5bc209f/serde/src/private/de.rs#L56-L125
433
426
fn smol_str < ' de : ' a , ' a , D > ( deserializer : D ) -> Result < SmolStr , D :: Error >
@@ -468,7 +461,7 @@ mod serde {
468
461
where
469
462
E : Error ,
470
463
{
471
- match std :: str:: from_utf8 ( v) {
464
+ match core :: str:: from_utf8 ( v) {
472
465
Ok ( s) => Ok ( SmolStr :: from ( s) ) ,
473
466
Err ( _) => Err ( Error :: invalid_value ( Unexpected :: Bytes ( v) , & self ) ) ,
474
467
}
@@ -478,7 +471,7 @@ mod serde {
478
471
where
479
472
E : Error ,
480
473
{
481
- match std :: str:: from_utf8 ( v) {
474
+ match core :: str:: from_utf8 ( v) {
482
475
Ok ( s) => Ok ( SmolStr :: from ( s) ) ,
483
476
Err ( _) => Err ( Error :: invalid_value ( Unexpected :: Bytes ( v) , & self ) ) ,
484
477
}
0 commit comments