Skip to content

Commit 9fe34c2

Browse files
committed
Replaced conversion with as_vector function for Vector and HalfVector
1 parent 90696fd commit 9fe34c2

File tree

6 files changed

+13
-22
lines changed

6 files changed

+13
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Added support for libpqxx 8
44
- Changed `HalfVector` to use `std::float16_t` or `_Float16` when available
5+
- Replaced conversion with `as_vector` function for `Vector` and `HalfVector`
56
- Dropped support for libpqxx 7
67
- Dropped support for C++17
78

include/pgvector/halfvec.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ class HalfVector {
4747
}
4848

4949
/// Returns the half vector as a `std::vector<pgvector::Half>`.
50-
operator const std::vector<Half>() const {
51-
return value_;
52-
}
53-
54-
/// Returns the half vector as a `std::span<const pgvector::Half>`.
55-
operator const std::span<const Half>() const {
50+
const std::vector<Half>& as_vector() const {
5651
return value_;
5752
}
5853

include/pgvector/pqxx.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ template <> struct string_traits<pgvector::Vector> {
4545
}
4646

4747
static std::string_view to_buf(std::span<char> buf, const pgvector::Vector& value, ctx c = {}) {
48-
std::span<const float> values{value};
48+
auto& values = value.as_vector();
4949

5050
// important! size_buffer cannot throw an exception on overflow
5151
// so perform this check before writing any data
@@ -69,7 +69,7 @@ template <> struct string_traits<pgvector::Vector> {
6969
}
7070

7171
static size_t size_buffer(const pgvector::Vector& value) noexcept {
72-
std::span<const float> values{value};
72+
auto& values = value.as_vector();
7373

7474
// cannot throw an exception here on overflow
7575
// so throw in into_buf
@@ -109,7 +109,7 @@ template <> struct string_traits<pgvector::HalfVector> {
109109
}
110110

111111
static std::string_view to_buf(std::span<char> buf, const pgvector::HalfVector& value, ctx c = {}) {
112-
std::span<const pgvector::Half> values{value};
112+
auto& values = value.as_vector();
113113

114114
// important! size_buffer cannot throw an exception on overflow
115115
// so perform this check before writing any data
@@ -133,7 +133,7 @@ template <> struct string_traits<pgvector::HalfVector> {
133133
}
134134

135135
static size_t size_buffer(const pgvector::HalfVector& value) noexcept {
136-
std::span<const pgvector::Half> values{value};
136+
auto& values = value.as_vector();
137137

138138
// cannot throw an exception here on overflow
139139
// so throw in into_buf

include/pgvector/vector.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ class Vector {
3131
}
3232

3333
/// Returns the vector as a `std::vector<float>`.
34-
operator const std::vector<float>() const {
35-
return value_;
36-
}
37-
38-
/// Returns the vector as a `std::span<const float>`.
39-
operator const std::span<const float>() const {
34+
const std::vector<float>& as_vector() const {
4035
return value_;
4136
}
4237

test/halfvec_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ static void test_constructor_span() {
1616
assert_equal(vec.dimensions(), 3u);
1717
}
1818

19-
static void test_conversion() {
19+
static void test_as_vector() {
2020
auto vec = HalfVector({1, 2, 3});
21-
auto half_vec = static_cast<std::vector<pgvector::Half>>(vec);
21+
auto& half_vec = vec.as_vector();
2222
assert_equal(half_vec == std::vector<pgvector::Half>{1, 2, 3}, true);
2323
}
2424

2525
void test_halfvec() {
2626
test_constructor_vector();
2727
test_constructor_span();
28-
test_conversion();
28+
test_as_vector();
2929
}

test/vector_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ static void test_constructor_span() {
1616
assert_equal(vec.dimensions(), 3u);
1717
}
1818

19-
static void test_conversion() {
19+
static void test_as_vector() {
2020
auto vec = Vector({1, 2, 3});
21-
auto float_vec = static_cast<std::vector<float>>(vec);
21+
auto& float_vec = vec.as_vector();
2222
assert_equal(float_vec == std::vector<float>{1, 2, 3}, true);
2323
}
2424

2525
void test_vector() {
2626
test_constructor_vector();
2727
test_constructor_span();
28-
test_conversion();
28+
test_as_vector();
2929
}

0 commit comments

Comments
 (0)