Skip to content

Commit e0ff11c

Browse files
committed
Use const generics and update heapless to 0.7
1 parent 8961910 commit e0ff11c

File tree

3 files changed

+59
-58
lines changed

3 files changed

+59
-58
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- [breaking-change] use `const_generics` in `to_string()` and `to_vec()` functions.
13+
- [breaking-change] update to `heapless` `0.7`.
14+
1015
### Added
1116

1217
- Support for opting out of heapless integration

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ version = "0.3.0"
1515
ryu = "1.0.5"
1616

1717
[dependencies.heapless]
18-
version = "0.6.1"
18+
version = "0.7"
1919
optional = true
2020

2121
[dependencies.serde]

src/ser/mod.rs

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Serialize a Rust data structure into JSON data
22
3-
use core::fmt;
3+
use core::{fmt, str};
44

55
use serde::ser;
66
use serde::ser::SerializeStruct as _;
@@ -425,23 +425,21 @@ impl<'a, 'b: 'a> ser::Serializer for &'a mut Serializer<'b> {
425425

426426
/// Serializes the given data structure as a string of JSON text
427427
#[cfg(feature = "heapless")]
428-
pub fn to_string<B, T>(value: &T) -> Result<String<B>>
428+
pub fn to_string<T, const N: usize>(value: &T) -> Result<String<N>>
429429
where
430-
B: heapless::ArrayLength<u8>,
431430
T: ser::Serialize + ?Sized,
432431
{
433-
Ok(unsafe { String::from_utf8_unchecked(to_vec(value)?) })
432+
Ok(unsafe { str::from_utf8_unchecked(&to_vec::<T, N>(value)?) }.into())
434433
}
435434

436435
/// Serializes the given data structure as a JSON byte vector
437436
#[cfg(feature = "heapless")]
438-
pub fn to_vec<B, T>(value: &T) -> Result<Vec<u8, B>>
437+
pub fn to_vec<T, const N: usize>(value: &T) -> Result<Vec<u8, N>>
439438
where
440-
B: heapless::ArrayLength<u8>,
441439
T: ser::Serialize + ?Sized,
442440
{
443-
let mut buf = Vec::<u8, B>::new();
444-
buf.resize_default(B::to_usize())?;
441+
let mut buf = Vec::<u8, N>::new();
442+
buf.resize_default(N)?;
445443
let len = to_slice(value, &mut buf)?;
446444
buf.truncate(len);
447445
Ok(buf)
@@ -521,17 +519,15 @@ impl ser::SerializeMap for Unreachable {
521519
mod tests {
522520
use serde_derive::Serialize;
523521

524-
use heapless::consts::U128;
525-
526-
type N = U128;
522+
const N: usize = 128;
527523

528524
#[test]
529525
fn array() {
530526
let buf = &mut [0u8; 128];
531527
let len = crate::to_slice(&[0, 1, 2], buf).unwrap();
532528
assert_eq!(len, 7);
533529
assert_eq!(&buf[..len], b"[0,1,2]");
534-
assert_eq!(&*crate::to_string::<N, _>(&[0, 1, 2]).unwrap(), "[0,1,2]");
530+
assert_eq!(&*crate::to_string::<_, N>(&[0, 1, 2]).unwrap(), "[0,1,2]");
535531
}
536532

537533
#[test]
@@ -541,7 +537,7 @@ mod tests {
541537
assert_eq!(len, 4);
542538
assert_eq!(&buf[..len], b"true");
543539

544-
assert_eq!(&*crate::to_string::<N, _>(&true).unwrap(), "true");
540+
assert_eq!(&*crate::to_string::<_, N>(&true).unwrap(), "true");
545541
}
546542

547543
#[test]
@@ -555,83 +551,83 @@ mod tests {
555551
}
556552

557553
assert_eq!(
558-
&*crate::to_string::<N, _>(&Type::Boolean).unwrap(),
554+
&*crate::to_string::<_, N>(&Type::Boolean).unwrap(),
559555
r#""boolean""#
560556
);
561557

562558
assert_eq!(
563-
&*crate::to_string::<N, _>(&Type::Number).unwrap(),
559+
&*crate::to_string::<_, N>(&Type::Number).unwrap(),
564560
r#""number""#
565561
);
566562
}
567563

568564
#[test]
569565
fn str() {
570-
assert_eq!(&*crate::to_string::<N, _>("hello").unwrap(), r#""hello""#);
571-
assert_eq!(&*crate::to_string::<N, _>("").unwrap(), r#""""#);
566+
assert_eq!(&*crate::to_string::<_, N>("hello").unwrap(), r#""hello""#);
567+
assert_eq!(&*crate::to_string::<_, N>("").unwrap(), r#""""#);
572568

573569
// Characters unescaped if possible
574-
assert_eq!(&*crate::to_string::<N, _>("ä").unwrap(), r#""ä""#);
575-
assert_eq!(&*crate::to_string::<N, _>("৬").unwrap(), r#""৬""#);
576-
// assert_eq!(&*crate::to_string::<N, _>("\u{A0}").unwrap(), r#"" ""#); // non-breaking space
577-
assert_eq!(&*crate::to_string::<N, _>("ℝ").unwrap(), r#""ℝ""#); // 3 byte character
578-
assert_eq!(&*crate::to_string::<N, _>("💣").unwrap(), r#""💣""#); // 4 byte character
570+
assert_eq!(&*crate::to_string::<_, N>("ä").unwrap(), r#""ä""#);
571+
assert_eq!(&*crate::to_string::<_, N>("৬").unwrap(), r#""৬""#);
572+
// assert_eq!(&*crate::to_string::<_, N>("\u{A0}").unwrap(), r#"" ""#); // non-breaking space
573+
assert_eq!(&*crate::to_string::<_, N>("ℝ").unwrap(), r#""ℝ""#); // 3 byte character
574+
assert_eq!(&*crate::to_string::<_, N>("💣").unwrap(), r#""💣""#); // 4 byte character
579575

580576
// " and \ must be escaped
581577
assert_eq!(
582-
&*crate::to_string::<N, _>("foo\"bar").unwrap(),
578+
&*crate::to_string::<_, N>("foo\"bar").unwrap(),
583579
r#""foo\"bar""#
584580
);
585581
assert_eq!(
586-
&*crate::to_string::<N, _>("foo\\bar").unwrap(),
582+
&*crate::to_string::<_, N>("foo\\bar").unwrap(),
587583
r#""foo\\bar""#
588584
);
589585

590586
// \b, \t, \n, \f, \r must be escaped in their two-character escaping
591587
assert_eq!(
592-
&*crate::to_string::<N, _>(" \u{0008} ").unwrap(),
588+
&*crate::to_string::<_, N>(" \u{0008} ").unwrap(),
593589
r#"" \b ""#
594590
);
595591
assert_eq!(
596-
&*crate::to_string::<N, _>(" \u{0009} ").unwrap(),
592+
&*crate::to_string::<_, N>(" \u{0009} ").unwrap(),
597593
r#"" \t ""#
598594
);
599595
assert_eq!(
600-
&*crate::to_string::<N, _>(" \u{000A} ").unwrap(),
596+
&*crate::to_string::<_, N>(" \u{000A} ").unwrap(),
601597
r#"" \n ""#
602598
);
603599
assert_eq!(
604-
&*crate::to_string::<N, _>(" \u{000C} ").unwrap(),
600+
&*crate::to_string::<_, N>(" \u{000C} ").unwrap(),
605601
r#"" \f ""#
606602
);
607603
assert_eq!(
608-
&*crate::to_string::<N, _>(" \u{000D} ").unwrap(),
604+
&*crate::to_string::<_, N>(" \u{000D} ").unwrap(),
609605
r#"" \r ""#
610606
);
611607

612608
// U+0000 through U+001F is escaped using six-character \u00xx uppercase hexadecimal escape sequences
613609
assert_eq!(
614-
&*crate::to_string::<N, _>(" \u{0000} ").unwrap(),
610+
&*crate::to_string::<_, N>(" \u{0000} ").unwrap(),
615611
r#"" \u0000 ""#
616612
);
617613
assert_eq!(
618-
&*crate::to_string::<N, _>(" \u{0001} ").unwrap(),
614+
&*crate::to_string::<_, N>(" \u{0001} ").unwrap(),
619615
r#"" \u0001 ""#
620616
);
621617
assert_eq!(
622-
&*crate::to_string::<N, _>(" \u{0007} ").unwrap(),
618+
&*crate::to_string::<_, N>(" \u{0007} ").unwrap(),
623619
r#"" \u0007 ""#
624620
);
625621
assert_eq!(
626-
&*crate::to_string::<N, _>(" \u{000e} ").unwrap(),
622+
&*crate::to_string::<_, N>(" \u{000e} ").unwrap(),
627623
r#"" \u000E ""#
628624
);
629625
assert_eq!(
630-
&*crate::to_string::<N, _>(" \u{001D} ").unwrap(),
626+
&*crate::to_string::<_, N>(" \u{001D} ").unwrap(),
631627
r#"" \u001D ""#
632628
);
633629
assert_eq!(
634-
&*crate::to_string::<N, _>(" \u{001f} ").unwrap(),
630+
&*crate::to_string::<_, N>(" \u{001f} ").unwrap(),
635631
r#"" \u001F ""#
636632
);
637633
}
@@ -644,7 +640,7 @@ mod tests {
644640
}
645641

646642
assert_eq!(
647-
&*crate::to_string::<N, _>(&Led { led: true }).unwrap(),
643+
&*crate::to_string::<_, N>(&Led { led: true }).unwrap(),
648644
r#"{"led":true}"#
649645
);
650646
}
@@ -657,22 +653,22 @@ mod tests {
657653
}
658654

659655
assert_eq!(
660-
&*crate::to_string::<N, _>(&Temperature { temperature: 127 }).unwrap(),
656+
&*crate::to_string::<_, N>(&Temperature { temperature: 127 }).unwrap(),
661657
r#"{"temperature":127}"#
662658
);
663659

664660
assert_eq!(
665-
&*crate::to_string::<N, _>(&Temperature { temperature: 20 }).unwrap(),
661+
&*crate::to_string::<_, N>(&Temperature { temperature: 20 }).unwrap(),
666662
r#"{"temperature":20}"#
667663
);
668664

669665
assert_eq!(
670-
&*crate::to_string::<N, _>(&Temperature { temperature: -17 }).unwrap(),
666+
&*crate::to_string::<_, N>(&Temperature { temperature: -17 }).unwrap(),
671667
r#"{"temperature":-17}"#
672668
);
673669

674670
assert_eq!(
675-
&*crate::to_string::<N, _>(&Temperature { temperature: -128 }).unwrap(),
671+
&*crate::to_string::<_, N>(&Temperature { temperature: -128 }).unwrap(),
676672
r#"{"temperature":-128}"#
677673
);
678674
}
@@ -685,20 +681,20 @@ mod tests {
685681
}
686682

687683
assert_eq!(
688-
&*crate::to_string::<N, _>(&Temperature { temperature: -20. }).unwrap(),
684+
&*crate::to_string::<_, N>(&Temperature { temperature: -20. }).unwrap(),
689685
r#"{"temperature":-20.0}"#
690686
);
691687

692688
assert_eq!(
693-
&*crate::to_string::<N, _>(&Temperature {
689+
&*crate::to_string::<_, N>(&Temperature {
694690
temperature: -20345.
695691
})
696692
.unwrap(),
697693
r#"{"temperature":-20345.0}"#
698694
);
699695

700696
assert_eq!(
701-
&*crate::to_string::<N, _>(&Temperature {
697+
&*crate::to_string::<_, N>(&Temperature {
702698
temperature: -2.3456789012345e-23
703699
})
704700
.unwrap(),
@@ -714,7 +710,7 @@ mod tests {
714710
}
715711

716712
assert_eq!(
717-
crate::to_string::<N, _>(&Property {
713+
crate::to_string::<_, N>(&Property {
718714
description: Some("An ambient temperature sensor"),
719715
})
720716
.unwrap(),
@@ -723,7 +719,7 @@ mod tests {
723719

724720
// XXX Ideally this should produce "{}"
725721
assert_eq!(
726-
crate::to_string::<N, _>(&Property { description: None }).unwrap(),
722+
crate::to_string::<_, N>(&Property { description: None }).unwrap(),
727723
r#"{"description":null}"#
728724
);
729725
}
@@ -736,7 +732,7 @@ mod tests {
736732
}
737733

738734
assert_eq!(
739-
&*crate::to_string::<N, _>(&Temperature { temperature: 20 }).unwrap(),
735+
&*crate::to_string::<_, N>(&Temperature { temperature: 20 }).unwrap(),
740736
r#"{"temperature":20}"#
741737
);
742738
}
@@ -746,7 +742,7 @@ mod tests {
746742
#[derive(Serialize)]
747743
struct Empty {}
748744

749-
assert_eq!(&*crate::to_string::<N, _>(&Empty {}).unwrap(), r#"{}"#);
745+
assert_eq!(&*crate::to_string::<_, N>(&Empty {}).unwrap(), r#"{}"#);
750746

751747
#[derive(Serialize)]
752748
struct Tuple {
@@ -755,23 +751,23 @@ mod tests {
755751
}
756752

757753
assert_eq!(
758-
&*crate::to_string::<N, _>(&Tuple { a: true, b: false }).unwrap(),
754+
&*crate::to_string::<_, N>(&Tuple { a: true, b: false }).unwrap(),
759755
r#"{"a":true,"b":false}"#
760756
);
761757
}
762758

763759
#[test]
764760
fn test_unit() {
765761
let a = ();
766-
assert_eq!(&*crate::to_string::<N, _>(&a).unwrap(), r#"null"#);
762+
assert_eq!(&*crate::to_string::<_, N>(&a).unwrap(), r#"null"#);
767763
}
768764

769765
#[test]
770766
fn test_newtype_struct() {
771767
#[derive(Serialize)]
772768
struct A(pub u32);
773769
let a = A(54);
774-
assert_eq!(&*crate::to_string::<N, _>(&a).unwrap(), r#"54"#);
770+
assert_eq!(&*crate::to_string::<_, N>(&a).unwrap(), r#"54"#);
775771
}
776772

777773
#[test]
@@ -782,7 +778,7 @@ mod tests {
782778
}
783779
let a = A::A(54);
784780

785-
assert_eq!(&*crate::to_string::<N, _>(&a).unwrap(), r#"{"A":54}"#);
781+
assert_eq!(&*crate::to_string::<_, N>(&a).unwrap(), r#"{"A":54}"#);
786782
}
787783

788784
#[test]
@@ -794,15 +790,15 @@ mod tests {
794790
let a = A::A { x: 54, y: 720 };
795791

796792
assert_eq!(
797-
&*crate::to_string::<N, _>(&a).unwrap(),
793+
&*crate::to_string::<_, N>(&a).unwrap(),
798794
r#"{"A":{"x":54,"y":720}}"#
799795
);
800796
}
801797

802798
#[test]
803799
fn test_serialize_bytes() {
804800
use core::fmt::Write;
805-
use heapless::{consts::U48, String};
801+
use heapless::String;
806802

807803
pub struct SimpleDecimal(f32);
808804

@@ -811,19 +807,19 @@ mod tests {
811807
where
812808
S: serde::Serializer,
813809
{
814-
let mut aux: String<U48> = String::new();
810+
let mut aux: String<{ N }> = String::new();
815811
write!(aux, "{:.2}", self.0).unwrap();
816812
serializer.serialize_bytes(&aux.as_bytes())
817813
}
818814
}
819815

820816
let sd1 = SimpleDecimal(1.55555);
821-
assert_eq!(&*crate::to_string::<N, _>(&sd1).unwrap(), r#"1.56"#);
817+
assert_eq!(&*crate::to_string::<_, N>(&sd1).unwrap(), r#"1.56"#);
822818

823819
let sd2 = SimpleDecimal(0.000);
824-
assert_eq!(&*crate::to_string::<N, _>(&sd2).unwrap(), r#"0.00"#);
820+
assert_eq!(&*crate::to_string::<_, N>(&sd2).unwrap(), r#"0.00"#);
825821

826822
let sd3 = SimpleDecimal(22222.777777);
827-
assert_eq!(&*crate::to_string::<N, _>(&sd3).unwrap(), r#"22222.78"#);
823+
assert_eq!(&*crate::to_string::<_, N>(&sd3).unwrap(), r#"22222.78"#);
828824
}
829825
}

0 commit comments

Comments
 (0)