27 releases

0.9.3 Feb 4, 2022
0.9.1 Jul 14, 2021
0.9.0 Mar 18, 2021
0.7.1 Aug 24, 2020
0.2.10 Nov 27, 2019

#37 in Encoding

Download history 23433/week @ 2022-03-12 21308/week @ 2022-03-19 21292/week @ 2022-03-26 19663/week @ 2022-04-02 23461/week @ 2022-04-09 22088/week @ 2022-04-16 25166/week @ 2022-04-23 21836/week @ 2022-04-30 19318/week @ 2022-05-07 21803/week @ 2022-05-14 23383/week @ 2022-05-21 21635/week @ 2022-05-28 22183/week @ 2022-06-04 21311/week @ 2022-06-11 22383/week @ 2022-06-18 18763/week @ 2022-06-25

88,107 downloads per month
Used in 617 crates (171 directly)

MIT/Apache

83KB
2K SLoC

Borsh in Rust   Latest Version borsh: rustc 1.40+ License Apache-2.0 badge License MIT badge

borsh-rs is Rust implementation of the Borsh binary serialization format.

Borsh stands for Binary Object Representation Serializer for Hashing. It is meant to be used in security-critical projects as it prioritizes consistency, safety, speed, and comes with a strict specification.

Example

use borsh::{BorshSerialize, BorshDeserialize};

#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
struct A {
    x: u64,
    y: String,
}

#[test]
fn test_simple_struct() {
    let a = A {
        x: 3301,
        y: "liber primus".to_string(),
    };
    let encoded_a = a.try_to_vec().unwrap();
    let decoded_a = A::try_from_slice(&encoded_a).unwrap();
    assert_eq!(a, decoded_a);
}

Features

Opting out from Serde allows borsh to have some features that currently are not available for serde-compatible serializers. Currently we support two features: borsh_init and borsh_skip (the former one not available in Serde).

borsh_init allows to automatically run an initialization function right after deserialization. This adds a lot of convenience for objects that are architectured to be used as strictly immutable. Usage example:

#[derive(BorshSerialize, BorshDeserialize)]
#[borsh_init(init)]
struct Message {
    message: String,
    timestamp: u64,
    public_key: CryptoKey,
    signature: CryptoSignature
    hash: CryptoHash
}

impl Message {
    pub fn init(&mut self) {
        self.hash = CryptoHash::new().write_string(self.message).write_u64(self.timestamp);
        self.signature.verify(self.hash, self.public_key);
    }
}

borsh_skip allows to skip serializing/deserializing fields, assuming they implement Default trait, similary to #[serde(skip)].

#[derive(BorshSerialize, BorshDeserialize)]
struct A {
    x: u64,
    #[borsh_skip]
    y: f32,
}

Releasing

The versions of all public crates in this repository are collectively managed by a single version in the workspace manifest.

So, to publish a new version of all the crates, you can do so by simply bumping that to the next "patch" version and submit a PR.

We have CI Infrastructure put in place to automate the process of publishing all crates once a version change has merged into master.

However, before you release, make sure the CHANGELOG is up to date and that the [Unreleased] section is present but empty.

License

This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). See LICENSE-MIT and LICENSE-APACHE for details.

Dependencies

~1.2–1.8MB
~39K SLoC