Code Monkey home page Code Monkey logo

Comments (6)

haixuanTao avatar haixuanTao commented on June 11, 2024

I'm not an expert of C++, but maybe try adding .data() at the end:

std::memcpy(PointCloud2.data.data(), output_data, output_data_len);

From memory alone, I think that PointCloud2.data is a standard vector in C++

https://chat.openai.com/c/c42af36d-9385-4db5-a168-c1337d570335

p.s: note that you can use ```c++ for syntax highlighting

from dora.

phil-opp avatar phil-opp commented on June 11, 2024

See https://cxx.rs/binding/vec.html for the type definition of rust::Vec<T>. While it's similar to a std::vector, it's a different type without any direction conversion. Unfortunately, I don't see any more efficient way to push multiple values either.

std::memcpy(PointCloud2.data.data(), output_data, output_data_len);

This is unsafe and will lead to undefined behavior if output_data_len is greater than PointCloud2.data.size().


A possible solution could be to generate two versions of each message type: one based on Rust types and one based on C++ types. The version based on C++ types would then contain normal std::vector fields instead of rust::Vec. Subscribe methods would only support the version based on Rust-types, but publish methods could support both types. The drawback is that it duplicates all message types, which could be confusing. What do you think about this approach @ShoreFlower?

from dora.

ShoreFlower avatar ShoreFlower commented on June 11, 2024

from dora.

phil-opp avatar phil-opp commented on June 11, 2024

To avoid the boilerplate of the for loop, you should be able to use back_inserter together with std::copy.

Doing a pointer copy would require a public set_len method, which is being discussed upstream. However, apparently such a function isn't even available for the standard C++ vector type.

If you're concerned about efficiency, you should try doing a benchmark with link-time optimization enabled. I can imagine that the compiler/linker optimizes the for loop and push_back calls to some quite efficient code.

from dora.

phil-opp avatar phil-opp commented on June 11, 2024

To make the push_back operation cheap you can call reserve with the required capacity beforehand. Then no reallocation is required anymore.

from dora.

ShoreFlower avatar ShoreFlower commented on June 11, 2024

Thank you for your answer, for having used std::copy can solve the data problem.
std::copy(output_data + 16, output_data + output_data_len, std::back_inserter(PointCloud2.data));

from dora.

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.