Skip to content

Commit

Permalink
Merge branch 'master' into std-interior-mutability
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yfo authored Feb 26, 2024
2 parents 451fe35 + 19e9fee commit 8324057
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .github/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ cargo test
cargo test --features unstable__schema,ascii --test test_ascii_strings
cargo test --features derive
cargo test --features unstable__schema
cargo test --test test_rc --features rc
cargo test --test test_rc --features unstable__schema,rc
cargo test --test test_hash_map --test test_btree_map --features de_strict_order

cargo test --no-default-features
cargo test --no-default-features --features unstable__schema,ascii --test test_ascii_strings
cargo test --no-default-features --features derive
cargo test --no-default-features --features unstable__schema
cargo test --no-default-features --test test_rc --features rc
cargo test --no-default-features --test test_rc --features unstable__schema,rc
cargo test --no-default-features --features hashbrown
popd
pushd borsh-derive
Expand Down
21 changes: 7 additions & 14 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ jobs:
tests:
strategy:
matrix:
rust_version: [1.66.0, stable]
rust_version: [1.67.0, stable]
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Rust toolchain
Expand All @@ -36,21 +36,14 @@ jobs:
run: rustup default ${{ matrix.rust_version }}
- name: print rustc version
run: rustc --version
# remove this step when MSRV >= 1.67.0
- name: downgrade `toml_edit`, time`, `toml_datetime` crate to support older Rust toolchain
if: matrix.rust_version == '1.66.0'
run: |
cargo update -p toml_edit --precise 0.20.2
cargo update -p toml_datetime --precise 0.6.3
cargo update -p time --precise 0.3.23
- name: Run tests
run: ./.github/test.sh

# remove this job when https://github.com/rust-lang/rust/issues/89554 stabilizes
test_exhaustive_checks:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install nightly for exhaustive check tests
uses: dtolnay/rust-toolchain@nightly
# a failure on this check means, that some of `syn` crate's enums have been extended
Expand All @@ -64,15 +57,15 @@ jobs:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Run clippy
run: cargo clippy --features unstable__schema --benches -- -D clippy::all

cargo-fmt:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Run cargo fmt
run: cargo fmt --check

Expand All @@ -83,7 +76,7 @@ jobs:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: run cargo doc
run: RUSTDOCFLAGS="-D warnings" cargo doc --features derive,unstable__schema

Expand All @@ -93,7 +86,7 @@ jobs:
if: github.ref == 'refs/heads/master'
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.CUSTOM_GITHUB_TOKEN }}
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.3.1](https://github.com/near/borsh-rs/compare/borsh-v1.3.0...borsh-v1.3.1) - 2024-01-10

### Other
- fix clippy ([#275](https://github.com/near/borsh-rs/pull/275))
- bump `proc-macro-crate` to `3`; bump MSRV to `1.67` ([#274](https://github.com/near/borsh-rs/pull/274))

## [1.3.0](https://github.com/near/borsh-rs/compare/borsh-v1.2.1...borsh-v1.3.0) - 2023-12-07

### Added
- impl `BorshSchema` for `Rc` and `Arc`; add doc for `rc` feature ([#268](https://github.com/near/borsh-rs/pull/268))

## [1.2.1](https://github.com/near/borsh-rs/compare/borsh-v1.2.0...borsh-v1.2.1) - 2023-12-06

### Other
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ members = ["borsh", "borsh-derive", "fuzz/fuzz-run", "benchmarks"]

[workspace.package]
# shared version of all public crates in the workspace
version = "1.2.1"
rust-version = "1.66.0"
version = "1.3.1"
rust-version = "1.67.0"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Borsh in Rust   [![Latest Version]][crates.io] [![borsh: rustc 1.66+]][Rust 1.66] [![License Apache-2.0 badge]][License Apache-2.0] [![License MIT badge]][License MIT]
# Borsh in Rust   [![Latest Version]][crates.io] [![borsh: rustc 1.67+]][Rust 1.67] [![License Apache-2.0 badge]][License Apache-2.0] [![License MIT badge]][License MIT]

[Borsh]: https://borsh.io
[Latest Version]: https://img.shields.io/crates/v/borsh.svg
[crates.io]: https://crates.io/crates/borsh
[borsh: rustc 1.66+]: https://img.shields.io/badge/rustc-1.66+-lightgray.svg
[Rust 1.66]: https://blog.rust-lang.org/2022/12/15/Rust-1.66.0.html
[borsh: rustc 1.67+]: https://img.shields.io/badge/rustc-1.67+-lightgray.svg
[Rust 1.67]: https://blog.rust-lang.org/2023/01/26/Rust-1.67.0.html
[License Apache-2.0 badge]: https://img.shields.io/badge/license-Apache2.0-blue.svg
[License Apache-2.0]: https://opensource.org/licenses/Apache-2.0
[License MIT badge]: https://img.shields.io/badge/license-MIT-blue.svg
Expand Down
2 changes: 1 addition & 1 deletion borsh-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ proc-macro = true

[dependencies]
syn = { version = "2", features = ["full", "fold"] }
proc-macro-crate = "2"
proc-macro-crate = "3"
proc-macro2 = "1"
quote = "1"
once_cell = "1.18.0"
Expand Down
2 changes: 1 addition & 1 deletion borsh-derive/src/internals/deserialize/structs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn process(input: &ItemStruct, cratename: Path) -> syn::Result<TokenStream2>
}
}
Fields::Unnamed(fields) => {
for (_field_idx, field) in fields.unnamed.iter().enumerate() {
for field in fields.unnamed.iter() {
deserialize::process_field(field, &cratename, &mut body, &mut generics_output)?;
}
quote! {
Expand Down
7 changes: 5 additions & 2 deletions borsh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cfg_aliases = "0.1.0"

[dependencies]
ascii = { version = "1.1", optional = true }
borsh-derive = { path = "../borsh-derive", version = "~1.2.1", optional = true }
borsh-derive = { path = "../borsh-derive", version = "~1.3.1", optional = true }

# hashbrown can be used in no-std context.
# NOTE: There is no reason to restrict use of older versions, but we don't want to get
Expand All @@ -45,13 +45,16 @@ borsh = { path = ".", default_features = false, features = ["bytes", "bson"] }
insta = "1.29.0"

[package.metadata.docs.rs]
features = ["derive", "unstable__schema"]
features = ["derive", "unstable__schema", "rc"]
targets = ["x86_64-unknown-linux-gnu"]

[features]
default = ["std"]
derive = ["borsh-derive"]
unstable__schema = ["derive", "borsh-derive/schema"]
std = []
# Opt into impls for Rc<T> and Arc<T>. Serializing and deserializing these types
# does not preserve identity and may result in multiple copies of the same data.
# Be sure that this is what you want before enabling this feature.
rc = []
de_strict_order = []
49 changes: 33 additions & 16 deletions borsh/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ use crate::__private::maybestd::{
};
use crate::io::{Error, ErrorKind, Read, Result};

#[cfg(feature = "rc")]
use crate::__private::maybestd::{rc::Rc, sync::Arc};
use crate::error::check_zst;

mod hint;
Expand Down Expand Up @@ -883,23 +881,42 @@ impl_range!(RangeFrom, start.., start);
impl_range!(RangeTo, ..end, end);
impl_range!(RangeToInclusive, ..=end, end);

/// Module is available if borsh is built with `features = ["rc"]`.
#[cfg(feature = "rc")]
impl<T: ?Sized> BorshDeserialize for Rc<T>
where
Box<T>: BorshDeserialize,
{
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
Ok(Box::<T>::deserialize_reader(reader)?.into())
pub mod rc {
//!
//! Module defines [BorshDeserialize] implementation for
//! [alloc::rc::Rc](std::rc::Rc) and [alloc::sync::Arc](std::sync::Arc).
use crate::__private::maybestd::{boxed::Box, rc::Rc, sync::Arc};
use crate::io::{Read, Result};
use crate::BorshDeserialize;

/// This impl requires the [`"rc"`] Cargo feature of borsh.
///
/// Deserializing a data structure containing `Rc` will not attempt to
/// deduplicate `Rc` references to the same data. Every deserialized `Rc`
/// will end up with a strong count of 1.
impl<T: ?Sized> BorshDeserialize for Rc<T>
where
Box<T>: BorshDeserialize,
{
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
Ok(Box::<T>::deserialize_reader(reader)?.into())
}
}
}

#[cfg(feature = "rc")]
impl<T: ?Sized> BorshDeserialize for Arc<T>
where
Box<T>: BorshDeserialize,
{
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
Ok(Box::<T>::deserialize_reader(reader)?.into())
/// This impl requires the [`"rc"`] Cargo feature of borsh.
///
/// Deserializing a data structure containing `Arc` will not attempt to
/// deduplicate `Arc` references to the same data. Every deserialized `Arc`
/// will end up with a strong count of 1.
impl<T: ?Sized> BorshDeserialize for Arc<T>
where
Box<T>: BorshDeserialize,
{
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
Ok(Box::<T>::deserialize_reader(reader)?.into())
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions borsh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
Gates implementation of [BorshSerialize] and [BorshDeserialize]
for [`Rc<T>`](std::rc::Rc)/[`Arc<T>`](std::sync::Arc) respectively.
In `no_std` setting `Rc`/`Arc` are pulled from `alloc` crate.
Serializing and deserializing these types
does not preserve identity and may result in multiple copies of the same data.
Be sure that this is what you want before enabling this feature.
* **hashbrown** -
Pulls in [HashMap](std::collections::HashMap)/[HashSet](std::collections::HashSet) when no `std` is available.
This feature is set to be mutually exclusive with **std** feature.
Expand Down
38 changes: 38 additions & 0 deletions borsh/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,44 @@ where
T::declaration()
}
}
/// Module is available if borsh is built with `features = ["rc"]`.
#[cfg(feature = "rc")]
pub mod rc {
//!
//! Module defines [BorshSchema] implementation for
//! [alloc::rc::Rc](std::rc::Rc) and [alloc::sync::Arc](std::sync::Arc).
use crate::BorshSchema;

use super::{Declaration, Definition};
use crate::__private::maybestd::collections::BTreeMap;
use crate::__private::maybestd::{rc::Rc, sync::Arc};

impl<T> BorshSchema for Rc<T>
where
T: BorshSchema + ?Sized,
{
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
T::add_definitions_recursively(definitions);
}

fn declaration() -> Declaration {
T::declaration()
}
}

impl<T> BorshSchema for Arc<T>
where
T: BorshSchema + ?Sized,
{
fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
T::add_definitions_recursively(definitions);
}

fn declaration() -> Declaration {
T::declaration()
}
}
}

macro_rules! impl_for_renamed_primitives {
($($ty: ty : $name: ident => $size: expr);+) => {
Expand Down
4 changes: 1 addition & 3 deletions borsh/src/schema/container_ext/max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ fn max_serialized_size_impl<'a>(
Ok(Definition::Primitive(size)) => match size {
0 => Ok(0),
size => {
let count_sizes = usize::try_from(*size)
.ok()
.and_then(|size| size.checked_mul(count.get()));
let count_sizes = usize::from(*size).checked_mul(count.get());
count_sizes.ok_or(Error::Overflow)
}
},
Expand Down
40 changes: 29 additions & 11 deletions borsh/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ use crate::__private::maybestd::{
use crate::error::check_zst;
use crate::io::{Error, ErrorKind, Result, Write};

#[cfg(feature = "rc")]
use crate::__private::maybestd::{rc::Rc, sync::Arc};

pub(crate) mod helpers;

const FLOAT_NAN_ERR: &str = "For portability reasons we do not allow to serialize NaNs.";
Expand Down Expand Up @@ -600,17 +597,38 @@ impl_range!(RangeFrom, this, &this.start);
impl_range!(RangeTo, this, &this.end);
impl_range!(RangeToInclusive, this, &this.end);

/// Module is available if borsh is built with `features = ["rc"]`.
#[cfg(feature = "rc")]
impl<T: BorshSerialize + ?Sized> BorshSerialize for Rc<T> {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
(**self).serialize(writer)
pub mod rc {
//!
//! Module defines [BorshSerialize] implementation for
//! [alloc::rc::Rc](std::rc::Rc) and [alloc::sync::Arc](std::sync::Arc).
use crate::__private::maybestd::{rc::Rc, sync::Arc};
use crate::io::{Result, Write};
use crate::BorshSerialize;

/// This impl requires the [`"rc"`] Cargo feature of borsh.
///
/// Serializing a data structure containing `Rc` will serialize a copy of
/// the contents of the `Rc` each time the `Rc` is referenced within the
/// data structure. Serialization will not attempt to deduplicate these
/// repeated data.
impl<T: BorshSerialize + ?Sized> BorshSerialize for Rc<T> {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
(**self).serialize(writer)
}
}
}

#[cfg(feature = "rc")]
impl<T: BorshSerialize + ?Sized> BorshSerialize for Arc<T> {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
(**self).serialize(writer)
/// This impl requires the [`"rc"`] Cargo feature of borsh.
///
/// Serializing a data structure containing `Arc` will serialize a copy of
/// the contents of the `Arc` each time the `Arc` is referenced within the
/// data structure. Serialization will not attempt to deduplicate these
/// repeated data.
impl<T: BorshSerialize + ?Sized> BorshSerialize for Arc<T> {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
(**self).serialize(writer)
}
}
}

Expand Down
Loading

0 comments on commit 8324057

Please sign in to comment.