pub struct CFixedString { /* private fields */ }
Expand description
This type owns a fixed length buffer containing a C-style string. The string is terminated by the first null byte in the buffer, or by the length of the buffer if no null byte is present.
The length of the buffer is known as the limit of the CFixedString.
The len()
method returns the length of the string within the buffer.
Any bytes after the first null byte are not considered part of the string and may contain garbage data. When comparing or hashing CFixedStrings, these bytes are not included.
§Performance
Be aware that many of the methods on this type which would normally be expected to run in constant time may run in time linear in the length of the string. For this reason, it is recommended to use the type only for interop with C code, and to convert to a standard string or byte buffer as soon as possible.
Implementations§
Source§impl CFixedString
impl CFixedString
Sourcepub fn new<T: Into<Box<[u8]>>>(t: T) -> CFixedString
pub fn new<T: Into<Box<[u8]>>>(t: T) -> CFixedString
Construct a new fixed-length C string from a byte slice The first null byte terminates the string, but the length of the slice determines the limit of this fixed-size string.
Sourcepub fn with_limit(limit: usize) -> CFixedString
pub fn with_limit(limit: usize) -> CFixedString
Construct an empty string with the specified limit. The buffer is entirely zero-initialized.
Sourcepub unsafe fn from_raw_parts(ptr: *mut c_char, limit: usize) -> CFixedString
pub unsafe fn from_raw_parts(ptr: *mut c_char, limit: usize) -> CFixedString
Retakes ownership of a CFixedString that was transferred to C.
§Safety
This should only ever be called with a pointer and limit that was earlier obtained from a CFixedString. The previous string must have been forgotten to ensure that the memory has not already been freed.
Sourcepub fn into_string(self) -> Result<String, IntoStringError>
pub fn into_string(self) -> Result<String, IntoStringError>
Converts the CFixedString into a String if it contains valid Unicode data.
On failure, ownership of the original CFixedString is returned.
Sourcepub fn into_c_string(self) -> CString
pub fn into_c_string(self) -> CString
Converts the CFixedString into a CString. If necessary, the buffer will be extended to make room for a final null byte. If the buffer does not need to be extended, no allocation will be performed.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Converts the CFixedString into a byte buffer. The length of the buffer will equal the length of the string up to but not including the first null byte. The capacity of the buffer will equal the limit of the CFixedString.
Sourcepub fn into_bytes_full(self) -> Vec<u8> ⓘ
pub fn into_bytes_full(self) -> Vec<u8> ⓘ
Converts the CFixedString into a byte buffer. The length of the buffer will equal the limit of the CFixedString. The buffer may contain garbage values after the first null byte.
Sourcepub fn as_c_fixed_str(&self) -> &CFixedStr
pub fn as_c_fixed_str(&self) -> &CFixedStr
Extracts a CFixedStr slice containing the entire buffer.
Sourcepub fn as_c_fixed_str_mut(&mut self) -> &mut CFixedStr
pub fn as_c_fixed_str_mut(&mut self) -> &mut CFixedStr
Extracts a mutable CFixedStr slice containing the entire buffer.
Sourcepub fn into_boxed_c_fixed_str(self) -> Box<CFixedStr>
pub fn into_boxed_c_fixed_str(self) -> Box<CFixedStr>
Converts this CFixedString into a boxed CFixedStr
Methods from Deref<Target = CFixedStr>§
Sourcepub fn as_ptr(&self) -> *const c_char
pub fn as_ptr(&self) -> *const c_char
Returns the inner pointer to this CFixedStr.
§Safety
It is your responsibility to make sure the underlying memory is not freed to early.
Sourcepub fn as_mut_ptr(&mut self) -> *mut c_char
pub fn as_mut_ptr(&mut self) -> *mut c_char
Returns the inner pointer to this mutable CFixedStr.
§Safety
It is your responsibility to make sure the underlying memory is not freed to early.
Sourcepub fn limit(&self) -> usize
pub fn limit(&self) -> usize
Returns the limit of this CFixedStr. This corresponds to the longest possible string that could be stored here.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the CFixedStr. This operation takes linear time.
Sourcepub fn to_bytes(&self) -> &[u8] ⓘ
pub fn to_bytes(&self) -> &[u8] ⓘ
Coverts this CFixedStr to a byte slice. The length of the slice is equal to the length of the string up to but not including the first null byte.
Sourcepub fn to_bytes_mut(&mut self) -> &mut [u8] ⓘ
pub fn to_bytes_mut(&mut self) -> &mut [u8] ⓘ
Coverts this CFixedStr to a mutable byte slice. The length of the slice is equal to the length of the string up to but not including the first null byte.
Sourcepub fn to_bytes_extended(&self) -> &[u8] ⓘ
pub fn to_bytes_extended(&self) -> &[u8] ⓘ
Coverts this CFixedStr to a byte slice. The length of the slice is equal to the length of the string up to and including the first null byte, if it exists.
Sourcepub fn to_bytes_mut_extended(&mut self) -> &mut [u8] ⓘ
pub fn to_bytes_mut_extended(&mut self) -> &mut [u8] ⓘ
Coverts this CFixedStr to a mutable byte slice. The length of the slice is equal to the length of the string up to and including the first null byte, if it exists.
Sourcepub fn as_bytes_full(&self) -> &[u8] ⓘ
pub fn as_bytes_full(&self) -> &[u8] ⓘ
Coverts this CFixedStr to a byte slice. The length of the slice is equal to the limit of the CFixedStr.
Sourcepub fn as_bytes_mut_full(&mut self) -> &mut [u8] ⓘ
pub fn as_bytes_mut_full(&mut self) -> &mut [u8] ⓘ
Coverts this CFixedStr to a mutable byte slice. The length of the slice is equal to the limit of the CFixedStr.
Sourcepub fn to_str(&self) -> Result<&str, Utf8Error>
pub fn to_str(&self) -> Result<&str, Utf8Error>
Yields a &str slice if the CFixedStr contains valid UTF-8.
This function will calculate the length of this string and check for UTF-8 validity, and then return the &str if it’s valid.
Sourcepub fn to_string_lossy(&self) -> Cow<'_, str>
pub fn to_string_lossy(&self) -> Cow<'_, str>
Converts a CFixedStr into a Cow
This function will calculate the length of this string and then return the resulting
slice as a Cow<str>
, replacing any invalid UTF-8 sequences with U+FFFD REPLACEMENT CHARACTER
.