From 9deb2b91ea737df79f33f7536587b59ed5f141ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Mon, 24 Jul 2023 15:13:14 +0200 Subject: [PATCH] make Serializer and Deserializer pub and usable (#72) * make Serializer/Deserializer pub This is required when using serde_json_core with code that is generic over the serde implementation, i.e. code that wants to instantiate the Serializer/Deserializer, in the way done in `de::from_slice` and `ser::to_slice`. This also adds public getters for the Serializer.current_length and Deserializer.index members. Two obsolete/unused impls on the `Unreachable` have been removed. * renames * improve docs * bump MSRV to 1.56.0 there is a MSRV bump somwhere in this dependency bump: Updating proc-macro2 v1.0.47 -> v1.0.66 Updating quote v1.0.23 -> v1.0.32 Updating serde v1.0.100 -> v1.0.175 Updating serde_derive v1.0.100 -> v1.0.175 Updating syn v1.0.109 -> v2.0.27 * fix wrong MSRV in README and src/lib.rs --- .github/workflows/ci.yml | 2 +- CHANGELOG.md | 6 +++++ Cargo.toml | 2 +- README.md | 2 +- src/de/mod.rs | 10 +++++--- src/lib.rs | 2 +- src/ser/mod.rs | 50 +++++++++------------------------------- 7 files changed, 28 insertions(+), 46 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 03c31e3f..d2a78cd1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,7 @@ jobs: include: # Test MSRV - - rust: 1.55.0 # keep in sync with manifest rust-version + - rust: 1.56.0 # keep in sync with manifest rust-version TARGET: x86_64-unknown-linux-gnu # Test nightly but don't fail diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ec7a41f..fe115d52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Support for serializing tuple structs. These are serialized as JSON arrays, which matches `serde_json` behaviour. +- `Serializer` and `Deserializer` are now `pub`. +- Added `pub` `Serializer::end()` and `Deserializer::end()`. + +### Changed + +- Increase MSRV to 1.56.0 to work around dependency-MSRV issues (see #72) ## [v0.5.0] - 2022-11-04 diff --git a/Cargo.toml b/Cargo.toml index 8c28537e..64cf80b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ categories = ["no-std"] description = "serde-json for no_std programs" documentation = "https://docs.rs/serde-json-core" edition = "2018" -rust-version = "1.55.0" # keep in sync with ci +rust-version = "1.56.0" # keep in sync with ci, src/lib.rs, and README keywords = ["serde", "json"] license = "MIT OR Apache-2.0" name = "serde-json-core" diff --git a/README.md b/README.md index d8ed05de..07a00f80 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ This project is developed and maintained by the [rust-embedded-community]. ## Minimum Supported Rust Version (MSRV) -This crate is guaranteed to compile on stable Rust 1.51.0 and up. It *might* +This crate is guaranteed to compile on stable Rust 1.56.0 and up. It *might* compile with older versions but that may change in any new patch release. ## License diff --git a/src/de/mod.rs b/src/de/mod.rs index f47f783c..dcfd9bd9 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -79,13 +79,15 @@ pub enum Error { impl serde::de::StdError for Error {} -pub(crate) struct Deserializer<'b> { +/// A structure that deserializes Rust values from JSON in a buffer. +pub struct Deserializer<'b> { slice: &'b [u8], index: usize, } impl<'a> Deserializer<'a> { - fn new(slice: &'a [u8]) -> Deserializer<'_> { + /// Create a new `Deserializer` + pub fn new(slice: &'a [u8]) -> Deserializer<'_> { Deserializer { slice, index: 0 } } @@ -93,7 +95,9 @@ impl<'a> Deserializer<'a> { self.index += 1; } - fn end(&mut self) -> Result { + /// Check whether there is any unexpected data left in the buffer + /// and return the amount of data consumed + pub fn end(&mut self) -> Result { match self.parse_whitespace() { Some(_) => Err(Error::TrailingCharacters), None => Ok(self.index), diff --git a/src/lib.rs b/src/lib.rs index c09c2b93..3ac15832 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,7 +52,7 @@ //! //! # Minimum Supported Rust Version (MSRV) //! -//! This crate is guaranteed to compile on stable Rust 1.51.0 and up. It *might* compile with older +//! This crate is guaranteed to compile on stable Rust 1.56.0 and up. It *might* compile with older //! versions but that may change in any new patch release. #![deny(missing_docs)] diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 3751c626..75153e8d 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -48,19 +48,26 @@ impl fmt::Display for Error { } } -pub(crate) struct Serializer<'a> { +/// A structure that serializes Rust values as JSON into a buffer. +pub struct Serializer<'a> { buf: &'a mut [u8], current_length: usize, } impl<'a> Serializer<'a> { - fn new(buf: &'a mut [u8]) -> Self { + /// Create a new `Serializer` + pub fn new(buf: &'a mut [u8]) -> Self { Serializer { buf, current_length: 0, } } + /// Return the current amount of serialized data in the buffer + pub fn end(&self) -> usize { + self.current_length + } + fn push(&mut self, c: u8) -> Result<()> { if self.current_length < self.buf.len() { unsafe { self.push_unchecked(c) }; @@ -475,20 +482,8 @@ impl ser::Error for Error { } } -pub(crate) enum Unreachable {} - -impl ser::SerializeTupleStruct for Unreachable { - type Ok = (); - type Error = Error; - - fn serialize_field(&mut self, _value: &T) -> Result<()> { - unreachable!() - } - - fn end(self) -> Result { - unreachable!() - } -} +/// An unreachable type to fill the SerializeTupleVariant type +pub enum Unreachable {} impl ser::SerializeTupleVariant for Unreachable { type Ok = (); @@ -503,29 +498,6 @@ impl ser::SerializeTupleVariant for Unreachable { } } -impl ser::SerializeMap for Unreachable { - type Ok = (); - type Error = Error; - - fn serialize_key(&mut self, _key: &T) -> Result<()> - where - T: ser::Serialize, - { - unreachable!() - } - - fn serialize_value(&mut self, _value: &T) -> Result<()> - where - T: ser::Serialize, - { - unreachable!() - } - - fn end(self) -> Result { - unreachable!() - } -} - #[cfg(test)] mod tests { use serde_derive::Serialize;