mirror of
https://github.com/esphome/esphome.git
synced 2026-10-21 13:34:12 +00:00
[packet_interface] Remove PacketBuffer::to_vector()
It allocated a vector for each call, which invites heap churn. Only the tests used it; they now have a local helper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
574e9f4687
commit
726c30dccd
@@ -18,8 +18,8 @@ namespace esphome::packet_interface {
|
||||
*
|
||||
* WARNING: PacketBuffer holds raw pointers to data. The user must ensure that the data remains valid
|
||||
* for the lifetime of the PacketBuffer instance. Storing PacketBuffer instances beyond the scope of the data they
|
||||
* reference will lead to dangling pointers and undefined behavior, so if you need to store a PacketBuffer, consider
|
||||
* copying the data into a std::vector or similar.
|
||||
* reference will lead to dangling pointers and undefined behavior, so to keep a packet, copy it with copy_to() into a
|
||||
* buffer allocated in setup().
|
||||
*/
|
||||
class PacketBuffer {
|
||||
public:
|
||||
@@ -162,17 +162,6 @@ class PacketBuffer {
|
||||
return copied;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the buffer to a std::vector.
|
||||
*/
|
||||
std::vector<uint8_t> to_vector() const {
|
||||
std::vector<uint8_t> result;
|
||||
size_t total_size = this->size();
|
||||
result.resize(total_size);
|
||||
this->copy_to(result.data(), total_size);
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
const uint8_t *pre_buffer_{nullptr};
|
||||
size_t pre_length_{0};
|
||||
|
||||
@@ -5,6 +5,13 @@
|
||||
|
||||
namespace esphome::packet_interface::testing {
|
||||
|
||||
// Copies a packet into a vector, for comparing in tests
|
||||
static std::vector<uint8_t> to_vector(const esphome::packet_interface::PacketBuffer &data) {
|
||||
std::vector<uint8_t> result(data.size());
|
||||
data.copy_to(result.data(), result.size());
|
||||
return result;
|
||||
}
|
||||
|
||||
// Test default constructor
|
||||
TEST(PacketBufferTest, DefaultConstructor) {
|
||||
PacketBuffer buffer;
|
||||
@@ -154,19 +161,6 @@ TEST(PacketBufferTest, CopyToComposite) {
|
||||
EXPECT_EQ(dest[5], 0x06);
|
||||
}
|
||||
|
||||
// Test to_vector method
|
||||
TEST(PacketBufferTest, ToVector) {
|
||||
uint8_t data[] = {0xAA, 0xBB, 0xCC};
|
||||
PacketBuffer buffer(data, 3);
|
||||
|
||||
std::vector<uint8_t> vec = buffer.to_vector();
|
||||
|
||||
EXPECT_EQ(vec.size(), 3);
|
||||
EXPECT_EQ(vec[0], 0xAA);
|
||||
EXPECT_EQ(vec[1], 0xBB);
|
||||
EXPECT_EQ(vec[2], 0xCC);
|
||||
}
|
||||
|
||||
// Test copying from an offset past the mid buffer
|
||||
TEST(PacketBufferTest, CopyToOffsetPastMid) {
|
||||
uint8_t pre[] = {0x01, 0x02};
|
||||
@@ -221,7 +215,7 @@ TEST(PacketBufferTest, EmptyBuffer) {
|
||||
EXPECT_EQ(copied, 0);
|
||||
EXPECT_EQ(dest[0], 0xFF); // Unchanged
|
||||
|
||||
std::vector<uint8_t> vec = buffer.to_vector();
|
||||
std::vector<uint8_t> vec = to_vector(buffer);
|
||||
EXPECT_EQ(vec.size(), 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,13 @@
|
||||
|
||||
namespace esphome::uart::testing {
|
||||
|
||||
// Copies a packet into a vector, for comparing in tests
|
||||
static std::vector<uint8_t> to_vector(const esphome::packet_interface::PacketBuffer &data) {
|
||||
std::vector<uint8_t> result(data.size());
|
||||
data.copy_to(result.data(), result.size());
|
||||
return result;
|
||||
}
|
||||
|
||||
using packet_interface::PacketBuffer;
|
||||
using packet_interface::PacketMetaData;
|
||||
|
||||
@@ -26,9 +33,8 @@ class UartPacketInterfaceTest : public ::testing::Test {
|
||||
interface_.set_uart_parent(&uart_);
|
||||
interface_.setup();
|
||||
|
||||
interface_.add_packet_interface_listener([this](const PacketBuffer &data, const PacketMetaData &meta) {
|
||||
received_packets_.push_back(data.to_vector());
|
||||
});
|
||||
interface_.add_packet_interface_listener(
|
||||
[this](const PacketBuffer &data, const PacketMetaData &meta) { received_packets_.push_back(to_vector(data)); });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user