1
1
//! Serialize a Rust data structure into JSON data
2
2
3
- use core:: marker:: Unsize ;
4
3
use core:: { fmt, mem} ;
5
4
6
5
use serde:: ser;
7
6
8
- use heapless:: { BufferFullError , String , Vec } ;
7
+ use heapless:: { String , Vec } ;
9
8
10
9
use self :: seq:: SerializeSeq ;
11
10
use self :: struct_:: SerializeStruct ;
@@ -25,35 +24,43 @@ pub enum Error {
25
24
__Extensible,
26
25
}
27
26
27
+ impl From < ( ) > for Error {
28
+ fn from ( _: ( ) ) -> Error {
29
+ Error :: BufferFull
30
+ }
31
+ }
32
+
33
+
34
+ impl From < u8 > for Error {
35
+ fn from ( _: u8 ) -> Error {
36
+ Error :: BufferFull
37
+ }
38
+ }
39
+
28
40
#[ cfg( feature = "std" ) ]
29
41
impl :: std:: error:: Error for Error {
30
42
fn description ( & self ) -> & str {
31
43
""
32
44
}
33
45
}
34
46
35
- impl From < BufferFullError > for Error {
36
- fn from ( _: BufferFullError ) -> Self {
37
- Error :: BufferFull
38
- }
39
- }
40
47
41
48
impl fmt:: Display for Error {
42
- fn fmt ( & self , _f : & mut fmt:: Formatter ) -> fmt:: Result {
43
- unreachable ! ( )
49
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
50
+ write ! ( f , "Buffer is full" )
44
51
}
45
52
}
46
53
47
54
pub ( crate ) struct Serializer < B >
48
55
where
49
- B : Unsize < [ u8 ] > ,
56
+ B : heapless :: ArrayLength < u8 > ,
50
57
{
51
58
buf : Vec < u8 , B > ,
52
59
}
53
60
54
61
impl < B > Serializer < B >
55
62
where
56
- B : Unsize < [ u8 ] > ,
63
+ B : heapless :: ArrayLength < u8 > ,
57
64
{
58
65
fn new ( ) -> Self {
59
66
Serializer { buf : Vec :: new ( ) }
@@ -120,7 +127,7 @@ macro_rules! serialize_signed {
120
127
121
128
impl < ' a , B > ser:: Serializer for & ' a mut Serializer < B >
122
129
where
123
- B : Unsize < [ u8 ] > ,
130
+ B : heapless :: ArrayLength < u8 > ,
124
131
{
125
132
type Ok = ( ) ;
126
133
type Error = Error ;
@@ -317,7 +324,7 @@ where
317
324
/// Serializes the given data structure as a string of JSON text
318
325
pub fn to_string < B , T > ( value : & T ) -> Result < String < B > >
319
326
where
320
- B : Unsize < [ u8 ] > ,
327
+ B : heapless :: ArrayLength < u8 > ,
321
328
T : ser:: Serialize + ?Sized ,
322
329
{
323
330
let mut ser = Serializer :: new ( ) ;
@@ -328,7 +335,7 @@ where
328
335
/// Serializes the given data structure as a JSON byte vector
329
336
pub fn to_vec < B , T > ( value : & T ) -> Result < Vec < u8 , B > >
330
337
where
331
- B : Unsize < [ u8 ] > ,
338
+ B : heapless :: ArrayLength < u8 > ,
332
339
T : ser:: Serialize + ?Sized ,
333
340
{
334
341
let mut ser = Serializer :: new ( ) ;
@@ -414,19 +421,20 @@ impl ser::SerializeStructVariant for Unreachable {
414
421
415
422
#[ cfg( test) ]
416
423
mod tests {
417
- const N : usize = 128 ;
424
+ use heapless:: consts:: U128 ;
425
+ type N = U128 ;
418
426
419
427
#[ test]
420
428
fn array ( ) {
421
429
assert_eq ! (
422
- & * super :: to_string:: <[ u8 ; N ] , _>( & [ 0 , 1 , 2 ] ) . unwrap( ) ,
430
+ & * super :: to_string:: <N , _>( & [ 0 , 1 , 2 ] ) . unwrap( ) ,
423
431
"[0,1,2]"
424
432
) ;
425
433
}
426
434
427
435
#[ test]
428
436
fn bool ( ) {
429
- assert_eq ! ( & * super :: to_string:: <[ u8 ; N ] , _>( & true ) . unwrap( ) , "true" ) ;
437
+ assert_eq ! ( & * super :: to_string:: <N , _>( & true ) . unwrap( ) , "true" ) ;
430
438
}
431
439
432
440
#[ test]
@@ -440,20 +448,20 @@ mod tests {
440
448
}
441
449
442
450
assert_eq ! (
443
- & * super :: to_string:: <[ u8 ; N ] , _>( & Type :: Boolean ) . unwrap( ) ,
451
+ & * super :: to_string:: <N , _>( & Type :: Boolean ) . unwrap( ) ,
444
452
r#""boolean""#
445
453
) ;
446
454
447
455
assert_eq ! (
448
- & * super :: to_string:: <[ u8 ; N ] , _>( & Type :: Number ) . unwrap( ) ,
456
+ & * super :: to_string:: <N , _>( & Type :: Number ) . unwrap( ) ,
449
457
r#""number""#
450
458
) ;
451
459
}
452
460
453
461
#[ test]
454
462
fn str ( ) {
455
463
assert_eq ! (
456
- & * super :: to_string:: <[ u8 ; N ] , _>( "hello" ) . unwrap( ) ,
464
+ & * super :: to_string:: <N , _>( "hello" ) . unwrap( ) ,
457
465
r#""hello""#
458
466
) ;
459
467
}
@@ -466,7 +474,7 @@ mod tests {
466
474
}
467
475
468
476
assert_eq ! (
469
- & * super :: to_string:: <[ u8 ; N ] , _>( & Led { led: true } ) . unwrap( ) ,
477
+ & * super :: to_string:: <N , _>( & Led { led: true } ) . unwrap( ) ,
470
478
r#"{"led":true}"#
471
479
) ;
472
480
}
@@ -479,22 +487,22 @@ mod tests {
479
487
}
480
488
481
489
assert_eq ! (
482
- & * super :: to_string:: <[ u8 ; N ] , _>( & Temperature { temperature: 127 } ) . unwrap( ) ,
490
+ & * super :: to_string:: <N , _>( & Temperature { temperature: 127 } ) . unwrap( ) ,
483
491
r#"{"temperature":127}"#
484
492
) ;
485
493
486
494
assert_eq ! (
487
- & * super :: to_string:: <[ u8 ; N ] , _>( & Temperature { temperature: 20 } ) . unwrap( ) ,
495
+ & * super :: to_string:: <N , _>( & Temperature { temperature: 20 } ) . unwrap( ) ,
488
496
r#"{"temperature":20}"#
489
497
) ;
490
498
491
499
assert_eq ! (
492
- & * super :: to_string:: <[ u8 ; N ] , _>( & Temperature { temperature: -17 } ) . unwrap( ) ,
500
+ & * super :: to_string:: <N , _>( & Temperature { temperature: -17 } ) . unwrap( ) ,
493
501
r#"{"temperature":-17}"#
494
502
) ;
495
503
496
504
assert_eq ! (
497
- & * super :: to_string:: <[ u8 ; N ] , _>( & Temperature { temperature: -128 } ) . unwrap( ) ,
505
+ & * super :: to_string:: <N , _>( & Temperature { temperature: -128 } ) . unwrap( ) ,
498
506
r#"{"temperature":-128}"#
499
507
) ;
500
508
}
@@ -507,15 +515,15 @@ mod tests {
507
515
}
508
516
509
517
assert_eq ! (
510
- super :: to_string:: <[ u8 ; N ] , _>( & Property {
518
+ super :: to_string:: <N , _>( & Property {
511
519
description: Some ( "An ambient temperature sensor" ) ,
512
520
} ) . unwrap( ) ,
513
521
r#"{"description":"An ambient temperature sensor"}"#
514
522
) ;
515
523
516
524
// XXX Ideally this should produce "{}"
517
525
assert_eq ! (
518
- super :: to_string:: <[ u8 ; N ] , _>( & Property { description: None } ) . unwrap( ) ,
526
+ super :: to_string:: <N , _>( & Property { description: None } ) . unwrap( ) ,
519
527
r#"{"description":null}"#
520
528
) ;
521
529
}
@@ -528,7 +536,7 @@ mod tests {
528
536
}
529
537
530
538
assert_eq ! (
531
- & * super :: to_string:: <[ u8 ; N ] , _>( & Temperature { temperature: 20 } ) . unwrap( ) ,
539
+ & * super :: to_string:: <N , _>( & Temperature { temperature: 20 } ) . unwrap( ) ,
532
540
r#"{"temperature":20}"#
533
541
) ;
534
542
}
@@ -539,7 +547,7 @@ mod tests {
539
547
struct Empty { }
540
548
541
549
assert_eq ! (
542
- & * super :: to_string:: <[ u8 ; N ] , _>( & Empty { } ) . unwrap( ) ,
550
+ & * super :: to_string:: <N , _>( & Empty { } ) . unwrap( ) ,
543
551
r#"{}"#
544
552
) ;
545
553
@@ -550,7 +558,7 @@ mod tests {
550
558
}
551
559
552
560
assert_eq ! (
553
- & * super :: to_string:: <[ u8 ; N ] , _>( & Tuple { a: true , b: false } ) . unwrap( ) ,
561
+ & * super :: to_string:: <N , _>( & Tuple { a: true , b: false } ) . unwrap( ) ,
554
562
r#"{"a":true,"b":false}"#
555
563
) ;
556
564
}
0 commit comments