Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deserialize into Bytes bug and clippy #68

Merged
merged 3 commits into from
Mar 13, 2024
Merged
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
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sonic-rs"
version = "0.3.3"
version = "0.3.4"
authors = ["Volo Team <[email protected]>"]
edition = "2021"
description = "Sonic-rs is a fast Rust JSON library based on SIMD"
Expand Down Expand Up @@ -42,6 +42,7 @@ faststr = "0.2"
json-benchmark = { git = "https://github.com/serde-rs/json-benchmark", default-features = false, features = ["all-files", "lib-serde"]}
paste = "1.0"
serde_bytes = "0.11"
bytes = {version = "1.4", features = ["serde"]}
chrono = { version = "0.4", features = ["serde"] }

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fuzz/fuzz_targets/from_slice.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_main]
#![allow(clippy::mutable_key_type)]

use libfuzzer_sys::fuzz_target;
use serde_json::Value as JValue;
Expand Down
7 changes: 1 addition & 6 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ use core::{
fmt::{self, Debug, Display},
result,
};
use std::{
borrow::Cow,
error,
str::FromStr,
string::{String, ToString},
};
use std::{borrow::Cow, error, str::FromStr};

use serde::{de, ser};
use thiserror::Error as ErrorTrait;
Expand Down
2 changes: 0 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::Into;

use crate::{
util::{private::Sealed, reborrow::DormantMutRef},
value::{
Expand Down
2 changes: 1 addition & 1 deletion src/lazyvalue/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl<'a> LazyValue<'a> {
#[cfg(test)]
mod test {
use super::*;
use crate::{get_unchecked, pointer, to_array_iter, value::JsonValueTrait};
use crate::{pointer, to_array_iter};

const TEST_JSON: &str = r#"{
"bool": true,
Expand Down
2 changes: 0 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ macro_rules! check_visit {
};
}

pub(crate) use perr;

#[inline(always)]
fn get_escaped_branchless_u64(prev_escaped: &mut u64, backslash: u64) -> u64 {
const EVEN_BITS: u64 = 0x5555_5555_5555_5555;
Expand Down
5 changes: 4 additions & 1 deletion src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,10 @@ impl<'de, 'a, R: Reader<'de>> de::Deserializer<'de> for &'a mut Deserializer<R>
Reference::Borrowed(b) => visitor.visit_borrowed_bytes(b),
Reference::Copied(b) => visitor.visit_bytes(b),
},
b'[' => self.deserialize_seq(visitor),
b'[' => {
self.parser.read.backward(1);
self.deserialize_seq(visitor)
}
_ => Err(self.peek_invalid_type(peek, &visitor)),
};

Expand Down
26 changes: 26 additions & 0 deletions src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use self::{
mod test {
use std::{borrow::Cow, collections::HashMap, hash::Hash, marker::PhantomData};

use bytes::Bytes;
use faststr::FastStr;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -306,6 +307,11 @@ mod test {
assert_eq!(sv, jv);
}
Err(err) => {
println!(
"parse invalid json {:?} failed for type {}",
$data,
stringify!($ty)
);
let _ = crate::from_slice::<$ty>($data).expect_err(&format!(
"parse invalid json {:?} wrong for type {}, should error: {}",
$data,
Expand All @@ -317,6 +323,14 @@ mod test {
};
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct Data {
#[serde(with = "serde_bytes")]
pub content: Vec<u8>,
}

use serde_bytes::ByteBuf;

// the testcase is found by fuzzing tests
#[test]
fn test_more_structs() {
Expand All @@ -326,5 +340,17 @@ mod test {
test_struct!(String, &[34, 92, 34, 34]);
test_struct!(String, b"\"\\umap9map009\"");
test_struct!(Foo, &b"[\"5XXXXXXZX:XXZX:[\",-0]"[..]);
test_struct!(Bytes, &b"\"hello world\""[..]);
test_struct!(
Bytes,
&b"[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]"[..]
);
test_struct!(
ByteBuf,
&b"[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]"[..]
);
test_struct!(ByteBuf, &b"\"hello world\""[..]);
test_struct!(Bytes, &b"[]"[..]);
test_struct!(Data, &br#"{"content":[1,2,3,4,5]}"#[..]);
}
}
6 changes: 1 addition & 5 deletions src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use core::{
fmt::{self, Display},
num::FpCategory,
};
use std::{
io,
string::{String, ToString},
vec::Vec,
};
use std::io;

use ::serde::ser::{self, Impossible, Serialize};

Expand Down
2 changes: 0 additions & 2 deletions src/util/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ mod lemire;
mod slow;
mod table;

use std::result::Result;

use self::{common::BiasedFp, float::RawFloat, table::POWER_OF_FIVE_128};
use crate::{error::ErrorCode, util::arch::simd_str2int};

Expand Down
2 changes: 1 addition & 1 deletion src/value/from.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, convert::Into, fmt::Debug, str::FromStr};
use std::{borrow::Cow, fmt::Debug, str::FromStr};

use faststr::FastStr;

Expand Down
5 changes: 1 addition & 4 deletions src/value/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1727,10 +1727,7 @@ mod test {
use std::path::Path;

use super::*;
use crate::{
error::{make_error, Result},
from_slice, from_str, pointer,
};
use crate::{error::make_error, from_slice, from_str, pointer};

#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)]
struct ValueInStruct {
Expand Down
2 changes: 1 addition & 1 deletion src/value/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ impl<'de> serde::de::Deserialize<'de> for Object {
#[cfg(test)]
mod test {
use super::*;
use crate::{from_str, object, to_string, Array, JsonValueMutTrait};
use crate::{from_str, to_string, Array, JsonValueMutTrait};

#[test]
fn test_object_serde() {
Expand Down
1 change: 0 additions & 1 deletion src/value/partial_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ impl_container_eq!(Array Object);
mod test {
use faststr::FastStr;

use crate::{array, json};
#[test]
fn test_slice_eq() {
assert_eq!(json!([1, 2, 3]), &[1, 2, 3]);
Expand Down
2 changes: 0 additions & 2 deletions src/value/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ impl Serializer {
}
}

use std::string::ToString;

use crate::serde::tri;

impl serde::Serializer for Serializer {
Expand Down
Loading