Coder Social home page Coder Social logo

real-felix / borsh-rs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from near/borsh-rs

0.0 2.0 0.0 68.58 MB

Rust implementation of Binary Object Representation Serializer for Hashing

Home Page: https://borsh.io/

License: Apache License 2.0

Rust 100.00%

borsh-rs's Introduction

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

Before you release, make sure CHANGELOG.md is up to date.

Use cargo-workspaces to save time.

Bump Versions

cargo workspaces version --force 'borsh*' --exact --no-individual-tags patch

This will bump all the versions to the next "patch" release (see cargo workspaces version --help for more options), create a new commit, push v0.x.x tag, push to the master.

Publish

To publish on crates.io the version that is currently in git:

cargo workspaces publish --from-git --skip-published

Alternatively, you may want to combine the version bumping with publishing:

cargo workspaces publish --force 'borsh*' --exact --no-individual-tags patch

Release on GitHub

  1. Navigate to the New Release page
  2. Enter the tag name, e.g. v0.8.0
  3. Write down the release log (basically, copy-paste from the CHANGELOG)
  4. Publish the release

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.

borsh-rs's People

Contributors

ailisp avatar alecmocatta avatar austinabell avatar bowenwang1996 avatar dtolnay avatar evgenykuzyakov avatar frol avatar hrls avatar ilblackdragon avatar jstarry avatar k06a avatar lexfrl avatar magicrb avatar maksymzavershynskyi avatar matklad avatar mcf-rocks avatar mikedotexe avatar mikhailok avatar nhynes avatar pmnoxx avatar snjax avatar vgrichina avatar volovyks avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.