Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/serde_bin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::error::Error;
use core::{convert::TryInto, time::Duration};
use core::{convert::TryInto, ops::Range, time::Duration};

use alloc::borrow::ToOwned;
use alloc::boxed::Box;
Expand Down Expand Up @@ -274,6 +274,16 @@ impl DeBin for String {
}
}

impl<T> SerBin for Range<T>
where
T: SerBin,
{
fn ser_bin(&self, s: &mut Vec<u8>) {
self.start.ser_bin(s);
self.end.ser_bin(s);
}
}

impl<T> SerBin for Vec<T>
where
T: SerBin,
Expand All @@ -287,6 +297,17 @@ where
}
}

impl<T> DeBin for Range<T>
where
T: DeBin,
{
fn de_bin(o: &mut usize, d: &[u8]) -> Result<Range<T>, DeBinErr> {
let start: T = DeBin::de_bin(o, d)?;
let end: T = DeBin::de_bin(o, d)?;
Ok(start..end)
}
}

impl<T> DeBin for Vec<T>
where
T: DeBin,
Expand Down
25 changes: 24 additions & 1 deletion src/serde_json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::str::Chars;
use core::{error::Error, time::Duration};
use core::{error::Error, ops::Range, time::Duration};

use alloc::boxed::Box;
use alloc::collections::{BTreeMap, BTreeSet, LinkedList};
Expand Down Expand Up @@ -871,6 +871,19 @@ impl DeJson for String {
}
}

impl<T> SerJson for Range<T>
where
T: SerJson,
{
fn ser_json(&self, d: usize, s: &mut SerJsonState) {
s.out.push('[');
self.start.ser_json(d, s);
s.out.push(',');
self.end.ser_json(d, s);
s.out.push(']');
}
}

impl<T> SerJson for Vec<T>
where
T: SerJson,
Expand All @@ -891,6 +904,16 @@ where
}
}

impl<T> DeJson for Range<T>
where
T: DeJson,
{
fn de_json(s: &mut DeJsonState, i: &mut Chars) -> Result<Self, DeJsonErr> {
let (start, end) = <(T, T)>::de_json(s, i)?;
Ok(start..end)
}
}

impl<T> DeJson for Vec<T>
where
T: DeJson,
Expand Down
25 changes: 24 additions & 1 deletion src/serde_ron.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::str::Chars;
use core::{error::Error, time::Duration};
use core::{error::Error, ops::Range, time::Duration};

use alloc::boxed::Box;
use alloc::collections::{BTreeMap, BTreeSet, LinkedList};
Expand Down Expand Up @@ -865,6 +865,19 @@ impl DeRon for String {
}
}

impl<T> SerRon for Range<T>
where
T: SerRon,
{
fn ser_ron(&self, d: usize, s: &mut SerRonState) {
s.out.push('(');
self.start.ser_ron(d, s);
s.out.push(',');
self.end.ser_ron(d, s);
s.out.push(')');
}
}

impl<T> SerRon for Vec<T>
where
T: SerRon,
Expand All @@ -881,6 +894,16 @@ where
}
}

impl<T> DeRon for Range<T>
where
T: DeRon,
{
fn de_ron(s: &mut DeRonState, i: &mut Chars) -> Result<Self, DeRonErr> {
let (start, end) = <(T, T)>::de_ron(s, i)?;
Ok(start..end)
}
}

impl<T> DeRon for Vec<T>
where
T: DeRon,
Expand Down
4 changes: 3 additions & 1 deletion tests/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

extern crate alloc;

use std::{array, sync::atomic::AtomicBool};
use std::{array, ops::Range, sync::atomic::AtomicBool};

use alloc::collections::{BTreeMap, BTreeSet, LinkedList};

Expand Down Expand Up @@ -256,13 +256,15 @@ fn collections() {
pub b: LinkedList<f32>,
pub c: BTreeMap<i32, i32>,
pub d: BTreeSet<i32>,
pub e: Range<i32>,
}

let test: Test = Test {
a: vec![1, 2, 3],
b: vec![1.0, 2.0, 3.0, 4.0].into_iter().collect(),
c: vec![(1, 2), (3, 4)].into_iter().collect(),
d: vec![1, 2, 3, 4, 5, 6].into_iter().collect(),
e: 1..3,
};

let bytes = SerBin::serialize_bin(&test);
Expand Down
3 changes: 3 additions & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use nanoserde::{DeJson, DeJsonErrReason, SerJson};
use std::{
collections::{BTreeMap, BTreeSet, LinkedList},
fmt::Debug,
ops::Range,
sync::atomic::AtomicBool,
};

Expand Down Expand Up @@ -656,13 +657,15 @@ fn collections() {
pub b: LinkedList<f32>,
pub c: BTreeMap<i32, i32>,
pub d: BTreeSet<i32>,
pub e: Range<i32>,
}

let test: Test = Test {
a: vec![1, 2, 3],
b: vec![1.0, 2.0, 3.0, 4.0].into_iter().collect(),
c: vec![(1, 2), (3, 4)].into_iter().collect(),
d: vec![1, 2, 3, 4, 5, 6].into_iter().collect(),
e: 1..3,
};

let bytes = SerJson::serialize_json(&test);
Expand Down
3 changes: 3 additions & 0 deletions tests/ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use nanoserde::{DeRon, DeRonErrReason, SerRon};

use std::{
collections::{BTreeMap, BTreeSet, LinkedList},
ops::Range,
sync::atomic::AtomicBool,
};

Expand Down Expand Up @@ -277,13 +278,15 @@ fn collections() {
pub b: LinkedList<f32>,
pub c: BTreeMap<i32, i32>,
pub d: BTreeSet<i32>,
pub e: Range<i32>,
}

let test: Test = Test {
a: vec![1, 2, 3],
b: vec![1.0, 2.0, 3.0, 4.0].into_iter().collect(),
c: vec![(1, 2), (3, 4)].into_iter().collect(),
d: vec![1, 2, 3, 4, 5, 6].into_iter().collect(),
e: 1..3,
};

let bytes = SerRon::serialize_ron(&test);
Expand Down