Skip to content

Commit

Permalink
add serialization of unit type
Browse files Browse the repository at this point in the history
  • Loading branch information
knickish committed Aug 6, 2023
1 parent aeb8cb1 commit 56e28c9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/serde_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,20 @@ where
}
}

impl SerBin for () {
#[inline(always)]
fn ser_bin(&self, _s: &mut Vec<u8>) {
// do nothing
}
}

impl DeBin for () {
#[inline(always)]
fn de_bin(_o: &mut usize, _d: &[u8]) -> Result<Self, DeBinErr> {
Ok(())
}
}

impl<A, B> SerBin for (A, B)
where
A: SerBin,
Expand Down
14 changes: 14 additions & 0 deletions src/serde_ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,20 @@ where
t
}

impl SerRon for () {
fn ser_ron(&self, _d: usize, s: &mut SerRonState) {
s.out.push_str("()");
}
}

impl DeRon for () {
fn de_ron(s: &mut DeRonState, i: &mut Chars) -> Result<(), DeRonErr> {
s.paren_open(i)?;
s.paren_close(i)?;
Ok(())
}
}

impl<A, B> SerRon for (A, B)
where
A: SerRon,
Expand Down
2 changes: 2 additions & 0 deletions tests/ser_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn ser_de() {
d: Option<String>,
e: Option<HashMap<String, String>>,
f: Option<([u32; 4], String)>,
g: (),
}

let mut map = HashMap::new();
Expand All @@ -24,6 +25,7 @@ fn ser_de() {
d: None,
e: Some(map),
f: Some(([1, 2, 3, 4], "tuple".to_string())),
g: (),
};

let bytes = SerBin::serialize_bin(&test);
Expand Down

0 comments on commit 56e28c9

Please sign in to comment.