Skip to content

Conversation

azerupi
Copy link
Contributor

@azerupi azerupi commented Aug 27, 2025

Follow-up on #370

This adds a wrapper around char arrays that allows to access them either as slices or as strings. When accessing the string representation, it will automatically convert the char array to a string slice up to the first null byte.

A part I have some slight doubts about is the serde derives on the CharArray<N> struct. If someone could validate that this is correct that would be great.

This adds a wrapper around char arrays that allows
to access them either as slices or as strings. When
accessing the string representation, it will automatically
convert the char array to a string slice up to the first null byte.
@joaoantoniocardoso joaoantoniocardoso self-requested a review August 28, 2025 16:55
@onur-ozkan onur-ozkan self-requested a review August 29, 2025 04:42
@onur-ozkan
Copy link
Collaborator

I will try to allocate sometime at Sunday to review this.

Comment on lines +23 to +26
#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct CharArray<const N: usize> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct CharArray<const N: usize> {
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct CharArray<const N: usize> {

Comment on lines +57 to +60
/// Returns the string stopping at the first null byte and if the string is not valid utf8
/// the returned string will be empty.
pub fn to_str(&self) -> &str {
core::str::from_utf8(&self.data[..self.str_len]).unwrap_or("")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that we return empty string if the value is invalid? I think the caller should decide what to do on invalid UTF8 values. We can simply return the inner Result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants