From a970c4d6817f97c303d6617898037e03633909e2 Mon Sep 17 00:00:00 2001 From: Jeff Ithier Date: Thu, 31 Oct 2024 15:07:29 +0100 Subject: [PATCH] [#490] Translate from Slice in CXX to [u8] in Rust --- iceoryx2-ffi/cxx/include/iox/slice.hpp | 25 ++-- .../include/iox2/port_factory_publisher.hpp | 22 +++- iceoryx2-ffi/cxx/include/iox2/publisher.hpp | 31 +++-- iceoryx2-ffi/cxx/include/iox2/sample.hpp | 13 +- iceoryx2-ffi/cxx/include/iox2/sample_mut.hpp | 28 ++-- .../service_builder_publish_subscribe.hpp | 41 +++--- .../src/service_publish_subscribe_tests.cpp | 123 ++++++++++-------- iceoryx2-ffi/ffi/src/api/publisher.rs | 24 +--- iceoryx2-ffi/ffi/src/api/sample.rs | 9 +- 9 files changed, 178 insertions(+), 138 deletions(-) diff --git a/iceoryx2-ffi/cxx/include/iox/slice.hpp b/iceoryx2-ffi/cxx/include/iox/slice.hpp index 9dd1386f..18a7981a 100644 --- a/iceoryx2-ffi/cxx/include/iox/slice.hpp +++ b/iceoryx2-ffi/cxx/include/iox/slice.hpp @@ -26,22 +26,21 @@ class Slice { using ConstIterator = const T*; using ValueType = T; - template - Slice(U* data, uint64_t len) - : m_data { const_cast(static_cast*>(data)) } - , m_len { len } { + Slice(const void* data, uint64_t number_of_elements) + : m_data { static_cast(const_cast(data)) } + , m_number_of_elements { number_of_elements } { } - auto len() const -> uint64_t { - return m_len; + auto number_of_elements() const -> uint64_t { + return m_number_of_elements; } auto operator[](const uint64_t n) const -> const T& { - IOX_ASSERT(n < m_len, "Index out of bounds"); + IOX_ASSERT(n < m_number_of_elements, "Index out of bounds"); return *(m_data + n); } auto operator[](const uint64_t n) -> T& { - IOX_ASSERT(n < m_len, "Index out of bounds"); + IOX_ASSERT(n < m_number_of_elements, "Index out of bounds"); return *(m_data + n); } @@ -55,17 +54,17 @@ class Slice { auto end() -> Iterator { if constexpr (std::is_same_v) { - return reinterpret_cast(reinterpret_cast(m_data) + m_len); + return reinterpret_cast(reinterpret_cast(m_data) + m_number_of_elements); } else { - return m_data + m_len; + return m_data + m_number_of_elements; } } auto end() const -> ConstIterator { if constexpr (std::is_same_v) { - return reinterpret_cast(reinterpret_cast(m_data) + m_len); + return reinterpret_cast(reinterpret_cast(m_data) + m_number_of_elements); } else { - return m_data + m_len; + return m_data + m_number_of_elements; } } @@ -79,7 +78,7 @@ class Slice { private: T* m_data; - uint64_t m_len; + uint64_t m_number_of_elements; }; template diff --git a/iceoryx2-ffi/cxx/include/iox2/port_factory_publisher.hpp b/iceoryx2-ffi/cxx/include/iox2/port_factory_publisher.hpp index bb8d36b0..164f5a7e 100644 --- a/iceoryx2-ffi/cxx/include/iox2/port_factory_publisher.hpp +++ b/iceoryx2-ffi/cxx/include/iox2/port_factory_publisher.hpp @@ -71,8 +71,26 @@ PortFactoryPublisher::create() && -> iox::expected(iox::into(value))); }); - m_max_slice_len.and_then( - [&](auto value) { iox2_port_factory_publisher_builder_set_max_slice_len(&m_handle, value); }); + m_max_slice_len + .and_then([&](auto value) { + // The payload type used by the C API is always a [u8]. + // Thus need to convert from N to N * sizeof(payload). + // TODO: Consider alignment... not aligning each element properly will impact performance + if constexpr (iox::IsSlice::VALUE) { + iox2_port_factory_publisher_builder_set_max_slice_len(&m_handle, + value * sizeof(typename Payload::ValueType)); + } else { + iox2_port_factory_publisher_builder_set_max_slice_len(&m_handle, value * sizeof(Payload)); + } + }) + .or_else([&]() { + // Assume only one element if not otherwise specified + if constexpr (iox::IsSlice::VALUE) { + iox2_port_factory_publisher_builder_set_max_slice_len(&m_handle, sizeof(typename Payload::ValueType)); + } else { + iox2_port_factory_publisher_builder_set_max_slice_len(&m_handle, sizeof(Payload)); + } + }); m_max_loaned_samples.and_then( [&](auto value) { iox2_port_factory_publisher_builder_set_max_loaned_samples(&m_handle, value); }); diff --git a/iceoryx2-ffi/cxx/include/iox2/publisher.hpp b/iceoryx2-ffi/cxx/include/iox2/publisher.hpp index ccc2e8ee..f7f479a8 100644 --- a/iceoryx2-ffi/cxx/include/iox2/publisher.hpp +++ b/iceoryx2-ffi/cxx/include/iox2/publisher.hpp @@ -147,7 +147,15 @@ inline auto Publisher::unable_to_deliver_strategy() cons template inline auto Publisher::max_slice_len() const -> uint64_t { - return iox2_publisher_max_slice_len(&m_handle); + // NOTE: The C API always uses a [u8] payload, therefore the max length returned is the number of bytes. + // Dividing by the size gives the number of slice elements available to the C++ API. + // + // NOTE: For non-slice types, the number of slice elements is always 1. + if constexpr (iox::IsSlice::VALUE) { + return iox2_publisher_max_slice_len(&m_handle) / sizeof(typename Payload::ValueType); + } else { + return iox2_publisher_max_slice_len(&m_handle) / sizeof(Payload); + } } template @@ -180,7 +188,7 @@ inline auto Publisher::loan_uninit() -> iox::expected, PublisherLoanError> { SampleMutUninit sample; - auto result = iox2_publisher_loan(&m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle); + auto result = iox2_publisher_loan_slice_uninit(&m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, 1); if (result == IOX2_OK) { return iox::ok(std::move(sample)); @@ -208,19 +216,18 @@ template template inline auto Publisher::loan_slice(const uint64_t number_of_elements) -> iox::expected, PublisherLoanError> { - SampleMutUninit sample; + auto sample_uninit = loan_slice_uninit(number_of_elements); - auto result = iox2_publisher_loan_slice_uninit( - number_of_elements, &m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle); + if (sample_uninit.has_error()) { + return iox::err(sample_uninit.error()); + } + auto sample_init = std::move(sample_uninit.value()); - if (result == IOX2_OK) { - for (auto it = sample.payload_slice().begin(); it != sample.payload_slice().end(); ++it) { - new (it) typename T::ValueType(); - } - return iox::ok(assume_init(std::move(sample))); + for (auto it = sample_init.payload_slice().begin(); it != sample_init.payload_slice().end(); ++it) { + new (it) typename T::ValueType(); } - return iox::err(iox::into(result)); + return iox::ok(assume_init(std::move(sample_init))); } template @@ -230,7 +237,7 @@ inline auto Publisher::loan_slice_uninit(const uint64_t SampleMutUninit sample; auto result = iox2_publisher_loan_slice_uninit( - number_of_elements, &m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle); + &m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, number_of_elements); if (result == IOX2_OK) { return iox::ok(std::move(sample)); diff --git a/iceoryx2-ffi/cxx/include/iox2/sample.hpp b/iceoryx2-ffi/cxx/include/iox2/sample.hpp index 790ccf0a..8b55808b 100644 --- a/iceoryx2-ffi/cxx/include/iox2/sample.hpp +++ b/iceoryx2-ffi/cxx/include/iox2/sample.hpp @@ -130,23 +130,24 @@ template template inline auto Sample::payload() const -> const Payload& { const void* ptr = nullptr; - size_t number_of_elements = 0; + size_t number_of_bytes = 0; - iox2_sample_payload(&m_handle, &ptr, &number_of_elements); + iox2_sample_payload(&m_handle, &ptr, &number_of_bytes); return *static_cast(ptr); } - template template inline auto Sample::payload_slice() const -> Payload { const void* ptr = nullptr; - size_t number_of_elements = 0; + size_t number_of_bytes = 0; + + iox2_sample_payload(&m_handle, &ptr, &number_of_bytes); - iox2_sample_payload(&m_handle, &ptr, &number_of_elements); + size_t number_of_elements = number_of_bytes / sizeof(typename Payload::ValueType); - return Payload(static_cast(ptr), number_of_elements); + return Payload(ptr, number_of_elements); } template diff --git a/iceoryx2-ffi/cxx/include/iox2/sample_mut.hpp b/iceoryx2-ffi/cxx/include/iox2/sample_mut.hpp index b07871cd..bbb9228e 100644 --- a/iceoryx2-ffi/cxx/include/iox2/sample_mut.hpp +++ b/iceoryx2-ffi/cxx/include/iox2/sample_mut.hpp @@ -197,10 +197,10 @@ template template inline auto SampleMut::payload() const -> const Payload& { const void* ptr = nullptr; - size_t number_of_elements = 0; + size_t number_of_bytes = 0; - iox2_sample_mut_payload(&m_handle, &ptr, &number_of_elements); - IOX_ASSERT(sizeof(Payload) <= number_of_elements, ""); + iox2_sample_mut_payload(&m_handle, &ptr, &number_of_bytes); + IOX_ASSERT(sizeof(Payload) <= number_of_bytes, ""); return *static_cast(ptr); } @@ -209,10 +209,10 @@ template template inline auto SampleMut::payload_mut() -> Payload& { void* ptr = nullptr; - size_t number_of_elements = 0; + size_t number_of_bytes = 0; - iox2_sample_mut_payload_mut(&m_handle, &ptr, &number_of_elements); - IOX_ASSERT(sizeof(Payload) <= number_of_elements, ""); + iox2_sample_mut_payload_mut(&m_handle, &ptr, &number_of_bytes); + IOX_ASSERT(sizeof(Payload) <= number_of_bytes, ""); return *static_cast(ptr); } @@ -221,22 +221,26 @@ template template inline auto SampleMut::payload_slice() const -> const Payload { const void* ptr = nullptr; - size_t number_of_elements = 0; + size_t number_of_bytes = 0; - iox2_sample_mut_payload(&m_handle, &ptr, &number_of_elements); + iox2_sample_mut_payload(&m_handle, &ptr, &number_of_bytes); - return Payload(static_cast(ptr), number_of_elements); + size_t number_of_elements = number_of_bytes / sizeof(typename Payload::ValueType); + + return Payload(ptr, number_of_elements); } template template inline auto SampleMut::payload_slice_mut() -> Payload { void* ptr = nullptr; - size_t number_of_elements = 0; + size_t number_of_bytes = 0; + + iox2_sample_mut_payload_mut(&m_handle, &ptr, &number_of_bytes); - iox2_sample_mut_payload_mut(&m_handle, &ptr, &number_of_elements); + size_t number_of_elements = number_of_bytes / sizeof(typename Payload::ValueType); - return Payload(static_cast(ptr), number_of_elements); + return Payload(ptr, number_of_elements); } template diff --git a/iceoryx2-ffi/cxx/include/iox2/service_builder_publish_subscribe.hpp b/iceoryx2-ffi/cxx/include/iox2/service_builder_publish_subscribe.hpp index 0e3eec7a..4cf9a70b 100644 --- a/iceoryx2-ffi/cxx/include/iox2/service_builder_publish_subscribe.hpp +++ b/iceoryx2-ffi/cxx/include/iox2/service_builder_publish_subscribe.hpp @@ -26,6 +26,17 @@ #include namespace iox2 { + +template +struct PayloadValueType { + using TYPE = T; +}; + +template +struct PayloadValueType> { + using TYPE = typename iox::Slice::ValueType; +}; + /// Builder to create new [`MessagingPattern::PublishSubscribe`] based [`Service`]s template class ServiceBuilderPublishSubscribe { @@ -138,18 +149,17 @@ inline void ServiceBuilderPublishSubscribe::set_paramete [&](auto value) { iox2_service_builder_pub_sub_set_payload_alignment(&m_handle, value); }); m_max_nodes.and_then([&](auto value) { iox2_service_builder_pub_sub_set_max_nodes(&m_handle, value); }); + using ValueType = typename PayloadValueType::TYPE; + auto type_variant = iox::IsSlice::VALUE ? iox2_type_variant_e_DYNAMIC : iox2_type_variant_e_FIXED_SIZE; + // payload type details - const auto* payload_type_name = typeid(Payload).name(); + const auto* payload_type_name = typeid(ValueType).name(); const auto payload_type_name_len = strlen(payload_type_name); - const auto payload_type_size = sizeof(Payload); - const auto payload_type_align = alignof(Payload); + const auto payload_type_size = sizeof(ValueType); + const auto payload_type_align = alignof(ValueType); - const auto payload_result = iox2_service_builder_pub_sub_set_payload_type_details(&m_handle, - iox2_type_variant_e_FIXED_SIZE, - payload_type_name, - payload_type_name_len, - payload_type_size, - payload_type_align); + const auto payload_result = iox2_service_builder_pub_sub_set_payload_type_details( + &m_handle, type_variant, payload_type_name, payload_type_name_len, payload_type_size, payload_type_align); if (payload_result != IOX2_OK) { IOX_PANIC("This should never happen! Implementation failure while setting the Payload-Type."); @@ -162,13 +172,12 @@ inline void ServiceBuilderPublishSubscribe::set_paramete const auto user_header_type_size = header_layout.size(); const auto user_header_type_align = header_layout.alignment(); - const auto user_header_result = - iox2_service_builder_pub_sub_set_user_header_type_details(&m_handle, - iox2_type_variant_e_FIXED_SIZE, - user_header_type_name, - user_header_type_name_len, - user_header_type_size, - user_header_type_align); + const auto user_header_result = iox2_service_builder_pub_sub_set_user_header_type_details(&m_handle, + type_variant, + user_header_type_name, + user_header_type_name_len, + user_header_type_size, + user_header_type_align); if (user_header_result != IOX2_OK) { IOX_PANIC("This should never happen! Implementation failure while setting the User-Header-Type."); diff --git a/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp b/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp index 2a86db0f..bd9f99d5 100644 --- a/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp +++ b/iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp @@ -201,12 +201,12 @@ TYPED_TEST(ServicePublishSubscribeTest, loan_uninit_send_receive_works) { TYPED_TEST(ServicePublishSubscribeTest, loan_slice_send_receive_works) { constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE; - constexpr uint64_t MAX_SLICE_LEN = 10; + constexpr uint64_t PAYLOAD_ALIGNMENT = 8; + constexpr auto CXX_SLICE_MAX_LENGTH = 10; constexpr uint64_t DEFAULT_VALUE_A = 42; constexpr uint64_t DEFAULT_VALUE_B = 777; constexpr uint64_t DEFAULT_VALUE_Z = 21; - struct MyNestedStruct { uint64_t a { DEFAULT_VALUE_A }; uint64_t b { DEFAULT_VALUE_B }; @@ -221,43 +221,47 @@ TYPED_TEST(ServicePublishSubscribeTest, loan_slice_send_receive_works) { auto node = NodeBuilder().create().expect(""); auto service = node.service_builder(service_name) .template publish_subscribe>() - .payload_alignment(8) + .payload_alignment(PAYLOAD_ALIGNMENT) .create() .expect(""); - auto sut_publisher = service.publisher_builder().max_slice_len(MAX_SLICE_LEN).create().expect(""); + auto sut_publisher = service.publisher_builder().max_slice_len(CXX_SLICE_MAX_LENGTH).create().expect(""); auto sut_subscriber = service.subscriber_builder().create().expect(""); - auto sample = sut_publisher.loan_slice(MAX_SLICE_LEN).expect(""); - send(std::move(sample)).expect(""); + auto send_sample = sut_publisher.loan_slice(CXX_SLICE_MAX_LENGTH).expect(""); - auto recv_sample = sut_subscriber.receive().expect(""); - ASSERT_TRUE(recv_sample.has_value()); + send(std::move(send_sample)).expect(""); + + auto recv_result = sut_subscriber.receive().expect(""); + ASSERT_TRUE(recv_result.has_value()); + auto recv_sample = std::move(recv_result.value()); - auto counter = 0; - for (const auto& item : recv_sample.value().payload_slice()) { + auto iterations = 0; + for (const auto& item : recv_sample.payload_slice()) { ASSERT_THAT(item.z, Eq(DEFAULT_VALUE_Z)); ASSERT_THAT(item.data.a, Eq(DEFAULT_VALUE_A)); ASSERT_THAT(item.data.b, Eq(DEFAULT_VALUE_B)); - ++counter; + ++iterations; } - // ASSERT_THAT(counter, Eq(MAX_SLICE_LEN)); // Fails, gives 160 + + ASSERT_THAT(recv_sample.payload_slice().number_of_elements(), Eq(CXX_SLICE_MAX_LENGTH)); + ASSERT_THAT(iterations, Eq(CXX_SLICE_MAX_LENGTH)); } TYPED_TEST(ServicePublishSubscribeTest, loan_slice_uninit_send_receive_works) { constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE; - constexpr uint64_t MAX_SLICE_LEN = 10; + constexpr uint64_t PAYLOAD_ALIGNMENT = 8; + constexpr auto CXX_SLICE_MAX_LENGTH = 10; constexpr uint64_t DEFAULT_VALUE_A = 42; constexpr uint64_t DEFAULT_VALUE_B = 777; constexpr uint64_t DEFAULT_VALUE_Z = 21; - struct MyNestedStruct { - uint64_t a; - uint64_t b; + uint64_t a { DEFAULT_VALUE_A }; + uint64_t b { DEFAULT_VALUE_B }; }; struct MyStruct { - uint64_t z; + uint64_t z { DEFAULT_VALUE_Z }; MyNestedStruct data; }; @@ -266,76 +270,84 @@ TYPED_TEST(ServicePublishSubscribeTest, loan_slice_uninit_send_receive_works) { auto node = NodeBuilder().create().expect(""); auto service = node.service_builder(service_name) .template publish_subscribe>() - .payload_alignment(8) + .payload_alignment(PAYLOAD_ALIGNMENT) .create() .expect(""); - auto sut_publisher = service.publisher_builder().max_slice_len(MAX_SLICE_LEN).create().expect(""); + auto sut_publisher = service.publisher_builder().max_slice_len(CXX_SLICE_MAX_LENGTH).create().expect(""); auto sut_subscriber = service.subscriber_builder().create().expect(""); - auto sample = sut_publisher.loan_slice_uninit(MAX_SLICE_LEN).expect(""); + auto send_sample = sut_publisher.loan_slice_uninit(CXX_SLICE_MAX_LENGTH).expect(""); - auto counter = 0; - for (auto it = sample.payload_slice().begin(); it != sample.payload_slice().end(); ++it) { - new (it) MyStruct { DEFAULT_VALUE_Z + counter, - MyNestedStruct { DEFAULT_VALUE_A + counter, DEFAULT_VALUE_B + counter } }; - ++counter; + auto iterations = 0; + for (auto& item : send_sample.payload_slice()) { + new (&item) MyStruct { DEFAULT_VALUE_Z + iterations, + MyNestedStruct { DEFAULT_VALUE_A + iterations, DEFAULT_VALUE_B + iterations } }; + ++iterations; } - send(assume_init(std::move(sample))).expect(""); + send(assume_init(std::move(send_sample))).expect(""); - auto recv_sample = sut_subscriber.receive().expect(""); - ASSERT_TRUE(recv_sample.has_value()); + auto recv_result = sut_subscriber.receive().expect(""); + ASSERT_TRUE(recv_result.has_value()); + auto recv_sample = std::move(recv_result.value()); - counter = 0; - for (const auto& item : recv_sample.value().payload_slice()) { - ASSERT_THAT(item.z, Eq(DEFAULT_VALUE_Z + counter)); - ASSERT_THAT(item.data.a, Eq(DEFAULT_VALUE_A + counter)); - ASSERT_THAT(item.data.b, Eq(DEFAULT_VALUE_B + counter)); - ++counter; + iterations = 0; + for (const auto& item : recv_sample.payload_slice()) { + ASSERT_THAT(item.z, Eq(DEFAULT_VALUE_Z + iterations)); + ASSERT_THAT(item.data.a, Eq(DEFAULT_VALUE_A + iterations)); + ASSERT_THAT(item.data.b, Eq(DEFAULT_VALUE_B + iterations)); + ++iterations; } - // ASSERT_THAT(counter, Eq(MAX_SLICE_LEN)); // Fails, gives 160 + + ASSERT_THAT(recv_sample.payload_slice().number_of_elements(), Eq(CXX_SLICE_MAX_LENGTH)); + ASSERT_THAT(iterations, Eq(CXX_SLICE_MAX_LENGTH)); } TYPED_TEST(ServicePublishSubscribeTest, loan_slice_uninit_with_bytes_send_receive_works) { constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE; + constexpr uint64_t PAYLOAD_ALIGNMENT = 8; + constexpr auto CXX_SLICE_MAX_LENGTH = 10; constexpr uint64_t DEFAULT_VALUE_A = 42; constexpr uint64_t DEFAULT_VALUE_B = 777; constexpr uint64_t DEFAULT_VALUE_Z = 21; - struct MyNestedStruct { - uint64_t a; - uint64_t b; + uint64_t a { DEFAULT_VALUE_A }; + uint64_t b { DEFAULT_VALUE_B }; }; struct MyStruct { - uint64_t z; + uint64_t z { DEFAULT_VALUE_Z }; MyNestedStruct data; }; - constexpr size_t STRUCT_SIZE = sizeof(MyStruct); const auto service_name = iox2_testing::generate_service_name(); auto node = NodeBuilder().create().expect(""); - auto service = - node.service_builder(service_name).template publish_subscribe>().create().expect(""); + auto service = node.service_builder(service_name) + .template publish_subscribe>() + .payload_alignment(PAYLOAD_ALIGNMENT) + .create() + .expect(""); - auto sut_publisher = service.publisher_builder().max_slice_len(STRUCT_SIZE).create().expect(""); + auto sut_publisher = service.publisher_builder().max_slice_len(sizeof(MyStruct)).create().expect(""); auto sut_subscriber = service.subscriber_builder().create().expect(""); - auto sample = sut_publisher.loan_slice_uninit(STRUCT_SIZE).expect(""); + auto send_sample = sut_publisher.loan_slice_uninit(sizeof(MyStruct)).expect(""); - new (sample.payload_slice().data()) + new (send_sample.payload_slice().data()) MyStruct { DEFAULT_VALUE_Z, MyNestedStruct { DEFAULT_VALUE_A, DEFAULT_VALUE_B } }; - send(assume_init(std::move(sample))).expect(""); - auto recv_sample = sut_subscriber.receive().expect(""); - ASSERT_TRUE(recv_sample.has_value()); + send(assume_init(std::move(send_sample))).expect(""); + + auto recv_result = sut_subscriber.receive().expect(""); + ASSERT_TRUE(recv_result.has_value()); + + auto recv_sample = std::move(recv_result.value()); + ASSERT_THAT(recv_sample.payload_slice().number_of_elements(), Eq(sizeof(MyStruct))); - auto recv_payload = recv_sample.value().payload_slice(); - auto recv_data = reinterpret_cast(recv_sample.value().payload_slice().data()); + auto recv_data = reinterpret_cast(recv_sample.payload_slice().data()); - // ASSERT_THAT(recv_payload.len(), Eq(STRUCT_SIZE)); // Fails, gives 384 ASSERT_THAT(recv_data->z, Eq(DEFAULT_VALUE_Z)); ASSERT_THAT(recv_data->data.a, Eq(DEFAULT_VALUE_A)); ASSERT_THAT(recv_data->data.b, Eq(DEFAULT_VALUE_B)); @@ -509,17 +521,18 @@ TYPED_TEST(ServicePublishSubscribeTest, publisher_applies_unable_to_deliver_stra TYPED_TEST(ServicePublishSubscribeTest, publisher_applies_max_slice_len) { constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE; - constexpr uint64_t MAX_SLICE_LEN = 256; + constexpr uint64_t DESIRED_MAX_SLICE_LEN = 10; + using ValueType = uint8_t; const auto service_name = iox2_testing::generate_service_name(); auto node = NodeBuilder().create().expect(""); auto service = - node.service_builder(service_name).template publish_subscribe>().create().expect(""); + node.service_builder(service_name).template publish_subscribe>().create().expect(""); - auto sut = service.publisher_builder().max_slice_len(MAX_SLICE_LEN).create().expect(""); + auto sut = service.publisher_builder().max_slice_len(DESIRED_MAX_SLICE_LEN).create().expect(""); - ASSERT_THAT(sut.max_slice_len(), Eq(MAX_SLICE_LEN)); + ASSERT_THAT(sut.max_slice_len(), Eq(DESIRED_MAX_SLICE_LEN)); } TYPED_TEST(ServicePublishSubscribeTest, send_receive_with_user_header_works) { diff --git a/iceoryx2-ffi/ffi/src/api/publisher.rs b/iceoryx2-ffi/ffi/src/api/publisher.rs index ec27ee16..266ecc76 100644 --- a/iceoryx2-ffi/ffi/src/api/publisher.rs +++ b/iceoryx2-ffi/ffi/src/api/publisher.rs @@ -72,18 +72,14 @@ impl IntoCInt for PublisherSendError { impl IntoCInt for PublisherLoanError { fn into_c_int(self) -> c_int { (match self { - PublisherLoanError::OutOfMemory => { - iox2_publisher_send_error_e::LOAN_ERROR_OUT_OF_MEMORY - } + PublisherLoanError::OutOfMemory => iox2_publisher_loan_error_e::OUT_OF_MEMORY, PublisherLoanError::ExceedsMaxLoanedSamples => { - iox2_publisher_send_error_e::LOAN_ERROR_EXCEEDS_MAX_LOANED_SAMPLES + iox2_publisher_loan_error_e::EXCEEDS_MAX_LOANED_SAMPLES } PublisherLoanError::ExceedsMaxLoanSize => { - iox2_publisher_send_error_e::LOAN_ERROR_EXCEEDS_MAX_LOAN_SIZE - } - PublisherLoanError::InternalFailure => { - iox2_publisher_send_error_e::LOAN_ERROR_INTERNAL_FAILURE + iox2_publisher_loan_error_e::EXCEEDS_MAX_LOAN_SIZE } + PublisherLoanError::InternalFailure => iox2_publisher_loan_error_e::INTERNAL_FAILURE, }) as c_int } } @@ -362,6 +358,7 @@ pub unsafe extern "C" fn iox2_publisher_send_copy( /// * `sample_struct_ptr` - Must be either a NULL pointer or a pointer to a valid [`iox2_sample_mut_t`]. /// If it is a NULL pointer, the storage will be allocated on the heap. /// * `sample_handle_ptr` - An uninitialized or dangling [`iox2_sample_mut_h`] handle which will be initialized by this function call if a sample is obtained, otherwise it will be set to NULL. +/// * `number_of_bytes` - The number of bytes to loan from the publisher's payload segment /// /// Return [`IOX2_OK`] on success, otherwise [`iox2_publisher_loan_error_e`]. /// @@ -369,21 +366,12 @@ pub unsafe extern "C" fn iox2_publisher_send_copy( /// /// * `publisher_handle` is valid and non-null /// * The `sample_handle_ptr` is pointing to a valid [`iox2_sample_mut_h`]. -#[no_mangle] -pub unsafe extern "C" fn iox2_publisher_loan( - publisher_handle: iox2_publisher_h_ref, - sample_struct_ptr: *mut iox2_sample_mut_t, - sample_handle_ptr: *mut iox2_sample_mut_h, -) -> c_int { - iox2_publisher_loan_slice_uninit(1, publisher_handle, sample_struct_ptr, sample_handle_ptr) -} - #[no_mangle] pub unsafe extern "C" fn iox2_publisher_loan_slice_uninit( - number_of_elements: usize, publisher_handle: iox2_publisher_h_ref, sample_struct_ptr: *mut iox2_sample_mut_t, sample_handle_ptr: *mut iox2_sample_mut_h, + number_of_elements: usize, ) -> c_int { publisher_handle.assert_non_null(); debug_assert!(!sample_handle_ptr.is_null()); diff --git a/iceoryx2-ffi/ffi/src/api/sample.rs b/iceoryx2-ffi/ffi/src/api/sample.rs index ea539f21..2c676ab2 100644 --- a/iceoryx2-ffi/ffi/src/api/sample.rs +++ b/iceoryx2-ffi/ffi/src/api/sample.rs @@ -214,12 +214,13 @@ pub unsafe extern "C" fn iox2_sample_user_header( /// /// * `handle` obtained by [`iox2_subscriber_receive()`](crate::iox2_subscriber_receive()) /// * `payload_ptr` a valid, non-null pointer pointing to a [`*const c_void`] pointer. -/// * `number_of_elements` (optional) either a null poitner or a valid pointer pointing to a [`c_size_t`]. +/// * `number_of_bytes` (optional) either a null poitner or a valid pointer pointing to a [`c_size_t`] with +/// the number of bytes used by the sample. #[no_mangle] pub unsafe extern "C" fn iox2_sample_payload( handle: iox2_sample_h_ref, payload_ptr: *mut *const c_void, - number_of_elements: *mut c_size_t, + number_of_bytes: *mut c_size_t, ) { handle.assert_non_null(); debug_assert!(!payload_ptr.is_null()); @@ -232,8 +233,8 @@ pub unsafe extern "C" fn iox2_sample_payload( }; *payload_ptr = payload.as_ptr().cast(); - if !number_of_elements.is_null() { - *number_of_elements = payload.len(); + if !number_of_bytes.is_null() { + *number_of_bytes = payload.len(); } }