postcard instead of bincode

This commit is contained in:
Dmitry Belyaev 2023-08-23 21:39:23 +03:00
parent 760f6d9415
commit 1d4005abdb
4 changed files with 23 additions and 77 deletions

18
Cargo.lock generated
View File

@ -351,7 +351,6 @@ dependencies = [
"async-compression 0.4.1", "async-compression 0.4.1",
"async-stream", "async-stream",
"async_zip", "async_zip",
"bincode",
"fmmap", "fmmap",
"futures", "futures",
"futures-core", "futures-core",
@ -359,6 +358,7 @@ dependencies = [
"insta", "insta",
"memmap", "memmap",
"pin-project", "pin-project",
"postcard",
"serde", "serde",
"serde_derive", "serde_derive",
"serde_json", "serde_json",
@ -467,6 +467,12 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b"
[[package]]
name = "cobs"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15"
[[package]] [[package]]
name = "colorchoice" name = "colorchoice"
version = "1.0.0" version = "1.0.0"
@ -1248,6 +1254,16 @@ dependencies = [
"plotters-backend", "plotters-backend",
] ]
[[package]]
name = "postcard"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9ee729232311d3cd113749948b689627618133b1c5012b77342c1950b25eaeb"
dependencies = [
"cobs",
"serde",
]
[[package]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.17" version = "0.2.17"

View File

@ -12,40 +12,16 @@ description = "Библиотека для доступа к файлу базы
[features] [features]
default = [] default = []
sync = ["zstd", "memmap"] sync = ["zstd", "memmap"]
async = [ async = ["futures", "futures-core", "futures-util", "fmmap", "tokio", "async-compression", "async-stream", "pin-project"]
"futures",
"futures-core",
"futures-util",
"fmmap",
"tokio",
"async-compression",
"async-stream",
"pin-project",
]
source = ["zip"] source = ["zip"]
source_async = [ source_async = ["async_zip", "tokio", "futures", "futures-core", "futures-util", "async-stream"]
"async_zip",
"tokio",
"futures",
"futures-core",
"futures-util",
"async-stream",
]
convert = ["zip"] convert = ["zip"]
convert_async = [ convert_async = ["futures", "futures-core", "futures-util", "async-stream", "async_zip", "tokio"]
"futures",
"futures-core",
"futures-util",
"async-stream",
"async_zip",
"tokio",
]
[dependencies] [dependencies]
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
serde_json = "1.0" serde_json = "1.0"
bincode = "^2.0.0-rc.2"
zip = { version = "0.6", optional = true } zip = { version = "0.6", optional = true }
async_zip = { version = "0.0.15" , features = [ async_zip = { version = "0.0.15" , features = [
"zstd", "zstd",
@ -70,6 +46,7 @@ async-stream = { version = "0.3", optional = true }
zstd = { version = "^0.12", default-features = false, optional = true } zstd = { version = "^0.12", default-features = false, optional = true }
memmap = { version = "0.7.0", optional = true } memmap = { version = "0.7.0", optional = true }
pin-project = { version = "1.1.3", optional = true } pin-project = { version = "1.1.3", optional = true }
postcard = { version = "1.0.6", default-features = false, features = ["use-std"] }
[dev-dependencies] [dev-dependencies]
insta = { version = "1.31.0", features = ["yaml"] } insta = { version = "1.31.0", features = ["yaml"] }

View File

@ -1,8 +1,6 @@
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
#[derive( #[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
Debug, Default, Clone, Serialize, Deserialize, bincode::Decode, bincode::Encode, PartialEq,
)]
pub struct BatchInfo { pub struct BatchInfo {
#[serde(default, skip_serializing_if = "String::is_empty")] #[serde(default, skip_serializing_if = "String::is_empty")]
pub filename: String, pub filename: String,
@ -32,9 +30,7 @@ pub struct BatchInfo {
pub rating: String, pub rating: String,
} }
#[derive( #[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq)]
Debug, Default, Clone, Serialize, Deserialize, bincode::Decode, bincode::Encode, PartialEq,
)]
pub struct Question { pub struct Question {
#[serde(default, skip_serializing_if = "u32_is_zero")] #[serde(default, skip_serializing_if = "u32_is_zero")]
pub num: u32, pub num: u32,

View File

@ -12,46 +12,3 @@ where
self.map_err(|e| e.to_string()) self.map_err(|e| e.to_string())
} }
} }
#[cfg(any(feature = "sync", feature = "async"))]
mod bincode_utils {
use std::ops::{Deref, DerefMut};
use bincode::enc::write::Writer;
use bincode::error::EncodeError;
/// struct that allows [`Vec<u8>`] to implement [bincode::enc::write::Writer] trait
pub struct BincodeVecWriter {
vec: Vec<u8>,
}
impl BincodeVecWriter {
pub fn new(vec: Vec<u8>) -> BincodeVecWriter {
BincodeVecWriter { vec }
}
}
impl Deref for BincodeVecWriter {
type Target = Vec<u8>;
fn deref(&self) -> &Self::Target {
&self.vec
}
}
impl DerefMut for BincodeVecWriter {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.vec
}
}
impl Writer for BincodeVecWriter {
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
self.vec.extend_from_slice(bytes);
Ok(())
}
}
}
#[cfg(any(feature = "sync", feature = "async"))]
pub use bincode_utils::BincodeVecWriter;