GithubHelp home page GithubHelp logo

Comments (7)

erickt avatar erickt commented on September 14, 2024

Some serialization formats, like cbor, have a more efficient encoding for an array of u8s compared to an array of arbitrary types. Formats like bincode go even further and don’t even encode a tag describing what the next item is, so the deserializer doesn’t know the type of the next type. Other formats, like json, do not.

to handle this, serde Deserialize impls need to hint to the Deserializer what we expect the next type to be. The Deserializer could then use this to decide to parse the next chunk in a special way for an untagged format like bincode, or fall back to the default. serde_bytes is just a convenient way to provide this hint.

as to why serde doesn’t do this automatically, we can’t because rust doesn’t support specialization yet, so we need to default to the generic array deserialization, and provide serde_bytes to override the default.

from bytes.

Xiretza avatar Xiretza commented on September 14, 2024

I get that that's the idea, but I can't find the point in the serde_bytes code where this actually happens for deserializing a &[u8]. It seems to just forward straight to the default serde implementation.

from bytes.

Xiretza avatar Xiretza commented on September 14, 2024

I have a feeling this might just be a simple bug, I've opened #31.

from bytes.

erickt avatar erickt commented on September 14, 2024

Actually, my previous comment was wrong, and I don't think we need #31. I think what might be happening here is some confusion over reusing the same name (especially since it confused me). First, there actually is an impl in serde explicitly for &'a [u8]. I thought we implemented that for a generic &[T] but I was wrong. Second, the impl in serde_bytes::de for &'a [u8] is implementing serde_bytes::Deserialize, not serde::Deserialize. This is just wiring up the generic impls so it can be used with serde_bytes::deserialize, which takes a serde_bytes::Deserialize, not a serde::Deserialize.

from bytes.

dtolnay avatar dtolnay commented on September 14, 2024

#[serde(with = "serde_bytes")] on &[u8] isn't for deserialization, it is for serialization. On other types it optimizes both serialization and deserialization. The deserialize-related code in this crate related to &[u8] is just to make that attribute work without getting a compile error if you also derive Deserialize.

from bytes.

Xiretza avatar Xiretza commented on September 14, 2024

Ah, I see! I thought the visit_borrowed_{bytes,str}() methods on BytesVisitor were the optimization, but if deserialization of Bytes is actually the same as that of &[u8] in plain serde, I'll just document that.

from bytes.

Xiretza avatar Xiretza commented on September 14, 2024

Does that also mean that the entire BytesVisitor is redundant and that impl serde::Deserialize for Bytes could just forward to serde::Deserialize for &[u8]?

from bytes.

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.