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

WIP: Add generic async support #337

Draft
wants to merge 47 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
d4d92f7
WIP: implement support for async I/O. FIXME: resolve issue with type …
DanikVitek Jan 21, 2025
23a292f
resolved issue with type parameters needing `Send` bound
DanikVitek Jan 21, 2025
5b7f19d
remove redundant `cfg_attr`s
DanikVitek Jan 21, 2025
cf2807c
Implement async I/O for all standard types. TODO: derive, etc.
DanikVitek Jan 21, 2025
447d7bb
remove `async-generic` version
DanikVitek Jan 21, 2025
984b16d
Merge remote-tracking branch 'upstream/master'
DanikVitek Jan 22, 2025
31a4c24
Remove dependency on sync counterparts
DanikVitek Jan 23, 2025
d484aa4
use `impl Future` for async traits
DanikVitek Jan 27, 2025
a2526e2
Implement async traits, using `impl Future`
DanikVitek Jan 27, 2025
ecdbd3c
inline inner `as_bytes` fns; use `write_u32` for `len`
DanikVitek Jan 27, 2025
037bc31
Update workflow Rust version
DanikVitek Jan 27, 2025
e5216c5
Formatting & remove redundant imports
DanikVitek Jan 27, 2025
bb3d210
Rollback remove unused imports. Add `#[allow(unused_imports)]`
DanikVitek Jan 27, 2025
7dacd4c
Add support for mutex/rwlock (de-)serialization (including async coun…
DanikVitek Jan 28, 2025
e322cb5
Fix docs
DanikVitek Jan 28, 2025
6e250a5
set patch `syn` version
DanikVitek Jan 28, 2025
0d0d550
Manually declare async trait variants. Update `async_generic` usage
DanikVitek Jan 28, 2025
a677a17
Fix formatting in macros
DanikVitek Jan 28, 2025
48f7d09
Fixate `async-generic` commit version
DanikVitek Jan 28, 2025
d2ea430
Fixate `async-generic` commit version
DanikVitek Jan 28, 2025
0c85a18
Box errors instead of strings
DanikVitek Jan 28, 2025
09735b2
Remove support locks and unnecessary trait bounds
DanikVitek Jan 28, 2025
f9aed6b
Remove unnecessary trait bounds
DanikVitek Jan 28, 2025
ae05c33
Remove wrong `#[cfg]`
DanikVitek Jan 28, 2025
c57e5e6
use `cfg!` for io error creation
DanikVitek Jan 29, 2025
5da74f3
move private `fn _new` into `pub fn new`
DanikVitek Jan 29, 2025
22bb75b
Fix `async-generic` revision specification
DanikVitek Jan 29, 2025
dc317ad
Partial revert
DanikVitek Jan 29, 2025
8ff7681
replace `cfg!` with `#[cfg]`
DanikVitek Jan 29, 2025
97febbe
move `ToString` import
DanikVitek Jan 29, 2025
a34c3e5
Fix macros formatting issues
DanikVitek Jan 29, 2025
b7f7fec
use `crate::io::Result`
DanikVitek Jan 29, 2025
43bec58
WIP: implement derive macro for async traits
DanikVitek Jan 29, 2025
d64ff6b
Rename async-related features
DanikVitek Jan 29, 2025
22b2bde
use `Cow<'static, str>` in place of `String` to reduce cloning
DanikVitek Jan 29, 2025
c142a0f
Merge pull request #2 from DanikVitek/master
DanikVitek Jan 29, 2025
b98e23d
formatting
DanikVitek Jan 29, 2025
7358d3a
Merge pull request #3 from DanikVitek/master
DanikVitek Jan 29, 2025
a48e9fa
add `pub use` for derive macros for async traits
DanikVitek Jan 29, 2025
2891864
Code cleanup. Reduce `quote!` usage where unnecessary. WIP: update de…
DanikVitek Jan 30, 2025
42fd2b2
Add compilation tests
DanikVitek Jan 30, 2025
8001084
Add default `read_exact` impl. Fix doc test
DanikVitek Jan 30, 2025
2d1c059
Merge pull request #4 from DanikVitek/master
DanikVitek Jan 30, 2025
4e04e09
Fix doc tests
DanikVitek Jan 30, 2025
e13e6ef
Add tests. Use `write_u8` for `variant_idx` in `BorshSerializeAsync` …
DanikVitek Jan 31, 2025
6c78116
fmt
DanikVitek Jan 31, 2025
a13066c
Merge pull request #1 from DanikVitek/async/derive
DanikVitek Jan 31, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
tests:
strategy:
matrix:
rust_version: [1.67, stable]
rust_version: [1.75.0, stable]
runs-on: ubuntu-20.04

steps:
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[workspace]
resolver = "2"
members = ["borsh", "borsh-derive", "fuzz/fuzz-run", "benchmarks"]

[workspace.package]
# shared version of all public crates in the workspace
version = "1.5.5"
rust-version = "1.67.0"
edition = "2021"
rust-version = "1.75.0"
2 changes: 1 addition & 1 deletion benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "benchmarks"
version.workspace = true
rust-version.workspace = true
authors = ["Near Inc <[email protected]>"]
edition = "2018"
edition.workspace = true
publish = false

# This is somehow needed for command line arguments to work: https://github.com/bheisler/criterion.rs/issues/193#issuecomment-415740713
Expand Down
10 changes: 5 additions & 5 deletions borsh-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "borsh-derive"
version.workspace = true
rust-version.workspace = true
authors = ["Near Inc <[email protected]>"]
edition = "2018"
edition.workspace = true
license = "Apache-2.0"
readme = "README.md"
categories = ["encoding", "network-programming"]
Expand All @@ -18,14 +18,14 @@ exclude = ["*.snap"]
proc-macro = true

[dependencies]
syn = { version = "2.0.81", features = ["full", "fold"] }
syn = { version = "2.0.96", features = ["full", "fold"] }
proc-macro-crate = "3"
proc-macro2 = "1"
quote = "1"
proc-macro2 = "1.0"
quote = "1.0"
once_cell = "1.18.0"

[dev-dependencies]
syn = { version = "2.0.81", features = ["full", "fold", "parsing"] }
syn = { version = "2.0.96", features = ["parsing"] }
prettyplease = "0.2.9"
insta = "1.29.0"

Expand Down
17 changes: 4 additions & 13 deletions borsh-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ pub fn borsh_serialize(input: TokenStream) -> TokenStream {
// Derive macros can only be defined on structs, enums, and unions.
unreachable!()
};
TokenStream::from(match res {
Ok(res) => res,
Err(err) => err.to_compile_error(),
})
TokenStream::from(res.unwrap_or_else(|err| err.to_compile_error()))
}

/// ---
Expand All @@ -80,10 +77,7 @@ pub fn borsh_deserialize(input: TokenStream) -> TokenStream {
// Derive macros can only be defined on structs, enums, and unions.
unreachable!()
};
TokenStream::from(match res {
Ok(res) => res,
Err(err) => err.to_compile_error(),
})
TokenStream::from(res.unwrap_or_else(|err| err.to_compile_error()))
}

/// ---
Expand All @@ -104,16 +98,13 @@ pub fn borsh_schema(input: TokenStream) -> TokenStream {
} else if let Ok(input) = syn::parse::<ItemEnum>(input.clone()) {
schema::enums::process(&input, cratename)
} else if syn::parse::<ItemUnion>(input).is_ok() {
Err(syn::Error::new(
Err(Error::new(
Span::call_site(),
"Borsh schema does not support unions yet.",
))
} else {
// Derive macros can only be defined on structs, enums, and unions.
unreachable!()
};
TokenStream::from(match res {
Ok(res) => res,
Err(err) => err.to_compile_error(),
})
TokenStream::from(res.unwrap_or_else(|err| err.to_compile_error()))
}
10 changes: 9 additions & 1 deletion borsh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "borsh"
version.workspace = true
rust-version.workspace = true
authors = ["Near Inc <[email protected]>"]
edition = "2018"
edition.workspace = true
license = "MIT OR Apache-2.0"
readme = "README.md"
categories = ["encoding", "network-programming"]
Expand Down Expand Up @@ -41,7 +41,12 @@ hashbrown = { version = ">=0.11,<0.16.0", optional = true }
bytes = { version = "1", optional = true }
bson = { version = "2", optional = true }

async-generic = { git = "https://github.com/DanikVitek/async-generic.git", rev = "392e8ef432c9bf4ca381262f56e192a27136e04f" }
tokio = { version = "1", default-features = false, features = ["io-util"], optional = true }
async-std = { version = "1", default-features = false, features = ["std"], optional = true }

[dev-dependencies]
tokio-test = "0.4.4"
insta = "1.29.0"
serde_json = { version = "1" }

Expand All @@ -52,6 +57,9 @@ targets = ["x86_64-unknown-linux-gnu"]
[features]
default = ["std"]
derive = ["borsh-derive"]
async = []
DanikVitek marked this conversation as resolved.
Show resolved Hide resolved
tokio = ["async", "dep:tokio"]
DanikVitek marked this conversation as resolved.
Show resolved Hide resolved
async-std = ["async", "dep:async-std"]
DanikVitek marked this conversation as resolved.
Show resolved Hide resolved
unstable__schema = ["derive", "borsh-derive/schema"]
std = []
# Opt into impls for Rc<T> and Arc<T>. Serializing and deserializing these types
Expand Down
6 changes: 5 additions & 1 deletion borsh/examples/serde_json_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ use std::collections::HashMap;
use borsh::{BorshDeserialize, BorshSerialize};

mod serde_json_value {
#[allow(unused_imports)] // used in attribute
pub use de::deserialize_value;
#[allow(unused_imports)] // used in attribute
pub use ser::serialize_value;

mod ser {
use core::convert::TryFrom;

use borsh::{
io::{ErrorKind, Result, Write},
BorshSerialize,
};
use core::convert::TryFrom;

/// this is mutually recursive with `serialize_array` and `serialize_map`
pub fn serialize_value<W: Write>(value: &serde_json::Value, writer: &mut W) -> Result<()> {
Expand Down
Loading
Loading