From 379a2c82e5f87e5eb0cfdb92db475d0fd854f574 Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Mon, 25 Dec 2023 03:06:55 +0100 Subject: [PATCH] [#56] Introduce `from_bytes_truncated` --- ROADMAP.md | 11 ++++++ doc/release-notes/iceoryx2-unreleased.md | 2 +- iceoryx2-bb/container/src/byte_string.rs | 25 +++++++++++++ .../container/tests/byte_string_tests.rs | 37 +++++++++++++++---- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index bec0acf75..20d60f879 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -7,6 +7,17 @@ * [ ] `#![no_std]` on `stable` on all tier 1 platforms * [ ] completely dynamic setup with dynamic shared memory +## Shared Memory Container & Types + +* [ ] Make `iceoryx2_bb_container` public with announcement +* [ ] Create and document dynamic size container concept for shared memory and apply it + to all existing containers: `ByteString`, `Vec`, `Queue` + * Open Question: How can these containers be cloned, copied? +* [ ] Introduce additional containers: `HashMap`, `Tree`, `Set`, `List` +* [ ] Introduce elementary types, look into: `simple-si-units` crate + * Add types like: memory size, percentage, strict percentage (0..100), data throughput, resolution + (further types found in informatics) + ## Language Bindings * [ ] C diff --git a/doc/release-notes/iceoryx2-unreleased.md b/doc/release-notes/iceoryx2-unreleased.md index e8a510de1..918d3b8c2 100644 --- a/doc/release-notes/iceoryx2-unreleased.md +++ b/doc/release-notes/iceoryx2-unreleased.md @@ -22,7 +22,7 @@ ### New API features - * Example [#1](https://github.com/eclipse-iceoryx/iceoryx2/issues/1) + * Add `FixedSizeByteString::from_bytes_truncated` [#56](https://github.com/eclipse-iceoryx/iceoryx2/issues/56) ### API Breaking Changes diff --git a/iceoryx2-bb/container/src/byte_string.rs b/iceoryx2-bb/container/src/byte_string.rs index 2f1fb638c..17763bd5d 100644 --- a/iceoryx2-bb/container/src/byte_string.rs +++ b/iceoryx2-bb/container/src/byte_string.rs @@ -237,6 +237,31 @@ impl FixedSizeByteString { Ok(new_self) } + /// Creates a new [`FixedSizeByteString`] from a byte slice. If the byte slice does not fit + /// into the [`FixedSizeByteString`] it will be truncated. + pub fn from_bytes_truncated(bytes: &[u8]) -> Self { + let mut new_self = Self::new(); + new_self.len = std::cmp::min(bytes.len(), CAPACITY); + unsafe { + std::ptr::copy( + bytes.as_ptr(), + new_self.data.as_ptr() as *mut u8, + bytes.len(), + ) + }; + + let zero = 0u8; + unsafe { + std::ptr::copy( + &zero, + new_self.data.as_ptr().add(new_self.len) as *mut u8, + 1, + ) + }; + + new_self + } + /// Creates a new byte string from a given null-terminated string /// /// # Safety diff --git a/iceoryx2-bb/container/tests/byte_string_tests.rs b/iceoryx2-bb/container/tests/byte_string_tests.rs index 0e58d6efd..1c1e9b633 100644 --- a/iceoryx2-bb/container/tests/byte_string_tests.rs +++ b/iceoryx2-bb/container/tests/byte_string_tests.rs @@ -47,19 +47,40 @@ fn fixed_size_byte_string_from_bytes_works() { assert_that!(sut.pop(), eq Some(b'd')); } +#[test] +fn fixed_size_byte_string_from_bytes_truncated_works() { + let sut = Sut::from_bytes(b"bonjour world"); + assert_that!(sut, is_ok); + let mut sut = sut.unwrap(); + + assert_that!(sut, is_not_empty); + assert_that!(sut.is_full(), eq false); + assert_that!(sut, len 13); + assert_that!(sut, eq b"bonjour world"); + assert_that!(sut, ne b"bonjour world! woo"); + assert_that!(sut.as_bytes(), eq b"bonjour world"); + assert_that!(sut.as_mut_bytes(), eq b"bonjour world"); + assert_that!(sut.as_bytes_with_nul(), eq b"bonjour world\0"); + assert_that!(sut.pop(), eq Some(b'd')); +} + #[test] fn fixed_size_byte_string_from_byte_slice_works() { - let mut sut = Sut::from(b"hello world!"); + let sut = FixedSizeByteString::<5>::from_bytes_truncated(b"hell"); assert_that!(sut, is_not_empty); assert_that!(sut.is_full(), eq false); - assert_that!(sut, len 12); - assert_that!(sut, eq b"hello world!"); - assert_that!(sut, ne b"hello world! woo"); - assert_that!(sut.as_bytes(), eq b"hello world!"); - assert_that!(sut.as_mut_bytes(), eq b"hello world!"); - assert_that!(sut.as_bytes_with_nul(), eq b"hello world!\0"); - assert_that!(sut.pop(), eq Some(b'!')); + assert_that!(sut, len 4); + assert_that!(sut, eq b"hell"); + assert_that!(sut.as_bytes_with_nul(), eq b"hell\0"); + + let sut = FixedSizeByteString::<5>::from_bytes_truncated(b"hello world"); + + assert_that!(sut, is_not_empty); + assert_that!(sut.is_full(), eq true); + assert_that!(sut, len 5); + assert_that!(sut, eq b"hello"); + assert_that!(sut.as_bytes_with_nul(), eq b"hello\0"); } #[test]