关于molecule的实现和使用的几个问题

我有几个关于molecule的几个问题:

  • molecule的序列化和反序列大量使用了bytes::Bytes,但是实际上序列化和反序列化和SyncSend是没有关系的。而bytes::Bytes是封装了原子指针的,这是否增加了开销。直接使用[u8]或者Vec<u8>是否更好?
  • 开发使用方面,现在是通过使用moleculec工具[GitHub - nervosnetwork/molecule: Another serialization system: minimalist and canonicalization.]生成molecule相关代码。为什么不用serde-rs一样使用宏?或者干脆引入serde-rs框架呢?
  1. Less memory used without any lifetime.

    The APIs with lifetime are hard to use.

    In fact, molecule uses Vec<u8> instead of Bytes with the no_std feature.

  2. Generated code should be able to locked in the version control system.

    The generated code with serde proc-macros could be changed when upgrading serde-rs, but no one can review the code.
    As a blockchain side-project, we really care about the generated code for data structures.

    p.s. Fewer dependencies are better for code audit.

    If you want to generate code dynamically, the molecule can generate code in build.rs.

1.Oh,the crate doc should have some questions.The bytes mod in https://docs.rs/molecule/0.7.1/molecule/ links to https://docs.rs/bytes/1.0.1/bytes/index.html.

2.If serde-rs is not good enough,why not use derive macro directly,which is more user friendly, instead? In fact,I really don’t like to install a tool to generate code. And, molecule crate seems not be able to generate code for enum.

p.s. serde-rs is a serderialize and deserialize frame,which just provide serderialize attributes.It may not change serialize logic.

First, molecule is a blockchain side-project.

So, we have to consider requirements for blockchain first.

  • why not use derive macro directly

    • Personally, I prefer to proc-macros.

    • We want to see the generated code by eyes, proc-macros hide the codes.

    • The schema files are for all languages, not only for rust.

      Therefore, we don’t want to:

      • Put the schemas in rust source code.

      • Or make two copies: one in schemas files, another in rust source code.

      If you were Java developers, do you want to read Rust code for the schemas?
      Then you have to learn serde-rs.

  • In fact,I really don’t like to install a tool to generate code.

    Try putting them in build.rs. Here is an example.

  • molecule crate seems not be able to generate code for enum.

    If you mean a type alias for numbers, yes. But we have union.

  • serde-rs is a serderialize and deserialize frame,which just provide serderialize attributes.It may not change serialize logic.

    How could you guarantee? Have you read the whole code of it? No one never writes bugs.

    I like serde-rs but it’s too complicated. We can’t afford the risk.

p.s. If you don’t like last reason, it’s OK, but I think the first reason is enough.

1 Like

Thanks for your reply.It’s enough.