Skip to content

Commit

Permalink
Avoid str path for datetime, date, time, uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Apr 30, 2024
1 parent c89e6d5 commit d81712f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/serialize/per_type/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Serialize for Date {
{
let mut buf = DateTimeBuffer::new();
self.write_buf(&mut buf);
serializer.serialize_str(str_from_slice!(buf.as_ptr(), buf.len()))
serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len()))
}
}

Expand Down Expand Up @@ -124,7 +124,7 @@ impl Serialize for Time {
if self.write_buf(&mut buf).is_err() {
err!(SerializeError::DatetimeLibraryUnsupported)
};
serializer.serialize_str(str_from_slice!(buf.as_ptr(), buf.len()))
serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len()))
}
}

Expand Down Expand Up @@ -243,6 +243,6 @@ impl Serialize for DateTime {
if self.write_buf(&mut buf, self.opts).is_err() {
err!(SerializeError::DatetimeLibraryUnsupported)
}
serializer.serialize_str(str_from_slice!(buf.as_ptr(), buf.len()))
serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len()))
}
}
2 changes: 1 addition & 1 deletion src/serialize/per_type/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ impl Serialize for UUID {
{
let mut buf = arrayvec::ArrayVec::<u8, 36>::new();
self.write_buf(&mut buf);
serializer.serialize_str(str_from_slice!(buf.as_ptr(), buf.len()))
serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len()))
}
}
14 changes: 12 additions & 2 deletions src/serialize/writer/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,18 @@ where
.map_err(Error::io)
}

fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
unreachable!();
#[inline(always)]
fn serialize_unit_struct(self, name: &'static str) -> Result<()> {
debug_assert!(name.len() <= 36);
reserve_minimum!(self.writer);
unsafe {
self.writer.write_reserved_punctuation(b'"').unwrap();
self.writer
.write_reserved_fragment(name.as_bytes())
.unwrap();
self.writer.write_reserved_punctuation(b'"').unwrap();
}
Ok(())
}

fn serialize_unit_variant(
Expand Down

0 comments on commit d81712f

Please sign in to comment.