-
Notifications
You must be signed in to change notification settings - Fork 205
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
base: main
Are you sure you want to change the base?
Add CString
cont.
#549
Conversation
src/c_string.rs
Outdated
/// | ||
/// assert_eq!(cstr.to_str(), Ok("hey there")); | ||
/// ``` | ||
pub fn push_bytes(&mut self, bytes: &[u8]) -> Result<(), CapacityError> { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
src/c_string.rs
Outdated
return Err(CapacityError.into()); | ||
} | ||
|
||
match memchr(0, bytes) { |
There was a problem hiding this comment.
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(())
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome stuff @raviqqe !
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
Deref<Target = CStr>
forCString
.Default
,AsRef
,Borrow
,PartialEq
,Eq
,PartialOrd
, andOrd
len
andas_str_unchecked
associated functions that should be implemented in the upstream ofcore::ffi::CStr
.push_bytes
associated functionextend_from_bytes
.heapless::Vec
.Future work
heapless::String
interoperabilityCString::into_string
heapless::Vec
interoperabilityCString::from_vec_with_nul
CString::into_bytes
CString::into_bytes_with_nul