Skip to content
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
22 changes: 16 additions & 6 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ resolver = "2"
homepage = "https://github.com/loro-dev/columnar"
repository = "https://github.com/loro-dev/columnar"

[workspace.dependencies]
serde = { version = "^1" }

# Added profile settings from fuzz/Cargo.toml
[profile.release]
debug = 1
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ For more detailed introduction, please refer to this `Notion` link: [Serde-Colum

![Image](https://github.com/user-attachments/assets/48475c7d-61c8-4903-892d-70d702137dba)

## 🚧 This crate is in progress and not stable, should not be used in production environments

## Features 🚀

`serde_columnar` comes with several remarkable features:
Expand Down
10 changes: 5 additions & 5 deletions columnar/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde_columnar"
version = "0.3.12"
version = "0.3.13"
edition = "2021"
authors = ["leonzhao <[email protected]>", "zxch3n <[email protected]>"]
license = "MIT OR Apache-2.0"
Expand All @@ -10,17 +10,17 @@ repository = "https://github.com/loro-dev/columnar"
keywords = ["columnar", "column-oriented", "compression", "serde", "compatible"]

[dependencies]
serde = { version = "1.0" }
serde_columnar_derive = { path = "../columnar_derive", version = "0.3.6" }
serde = { workspace = true }
serde_columnar_derive = { path = "../columnar_derive", version = "0.3.7" }
postcard = { version = "^1.1.0", features = ["alloc"] }
thiserror = "1.0"
lazy_static = { version = "1.4", optional = true }
bincode = { version = "1.3.3", optional = true }
itertools = "^0.11.0"
flate2 = { version = "1.0", optional = true }
flate2 = { version = "^1.1", optional = true }

[dev-dependencies]
serde = { version = "1.0.188", features = ["derive"] }
serde = { workspace = true, features = ["derive"] }
criterion = "0.5.1"
serde_json = "1.0"
insta = { version = "1.31.0", features = ["yaml"] }
Expand Down
139 changes: 139 additions & 0 deletions columnar/src/__serde_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use serde::{
de::{Error, Unexpected, Visitor},
Deserializer,
};
use std::borrow::Cow;

pub fn borrow_cow_str<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error>
where
D: Deserializer<'de>,
R: From<Cow<'a, str>>,
{
struct CowStrVisitor;

impl<'a> Visitor<'a> for CowStrVisitor {
type Value = Cow<'a, str>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a string")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Owned(v.to_owned()))
}

fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Borrowed(v))
}

fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Owned(v))
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: Error,
{
match str::from_utf8(v) {
Ok(s) => Ok(Cow::Owned(s.to_owned())),
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
}
}

fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
where
E: Error,
{
match str::from_utf8(v) {
Ok(s) => Ok(Cow::Borrowed(s)),
Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
}
}

fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
where
E: Error,
{
match String::from_utf8(v) {
Ok(s) => Ok(Cow::Owned(s)),
Err(e) => Err(Error::invalid_value(
Unexpected::Bytes(&e.into_bytes()),
&self,
)),
}
}
}

deserializer.deserialize_str(CowStrVisitor).map(From::from)
}

pub fn borrow_cow_bytes<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error>
where
D: Deserializer<'de>,
R: From<Cow<'a, [u8]>>,
{
struct CowBytesVisitor;

impl<'a> Visitor<'a> for CowBytesVisitor {
type Value = Cow<'a, [u8]>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a byte array")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Owned(v.as_bytes().to_vec()))
}

fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Borrowed(v.as_bytes()))
}

fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Owned(v.into_bytes()))
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Owned(v.to_vec()))
}

fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Borrowed(v))
}

fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Owned(v))
}
}

deserializer
.deserialize_bytes(CowBytesVisitor)
.map(From::from)
}
1 change: 1 addition & 0 deletions columnar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub use wrap::{ColumnarMap, ColumnarVec};

pub use postcard::Error as PostcardError;
pub use serde_columnar_derive::*;
pub mod __serde_utils;

#[cfg(feature = "bench")]
extern crate lazy_static;
Expand Down
2 changes: 1 addition & 1 deletion columnar_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde_columnar_derive"
version = "0.3.6"
version = "0.3.7"
edition = "2021"
authors = ["leonzhao <[email protected]>", "zxch3n <[email protected]>"]
license = "MIT OR Apache-2.0"
Expand Down
26 changes: 13 additions & 13 deletions columnar_derive/src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl DeFieldAttrs {
quote::quote!(
let #field_name = {
#wrapper
::serde::__private::Option::map(
std::option::Option::map(
::serde::de::SeqAccess::next_element::<#wrapper_ty>(&mut seq)?,
|__wrap| __wrap.value).ok_or_else(|| __A::Error::custom("DeserializeUnexpectedEnd"))?
};
Expand Down Expand Up @@ -118,9 +118,9 @@ impl DeFieldAttrs {
segments: Punctuated::new(),
};
let span = Span::call_site();
path.segments.push(Ident::new("serde", span).into());
path.segments.push(Ident::new("__private", span).into());
path.segments.push(Ident::new("de", span).into());
path.segments
.push(Ident::new("serde_columnar", span).into());
path.segments.push(Ident::new("__serde_utils", span).into());
path.segments
.push(Ident::new("borrow_cow_str", span).into());
let ans = syn::ExprPath {
Expand All @@ -135,9 +135,9 @@ impl DeFieldAttrs {
segments: Punctuated::new(),
};
let span = Span::call_site();
path.segments.push(Ident::new("serde", span).into());
path.segments.push(Ident::new("__private", span).into());
path.segments.push(Ident::new("de", span).into());
path.segments
.push(Ident::new("serde_columnar", span).into());
path.segments.push(Ident::new("__serde_utils", span).into());
path.segments
.push(Ident::new("borrow_cow_bytes", span).into());
let ans = syn::ExprPath {
Expand Down Expand Up @@ -381,19 +381,19 @@ fn wrap_deserialize_with(
#[doc(hidden)]
struct __DeserializeWith #de_impl_generics #where_clause {
value: #value_ty,
phantom: ::serde::__private::PhantomData<#this_type #ty_generics>,
lifetime: ::serde::__private::PhantomData<&#delife ()>,
phantom: std::marker::PhantomData<#this_type #ty_generics>,
lifetime: std::marker::PhantomData<&#delife ()>,
}

impl #de_impl_generics serde::Deserialize<#delife> for __DeserializeWith #de_ty_generics #where_clause {
fn deserialize<__D>(__deserializer: __D) -> ::serde::__private::Result<Self, __D::Error>
fn deserialize<__D>(__deserializer: __D) -> std::result::Result<Self, __D::Error>
where
__D: serde::Deserializer<#delife>,
{
::serde::__private::Ok(__DeserializeWith {
std::result::Result::Ok(__DeserializeWith {
value: #deserialize_with(__deserializer)?,
phantom: ::serde::__private::PhantomData,
lifetime: ::serde::__private::PhantomData,
phantom: std::marker::PhantomData,
lifetime: std::marker::PhantomData,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde = { workspace = true, features = ["derive"] }
arbitrary = { version = "1.1.7", features = ["derive"] }
postcard = "^1.0"

Expand Down
Loading