Giter VIP home page Giter VIP logo

Comments (3)

isabelatkinson avatar isabelatkinson commented on September 4, 2024 1

Filed RUST-1747. Going to close this out but feel free to open another issue if you run into anything else!

from bson-rust.

isabelatkinson avatar isabelatkinson commented on September 4, 2024

Hey Dan, I'd recommend using the TryFrom<RawDocument> implementation for Document to convert between the two:

let raw_doc = RawDocument::from_bytes(b"raw document contents")?;
let update = doc! {
    "$set": Document::try_from(raw_doc)?,
};

This will allow you to avoid the additional allocation of a RawDocumentBuf.

it would be nice to have a usable RawDocument without extra/intermediate allocation

Because RawDocument and Document have different internal representations, it's not possible to avoid allocation if you want to nest a RawDocument inside of a Document. If you need to end up with a Document, it might be more efficient to construct that first doc as a regular non-raw document (if that works for your use case) to avoid conversion between types.

I think there's also some room for improvement in the driver API to accept raw documents in certain places we currently accept regular documents (like update documents, which I believe is your use case here) so that you don't need to allocate a Document in the first place. We'd have to wait until the next major version to change our API like this, but I will discuss this idea with the team as a consideration for 3.0!

from bson-rust.

TheDan64 avatar TheDan64 commented on September 4, 2024

If you need to end up with a Document, it might be more efficient to construct that first doc as a regular non-raw document (if that works for your use case) to avoid conversion between types.

Yeah, that makes sense but doesn't work for my use case since raw document bytes are being sent and then deserialized to RawDocument to avoid overhead (since we know it should always be a valid document).

Appreciate the tip, didn't know TryFrom was implemented! & thanks for the consideration for v3! Being able to accept a raw document for updates would be useful!

from bson-rust.

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.