Commit 6fa281d7 by Ken Oslund Committed by Copybara-Service

Internal change

PiperOrigin-RevId: 368762888
parent 20c01c01
......@@ -269,6 +269,18 @@ struct type_caster<absl::Span<const T>> {
: vector_converter_(std::move(other.vector_converter_)),
value_(get_vector()) {}
type_caster& operator=(const type_caster<absl::Span<const T>>& other) {
vector_converter_ = other.vector_converter_;
value_ = get_vector();
return *this;
}
type_caster& operator=(type_caster<absl::Span<const T>>&& other) {
vector_converter_ = std::move(other.vector_converter_);
value_ = get_vector();
return *this;
}
static constexpr auto name = _("Span[") + make_caster<T>::name + _("]");
// We do not allow moving because 1) spans are super lightweight, so there's
......
......@@ -43,6 +43,13 @@ bool CheckSpan(absl::Span<const int> span, const std::vector<int>& values) {
return true;
}
bool CheckSpanCasterCopy(const handle& span, const std::vector<int>& values) {
pybind11::detail::make_caster<absl::Span<const int>> caster;
caster = pybind11::detail::load_type<absl::Span<const int>>(span);
return CheckSpan(pybind11::detail::cast_op<absl::Span<const int>>(caster),
values);
}
absl::CivilSecond MakeCivilSecond(double secs) {
return absl::ToCivilSecond(absl::FromUnixSeconds(static_cast<int64_t>(secs)),
absl::UTCTimeZone());
......@@ -246,6 +253,8 @@ PYBIND11_MODULE(absl_example, m) {
// absl::Span bindings.
m.def("check_span", &CheckSpan, arg("span"), arg("values"));
m.def("check_span_caster_copy", &CheckSpanCasterCopy, arg("span"),
arg("values"));
class_<VectorContainer>(m, "VectorContainer")
.def(init())
.def("make_span", &VectorContainer::MakeSpan, arg("values"));
......
......@@ -227,6 +227,7 @@ class AbslSpanTest(parameterized.TestCase):
# Pass values twice- one will be converted to a span, the other to a vector
# (which is known to work), and then they will be compared.
self.assertTrue(absl_example.check_span(values, values))
self.assertTrue(absl_example.check_span_caster_copy(values, values))
def test_span_with_pointers(self):
objs = [absl_example.ObjectForSpan(3), absl_example.ObjectForSpan(5)]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment