Git Product home page Git Product logo

Comments (2)

dj8yfo avatar dj8yfo commented on July 18, 2024

@cameron1024 probably, you could achieve optionality by expressing it with type:

struct FooV3 {
  data: Vec<u8>,
  something_else: Option<i32>,
}

This juggling of encoded bytes of one type and decoding them as another type looks weird at first glance.
I've checked #serde[(default=... doc , it's not obvious how alleged #[borsh(default= ... attribute must behave, given that borsh format doesn't encode field names and current field being decoded is assumed from context (position in stream). Thus atm it's either a field is always present in output, or always skipped. As already mentioned, optionality can be expressed with Option<T> without using any additional encoding attributes.

from borsh-rs.

frol avatar frol commented on July 18, 2024

As already mentioned, optionality can be expressed with Option without using any additional encoding attributes.

@dj8yfo Well, None still requires an explicit 0 byte in the input, so FooV1 { data } is not byte-equal to FooV2 { data, something_else: None }.

@cameron1024 Since Borsh does not have schema inside, it can become ambiguous if you randomly try to have default field value and expect to skip it. I recommend you manually implement BorshDeserialize for FooV2 that will handle it manually.

from borsh-rs.

Related Issues (20)

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.