Skip to content

Commit

Permalink
[#56] Introduce from_bytes_truncated
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Dec 25, 2023
1 parent 03a4ec2 commit 379a2c8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
11 changes: 11 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/release-notes/iceoryx2-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions iceoryx2-bb/container/src/byte_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,31 @@ impl<const CAPACITY: usize> FixedSizeByteString<CAPACITY> {
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
Expand Down
37 changes: 29 additions & 8 deletions iceoryx2-bb/container/tests/byte_string_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 379a2c8

Please sign in to comment.