Allow hidl_vec = initializer list

Currently, a hidl vector can be constructed using an initializer list, but
it cannot be made equal to an initializer list.
Add the missing operator here to allow for simpler usage.

Test: atest libhidl_test
Bug: 117935272
Change-Id: Id21e9cafee754968a7300a3d7bc85481d32dcc3a
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 43e84c0..55f21ea 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -345,19 +345,7 @@
         *this = std::move(other);
     }
 
-    hidl_vec(const std::initializer_list<T> list)
-            : mOwnsBuffer(true) {
-        if (list.size() > UINT32_MAX) {
-            details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
-        }
-        mSize = static_cast<uint32_t>(list.size());
-        mBuffer = new T[mSize];
-
-        size_t idx = 0;
-        for (auto it = list.begin(); it != list.end(); ++it) {
-            mBuffer[idx++] = *it;
-        }
-    }
+    hidl_vec(const std::initializer_list<T> list) : hidl_vec() { *this = list; }
 
     hidl_vec(const std::vector<T> &other) : hidl_vec() {
         *this = other;
@@ -453,6 +441,24 @@
         return *this;
     }
 
+    hidl_vec& operator=(const std::initializer_list<T> list) {
+        if (list.size() > UINT32_MAX) {
+            details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements.");
+        }
+        if (mOwnsBuffer) {
+            delete[] mBuffer;
+        }
+        mSize = static_cast<uint32_t>(list.size());
+        mBuffer = new T[mSize];
+        mOwnsBuffer = true;
+
+        size_t idx = 0;
+        for (auto it = list.begin(); it != list.end(); ++it) {
+            mBuffer[idx++] = *it;
+        }
+        return *this;
+    }
+
     // cast to an std::vector.
     operator std::vector<T>() const {
         std::vector<T> v(mSize);
diff --git a/test_main.cpp b/test_main.cpp
index beb89d7..2b9f52c 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -280,6 +280,21 @@
     EXPECT_TRUE(hv1 != hv3);
 }
 
+TEST_F(LibHidlTest, VecEqInitializerTest) {
+    std::vector<int32_t> reference{5, 6, 7};
+    android::hardware::hidl_vec<int32_t> hv1{1, 2, 3};
+    hv1 = {5, 6, 7};
+    android::hardware::hidl_vec<int32_t> hv2;
+    hv2 = {5, 6, 7};
+    android::hardware::hidl_vec<int32_t> hv3;
+    hv3 = {5, 6, 8};
+
+    // use the == and != operator intentionally here
+    EXPECT_TRUE(hv1 == hv2);
+    EXPECT_TRUE(hv1 == reference);
+    EXPECT_TRUE(hv1 != hv3);
+}
+
 TEST_F(LibHidlTest, VecRangeCtorTest) {
     struct ConvertibleType {
         int val;