Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CString cont. #549

Open
wants to merge 79 commits into
base: main
Choose a base branch
from
Open

Add CString cont. #549

wants to merge 79 commits into from

Conversation

raviqqe
Copy link

@raviqqe raviqqe commented Apr 7, 2025

This PR is a continued work of #342, the heapless implementation of alloc::ffi::CString. Credits to @vrmiguel.

Closes #330.

Notable changes from the previous PR

  • Implement Deref<Target = CStr> for CString.
    • As well as Default, AsRef, Borrow, PartialEq, Eq, PartialOrd, and Ord
  • Remove the len and as_str_unchecked associated functions that should be implemented in the upstream of core::ffi::CStr.
  • Rename the push_bytes associated function extend_from_bytes.
    • This change is to align the nominology with heapless::Vec.

Future work

  • heapless::String interoperability
    • CString::into_string
  • heapless::Vec interoperability
    • CString::from_vec_with_nul
    • CString::into_bytes
    • CString::into_bytes_with_nul

src/c_string.rs Outdated
///
/// assert_eq!(cstr.to_str(), Ok("hey there"));
/// ```
pub fn push_bytes(&mut self, bytes: &[u8]) -> Result<(), CapacityError> {
Copy link
Author

Choose a reason for hiding this comment

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

Should we rename this extend_from_bytes or extend_from_slice?

Copy link
Author

Choose a reason for hiding this comment

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

I renamed it extend_from_bytes for now. extend_from_slice is a bit ambiguous because it might mean either byte or CStr slices.

@raviqqe raviqqe marked this pull request as ready for review April 8, 2025 01:07
@raviqqe raviqqe requested review from reitermarkus and vrmiguel April 8, 2025 01:08
@raviqqe raviqqe marked this pull request as draft April 8, 2025 01:14
@raviqqe raviqqe marked this pull request as ready for review April 8, 2025 02:53
src/c_string.rs Outdated
return Err(CapacityError.into());
}

match memchr(0, bytes) {
Copy link

Choose a reason for hiding this comment

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

I was wondering if we should use CStr::from_bytes_with_nul here directly (it uses the memchr crate)

    match CStr::from_bytes_with_nul(bytes) {
        Ok(_cstr) => {
            unsafe { self.extend_from_bytes_unchecked(bytes) }?;
            Ok(())
        },
        Err(FromBytesWithNulError::InteriorNul { position }) => {
            Err(ExtendError::InteriorNulByte(position))
        },
        Err(FromBytesWithNulError::NotNulTerminated) => {
            unsafe {
                self.extend_from_bytes_unchecked(bytes).unwrap();
                self.inner.push_unchecked(0);
            };
            Ok(())
        }
    }
}

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense.

@reitermarkus reitermarkus requested a review from a team April 8, 2025 21:39
@raviqqe raviqqe requested a review from vrmiguel April 9, 2025 04:53
Copy link

@vrmiguel vrmiguel left a comment

Choose a reason for hiding this comment

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

Awesome stuff @raviqqe !

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.

Is there interest for heapless::CString?
3 participants