Go to file
Armani Ferrante 2351c05a10
Rename packages for crates.io and bump dependency
2021-01-31 04:51:34 -08:00
benchmarks Rename packages for crates.io and bump dependency 2021-01-31 04:51:34 -08:00
borsh Rename packages for crates.io and bump dependency 2021-01-31 04:51:34 -08:00
borsh-derive release: v0.8.1 2021-01-13 12:50:39 +02:00
borsh-derive-internal release: v0.8.1 2021-01-13 12:50:39 +02:00
borsh-schema-derive-internal release: v0.8.1 2021-01-13 12:50:39 +02:00
fuzz/fuzz-run refactor: Reorganized the project after decoupling from the borsh repo (#1) 2021-01-07 10:23:51 -08:00
.gitattributes Improve performance of String and Vec<u8> (#58) 2020-03-13 10:13:51 -07:00
.gitignore js related files and configs deleted 2020-12-01 14:02:14 +02:00
.travis.yml refactor: Enabled check code formatting on CI (#4) 2021-01-12 09:27:42 -08:00
CHANGELOG.md release: v0.8.1 2021-01-13 12:50:39 +02:00
CODEOWNERS Update CODEOWNERS 2020-07-20 18:19:07 -07:00
Cargo.toml refactor: Reorganized the project after decoupling from the borsh repo (#1) 2021-01-07 10:23:51 -08:00
LICENSE-APACHE standardize to be dual license MIT and Apache (#63) 2020-05-19 08:59:06 -07:00
LICENSE-MIT refactor: Reorganized the project after decoupling from the borsh repo (#1) 2021-01-07 10:23:51 -08:00
README.md fix(docs): Updated the release instructions 2021-01-11 22:54:15 +02:00
publish.sh refactor: Reorganized the project after decoupling from the borsh repo (#1) 2021-01-07 10:23:51 -08:00

README.md

Borsh in Rust Build Status 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

After you merged your change into the master branch and bumped the versions of all three crates it is time to officially release the new version.

Make sure borsh, borsh-derive, borsh-derive-internal, and borsh-schema-derive-internal all have the new crate versions. Then run the publish.sh script:

sh publish.sh

Make sure you are on the master branch, then tag the code and push the tag:

git tag -a v9.9.9 -m "My superawesome change."
git push origin v9.9.9

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.